Created Signup and Sign In
This commit is contained in:
parent
b0569011e8
commit
52f39cf3ae
7
app.js
7
app.js
|
@ -1,8 +1,15 @@
|
|||
const express = require('express') ;
|
||||
const mongoose = require('mongoose') ;
|
||||
const bodyparser = require('body-parser') ;
|
||||
const authRoute = require('./routes/auth') ;
|
||||
|
||||
const app = express() ;
|
||||
const MONGO_URI = `mongodb+srv://Cantilever:Cantilever@cluster0.dqxva.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`
|
||||
|
||||
app.use(bodyparser.json()) ;
|
||||
|
||||
app.use(authRoute) ;
|
||||
|
||||
mongoose.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }).
|
||||
then(result => {
|
||||
console.log('connected');
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
const bcrypt = require('bcryptjs') ;
|
||||
const User = require('../models/User') ;
|
||||
|
||||
module.exports.postSignup = async (req , res , next) => {
|
||||
try
|
||||
{
|
||||
let firstName = req.body.firstName || " " ;
|
||||
let lastName = req.body.lastName || " " ;
|
||||
let email = req.body.email ;
|
||||
let phoneNumber = req.body.phoneNumber ;
|
||||
let password = req.body.password ;
|
||||
let user = await User.findOne({email:email}) ;
|
||||
if(user)
|
||||
{
|
||||
res.json({
|
||||
message:"User already exist" ,
|
||||
type:"error"
|
||||
})
|
||||
}
|
||||
else
|
||||
{
|
||||
const hashedPass = await bcrypt.hash(password , 12) ;
|
||||
user = new User({
|
||||
firstName : firstName ,
|
||||
lastName : lastName ,
|
||||
email : email ,
|
||||
password : hashedPass ,
|
||||
phoneNumber : phoneNumber
|
||||
}) ;
|
||||
user = await user.save() ;
|
||||
res.json({
|
||||
message:"Successfully signed Up" ,
|
||||
type:"success"
|
||||
})
|
||||
}
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
console.log(err);
|
||||
}
|
||||
} ;
|
||||
|
||||
|
||||
module.exports.postSignin = async (req , res , next) => {
|
||||
try
|
||||
{
|
||||
let email = req.body.email ;
|
||||
let password = req.body.password ;
|
||||
let user = await User.findOne({email : email}) ;
|
||||
if(user)
|
||||
{
|
||||
const isMathced = await bcrypt.compare(password , user.password) ;
|
||||
if(isMathced)
|
||||
{
|
||||
res.json({
|
||||
message:"Logged In" ,
|
||||
type : "success"
|
||||
})
|
||||
}
|
||||
else
|
||||
{
|
||||
res.json({
|
||||
message:"email and password doesn't match" ,
|
||||
type:"error"
|
||||
})
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
res.json({
|
||||
message:"No user with this email exists" ,
|
||||
type : "error"
|
||||
})
|
||||
}
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
console.log(err);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
const mongoose = require('mongoose') ;
|
||||
|
||||
const Schema = mongoose.Schema ;
|
||||
|
||||
const userSchema = new Schema({
|
||||
firstName : {
|
||||
type :String ,
|
||||
required : true
|
||||
} ,
|
||||
lastName : {
|
||||
type:String ,
|
||||
required: true
|
||||
} ,
|
||||
email : {
|
||||
type:String ,
|
||||
required: true
|
||||
} ,
|
||||
phoneNumber : {
|
||||
type : String ,
|
||||
required : true
|
||||
} ,
|
||||
password : {
|
||||
type : String ,
|
||||
required : true
|
||||
}
|
||||
}) ;
|
||||
|
||||
module.exports = mongoose.model("User" , userSchema) ;
|
|
@ -114,6 +114,11 @@
|
|||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
|
||||
"dev": true
|
||||
},
|
||||
"bcryptjs": {
|
||||
"version": "2.4.3",
|
||||
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
|
||||
"integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms="
|
||||
},
|
||||
"binary-extensions": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz",
|
||||
|
|
|
@ -4,11 +4,13 @@
|
|||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "nodemon app.js"
|
||||
"start:dev": "nodemon app.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"bcryptjs": "^2.4.3",
|
||||
"body-parser": "^1.19.0",
|
||||
"express": "^4.17.1",
|
||||
"mongoose": "^5.12.2"
|
||||
},
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
const express = require('express') ;
|
||||
const authController = require('../controllers/auth') ;
|
||||
|
||||
const router = express.Router() ;
|
||||
|
||||
router.post('/signup' , authController.postSignup) ;
|
||||
|
||||
router.post('/signin' , authController.postSignin) ;
|
||||
|
||||
module.exports = router ;
|
Loading…
Reference in New Issue