-
Notifications
You must be signed in to change notification settings - Fork 0
/
userController.js
61 lines (49 loc) · 1.75 KB
/
userController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// const {v4: uuidv4} = require('uuid') // NO NEED OF YOU 'jwt in useg'
const bcrypt = require('bcrypt');
const userModel = require('../models/userSchema');
const {setUser} = require('../service/auth');
const OtpMailSender = require('../service/optMailSender')
const otpR = Math.random();
const otpGenrate = Math.round(otpR*9000+1000);
const mostOtp = otpGenrate;
console.log(otpGenrate, mostOtp);
async function HandelSingup(req, res){
const saltRounds = 10;
const {name, email, password} = req.body;
if(!name || !email || !password) return res.render('singup');
// PASSWORD HASING!
bcrypt.genSalt(saltRounds, async(error, salt)=>{
bcrypt.hash(password, salt, async(error, hash)=>{
await userModel.create({name, email, password: hash});
})
});
res.render('login');
}
async function HandelLogin(req, res){
const {email, password} = req.body;
const userEmail = await userModel.findOne({email});
if(!userEmail) return res.render('login');;
const matchPassword = await bcrypt.compare(password, userEmail.password); // MATCHING USER GIVEN PASSWORD WITH DB HASH PASSWORD!
if(matchPassword){
OtpMailSender(email, otpGenrate); // SENDEING OTP VIA EMAIL
const token = setUser(userEmail);
res.cookie('uid', token);
return res.render('otpCheck');
} else{
return res.render('login');
}
}
async function HandelOtpReq(req, res){
const {otpSubmit} = req.body;
if(!otpSubmit) return res. render('otpCheck');
if(otpSubmit==mostOtp){
return res.redirect('/');
} else{
return res.render('otpCheck');
}
}
module.exports = {
HandelSingup,
HandelLogin,
HandelOtpReq,
}