Created
June 12, 2021 21:31
-
-
Save harryWonder/ef8d56dc68e0429ed400c0552a859a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Jwt = require('jsonwebtoken'); | |
const Controller = require('../Controller'); | |
const UserModel = require('../../models/Users'); | |
/* Validators */ | |
const Validators = require('../../validators/Auth') | |
class Login extends Controller { | |
constructor() { super(); } | |
async login(req, res) { | |
try { | |
const Body = req.body; | |
const Validator = await Validators.login(Body, UserModel); | |
if (Validator.status) { | |
return super.response(res, | |
400, | |
'There are some errors in your request. Please, try again.', {}, | |
Validator.errors); | |
} | |
/* Login The User */ | |
const User = await UserModel.findOne({ email: Body.email }).lean(); | |
const Token = Jwt.sign({ | |
data: { | |
access: 'user-level', | |
phone: User.phone, | |
email: User.email | |
} | |
}, super.fetchAppConfigs().jwtSecretUser, { expiresIn: '3days' }); | |
return super.response(res, 200, 'Login Successfull', { user: User, token: Token }); | |
} catch (e) { | |
return super.response(res, 500, 'An unexpected error occurred. Please, try again.', {}, { server: 'Operation Failed' }); | |
} | |
} | |
} | |
module.exports = new Login(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment