Skip to content

Instantly share code, notes, and snippets.

@harryWonder
Created June 10, 2021 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryWonder/bd66868c021ccee11c9f3a8607bcc345 to your computer and use it in GitHub Desktop.
Save harryWonder/bd66868c021ccee11c9f3a8607bcc345 to your computer and use it in GitHub Desktop.
LD NodeJS & Nginx
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();
const Middleware = require('../middleware/JwtMiddleware');
const Newsletter = require('../controllers/app/Newsletter');
const Welcome = require('../controllers/Welcome');
const Signup = require('../controllers/auth/Signup');
const Login = require('../controllers/auth/Login');
/**
* All the API endpoint or routes to mars is loaded here. You can load in routes from anywhere but it's best that you load them in from the controllers.
*
* @author Ilori Stephen <stephenilori458@gmail.com>
* @param {Null}
* @returns {Function} Express
* @name Routes
* @alias ApplicationRoutes
*
*/
module.exports = (App) => {
/* Are we still on earth? */
App.get('/api/v1/welcome', Welcome.whatYearIsIt);
App.post('/api/v1/newsletter', Newsletter.registerEmail);
/* Auth Routes */
App.post('/api/v1/login', Login.login);
App.post('/api/v1/register', Signup.signup);
/* In need of some inspiration? */
App.all('/api/v1/inspire', (req, res) => {
res.status(200).send('You can build anything you set your mind to!');
res.end();
return;
});
}
const Jwt = require('jsonwebtoken');
const CryptoJs = require('crypto-js');
const Controller = require('../Controller');
const UserModel = require('../../models/Users');
/* Validators */
const Validators = require('../../validators/Auth')
class Signup extends Controller {
constructor() { super(); }
async signup(req, res) {
try {
const Body = req.body;
const Validator = await Validators.register(Body, UserModel);
if (Validator.status) {
return super.response(res,
400,
'There are some errors in your request. Please, try again.', {},
Validator.errors);
}
/* Create The User */
let newUser = new UserModel({
firstName: Body.firstName,
lastName: Body.lastName,
email: Body.email,
status: 1,
password: CryptoJs.AES.encrypt(Body.password, super.fetchAppConfigs().appKey).toString(),
});
const User = await newUser.save();
const Token = Jwt.sign({
data: {
access: 'user-level',
phone: User.phone,
email: User.email
}
}, super.fetchAppConfigs().jwtSecretUser, { expiresIn: '3days' });
return super.response(res, 200, 'Registration 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 Signup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment