Skip to content

Commit

Permalink
jwt token,UserController,Usermodel
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv9544 committed Oct 26, 2023
1 parent 325fc47 commit cff47a9
Show file tree
Hide file tree
Showing 13 changed files with 1,268 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
node_modules/

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
32 changes: 32 additions & 0 deletions Controller/UserController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const UserModel = require("../Model/UserModel");
const jwt = require("jsonwebtoken");
require("dotenv").config();
const { ACCESS_TOKEN_SECRET } = process.env;

module.exports.signup = async function (req, res) {
let User = new UserModel(req.body);
console.log(User);

let data = await User.save();

res.json({ data: data, msg: "User Added", rcode: 200 });
};

module.exports.login = async function (req, res) {
const { Email, Password } = req.body;

let User = await UserModel.findOne({ Email: Email });

if (User && User.Password == Password) {

const accesstoken = jwt.sign(
{ Email:User.Email , Password:User.Password },
ACCESS_TOKEN_SECRET,
{ expiresIn: "1d" }
);
console.log("accesstoken=>" + accesstoken);
res.json({ data: User, msg: "login done", token: accesstoken, rcode: 200 });
} else {
res.json({ data: req.body, msg: "Invalid credential", rcode: -9 });
}
};
Empty file removed Controller/admin.js
Empty file.
44 changes: 44 additions & 0 deletions Middleware/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const jwt = require("jsonwebtoken");
const { ACCESS_TOKEN_SECRET } = process.env;


module.exports = function (req, res, next) {
console.log("In the AuthMidd");
token = req.headers.token;
console.log("token=>" + token);

jwt.verify(req.headers.token, ACCESS_TOKEN_SECRET, function (err, decode) {
if (err) {

if(err.name === "JsonWebTokenError")
{
console.log(err);
res.json({
msg: "Please Login before acccess the service ",
rcode: -9,
data: "",
});
}

if ( err.name === "TokenExpiredError") {

jwt.verify(req.headers.refreshtoken, ACCESS_TOKEN_SECRET, function (err, decoded,next) {
if (err) {
console.log(err);
res.json({
msg: "Please Login before acccess the service 2",
rcode: -9,
data: "",
});
} else {
console.log("decoded from referesh => ", decoded);
next();
}
});
}
} else {
console.log("decoded => ", decode);
next();
}
});
};
57 changes: 57 additions & 0 deletions Model/UserModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const mongoose = require("mongoose");
const validator = require("validator");

const UserSchema = new mongoose.Schema({
Name: {
type: String,
required: [true, "Please Enter your name"],
trim: true,
},

Email: {
type: String,
required: [true, "Please Enter your email"],
trim: true,
unique: true,
validate: [validator.isEmail, "please enter a valid email"],
},

Password: {
type: String,
require: true,
minlength: 8,
trim: true,
},

ContactNum: {
type: String,
required: [true, "Please Enter your contactnumber"],
require: true,
trim: true,
},

DOB: {
type: Date,
required: [true, "Please Enter your date of birth"],
require: true,
},

Role:{
type:Number
},

IsActive: {
type: Number,
default: 1,
},

DistrictId: { type: mongoose.SchemaTypes.ObjectId, ref: "districts" },
createdBy: { type: mongoose.SchemaTypes.ObjectId, ref: "Users" },

CreatedAt: {
type: Date,
default: Date.now(),
},
});

module.exports = mongoose.model("User", UserSchema);
Empty file removed Model/admin.js
Empty file.
9 changes: 9 additions & 0 deletions Routes/UserRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express')
const UserController = require('../Controller/UserController')

const route = express.Router()

route.post('/signup',UserController.signup)
route.post('/login',UserController.login)

module.exports = route
Empty file removed Routes/admin.js
Empty file.
Empty file removed config/db.js
Empty file.
10 changes: 10 additions & 0 deletions config/dbconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require("mongoose")
require('dotenv').config()

const {MONGO_URI} = process.env

module.exports.getDbConnetion = function(){
mongoose.connect(MONGO_URI).then(()=>console.log("DB connected")).catch((err)=>{
console.log(err);
})
}
Loading

0 comments on commit cff47a9

Please sign in to comment.