You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Зміна статусу email-у на "верифікований" та очищення верифікаційного коду в БД після підтвердження пошти в отриманому листі
46
+
constverifyEmail=async(req,res)=>{
47
+
const{ verificationCode }=req.params;
48
+
constuser=awaitUser.findOne({verificationCode});// перевіряємо чи є в БД користувач з таким кодом
49
+
if(!user){
50
+
throwHttpError(401,'Email is not found')
51
+
}
52
+
awaitUser.findByIdAndUpdate(user._id,{verify: true,verificationCode: ''});// якщо є такий користувач, то вносимо зміни до БД
53
+
54
+
console.log('user:',user);
55
+
56
+
res.json({
57
+
message: "The email is successfully verified."
58
+
})
59
+
};
60
+
61
+
constresendVerifyEmail=async(req,res)=>{
62
+
const{ email }=req.body;
63
+
constuser=awaitUser.findOne({email});
64
+
if(!user){
65
+
throwHttpError(401,'Email is not found');
66
+
}
67
+
if(user.verify){//якщо користувач не підтвердив email (user.verify = false)
68
+
throwHttpError(401,'The email is already verified')
69
+
}
70
+
constverifyEmail={
71
+
to: email,
72
+
subject: 'Verify email',
73
+
html: `<a target='_blank' href="${BASE_URL}/api/auth/verify/${user.verificationCode}">Let's verify your email so you can start login. Click here to verify.</a>`,
74
+
};
75
+
awaitsendEmail(verifyEmail);
76
+
77
+
res.json({
78
+
message: 'Verification email is resent.'
79
+
})
80
+
}
81
+
45
82
constlogin=async(req,res)=>{
46
83
const{ email, password }=req.body;
47
84
constuser=awaitUser.findOne({email});
48
85
if(!user){
49
86
throwHttpError(401,'Email or password invalid');//більш безпечний варіант писати ...або..., а не один email
50
87
}
88
+
if(!user.verify){//якщо користувач не підтвердив email (user.verify = false)
89
+
throwHttpError(401,'Email not verified')
90
+
}
51
91
constpasswordCompare=awaitbcrypt.compare(password,user.password);//порівнюємо введений пароль з тим, що є в БД (true/false)
0 commit comments