-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,268 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
} |
Oops, something went wrong.