|
1 | | -const { genrateToken } = require('../util'); |
2 | | -const user = require('../models/user'); |
| 1 | +import util, {logger} from "#util"; |
| 2 | +import { authenticateUser, userExists, updatePassword } from "#services/user"; |
3 | 3 |
|
4 | | -exports.login = async function(req, res, next) { |
5 | | - const {id, password} = req.body; |
6 | | - let userValidated = await user.validateUser(id, password); |
7 | | - if(userValidated){ |
8 | | - userDetails = { |
9 | | - "uid": userValidated.uid, |
10 | | - "name": userValidated.name, |
11 | | - "emailId":userValidated.emailId, |
12 | | - "type": userValidated.userType, |
13 | | - } |
14 | | - let token = genrateToken(userDetails); |
15 | | - userDetails["token"] = token; |
16 | | - res.send({res:"welcome", user:userDetails}) |
17 | | - } |
18 | | - else{ |
19 | | - res.status(403) |
20 | | - res.send({err:"incorrect ID password"}); |
21 | | - } |
| 4 | +const otpStore = {}; |
| 5 | + |
| 6 | +async function login(req, res) { |
| 7 | + const { id, password } = req.body; |
| 8 | + try { |
| 9 | + const userValidated = await authenticateUser(id, password); |
| 10 | + const userDetails = { |
| 11 | + uid: userValidated.uid, |
| 12 | + name: userValidated.name, |
| 13 | + emailId: userValidated.emailId, |
| 14 | + type: userValidated.userType, |
| 15 | + }; |
| 16 | + const token = util.generateToken(userDetails, req.ip); |
| 17 | + userDetails.token = token; |
| 18 | + res.json({ res: "welcome", user: userDetails }); |
| 19 | + } catch (error) { |
| 20 | + logger.error("Error while login", error) |
| 21 | + if (error.name === "UserDoesNotExist") { |
| 22 | + res.status(403); |
| 23 | + res.json({ err: "Incorrect ID password" }); |
| 24 | + } else { |
| 25 | + res.status(500); |
| 26 | + res.json({ err: "Something is wrong on our side. Try again" }); |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function validateUser(req, res) { |
| 32 | + res.json({ res: req.user, msg: "user validated", err: null }); |
| 33 | +} |
| 34 | + |
| 35 | +async function sendOTP(req, res) { |
| 36 | + const { uid, emailId } = req.body; |
| 37 | + if (await userExists(uid, emailId)) { |
| 38 | + const otp = Math.floor(1000 + Math.random() * 9000); |
| 39 | + otpStore[uid] = otp; |
| 40 | + util.sendOTP(emailId, otp); |
| 41 | + res.json({ res: "otp sent to emailID" }); |
| 42 | + } else { |
| 43 | + res.json({ err: "incorrect UID or emailId" }); |
| 44 | + } |
22 | 45 | } |
23 | 46 |
|
24 | | -exports.validateUser = function(req, res, next) { |
25 | | - res.json({res: req.user, msg: "user validated", err:null}) |
| 47 | +async function resetPassword(req, res) { |
| 48 | + const { uid, otp, password } = req.body; |
| 49 | + if (otpStore[uid] === otp) { |
| 50 | + try { |
| 51 | + await updatePassword(uid, password); |
| 52 | + res.json({ res: "successfully updated password" }); |
| 53 | + } catch (error) { |
| 54 | + logger.log("Error while updating", error) |
| 55 | + res.status(500); |
| 56 | + if (error.name === "UpdateError") res.json({ err: "Something went wrong while updating password" }); |
| 57 | + else res.json({ err: "something went wrong" }); |
| 58 | + } |
| 59 | + } else { |
| 60 | + res.json({ err: "incorrect otp" }); |
| 61 | + } |
26 | 62 | } |
27 | 63 |
|
| 64 | +export default { |
| 65 | + validateUser, sendOTP, resetPassword, login, |
| 66 | +}; |
0 commit comments