Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- export const authController = {
- registerPage: (req, res) => {
- this.res.render('pages/register', {
- 'messageSuccess' : this.req.flash('successMessage'),
- 'messageError' : this.req.flash('errorMessage'),
- 'username' : this.req.flash('username'),
- 'email' : this.req.flash('email'),
- });
- },
- loginPage: (req, res) => {
- res.render('pages/login', {
- 'messageSuccess' : req.flash('successMessage'),
- 'messageError' : req.flash('errorMessage'),
- 'identifier' : req.flash('identifier')
- });
- },
- resetPasswordPage: (req, res) => {
- res.render('pages/resetPassword');
- },
- register: (req, res) => {
- let hashedPassword = bcrypt.hashSync(req.body.password, 10);
- let newUser = new User({
- username: req.body.username,
- email: req.body.email,
- password: hashedPassword,
- role: 2,
- active: true
- });
- newUser.save((err) => {
- if (err) {
- req.flash('errorMessage', err.message);
- req.flash('username', req.body.username);
- req.flash('email', req.body.email);
- this.registerPage(req, res);
- } else {
- req.flash('successMessage', 'User Successfuly Registered');
- this.loginPage(req, res);
- }
- });
- },
- login: (req, res) => {
- User.findOne({
- $or: [
- { email: req.body.identifier },
- { username: req.body.identifier }
- ]
- }).exec((err, user) => {
- if (err) {
- req.flash('errorMessage', err.message);
- this.loginPage(req, res);
- }
- if (!user) {
- req.flash('errorMessage', 'User with the provided credentials does not exist');
- this.loginPage(req, res);
- }
- if (!bcrypt.compareSync(req.body.password, user.password)) {
- req.flash('errorMessage', 'Incorrect Credentials');
- this.loginPage(req, res);
- }
- let token = jwt.sign({id: user._id}, jwtConfig.secret, {expiresIn: jwtConfig.expires});
- req.flash('successMessage', 'Login Successful');
- res.cookie('auth', token);
- this.loginPage(req, res);
- });
- },
- logout: (req, res) => {
- req.logout();
- res.redirect('/');
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement