-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetPassword.js
37 lines (32 loc) · 1000 Bytes
/
setPassword.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
import client from '../../src/db';
export default async function handler(req, res) {
try {
if (req.method !== 'POST') {
return res.status(405).json({error: 'Method Not Allowed'});
}
const {email, password, token} = req.body;
await client.connect();
const usersCollection = client.db('ivrStudio').collection('users');
const existingUser = await usersCollection.findOne({
email,
isEmailVerified: true,
otp: parseInt(token),
});
if (existingUser) {
existingUser.password = password;
await usersCollection.updateOne(
{_id: existingUser._id},
{$set: {password}, $unset: {otp: 1}}
);
return res.status(200).json({message: 'Password updated successfully.'});
} else {
return res
.status(404)
.json({message: 'User not found or email not verified.'});
}
} catch (err) {
return res.status(401).json({message: err.message});
} finally {
await client.close();
}
}