2020-01-18 20:46:06 +00:00
|
|
|
'use strict'
|
2020-05-13 01:30:11 +00:00
|
|
|
const User = use('App/Models/User');
|
|
|
|
const EmailUtils = use('App/Utils/EmailUtils');
|
2020-05-18 00:39:53 +00:00
|
|
|
const RedisUtils = use('App/Utils/RedisUtils');
|
2020-01-18 20:46:06 +00:00
|
|
|
class AuthController {
|
2020-05-10 02:15:24 +00:00
|
|
|
async registerIndex({view, auth, response}) {
|
|
|
|
if (auth.user) {
|
|
|
|
response.redirect('/');
|
|
|
|
return;
|
|
|
|
}
|
2020-02-15 17:34:42 +00:00
|
|
|
return view.render('register')
|
|
|
|
}
|
2020-01-18 20:46:06 +00:00
|
|
|
|
2020-05-10 02:15:24 +00:00
|
|
|
async loginIndex({view, auth, response}) {
|
|
|
|
if (auth.user) {
|
|
|
|
response.redirect('/');
|
|
|
|
return;
|
|
|
|
}
|
2020-02-15 17:34:42 +00:00
|
|
|
return view.render('login')
|
|
|
|
}
|
2020-01-18 20:46:06 +00:00
|
|
|
|
2020-02-15 17:34:42 +00:00
|
|
|
async register({request, response, view, session, auth}) {
|
|
|
|
const user = await User.create({
|
|
|
|
email: request.input('email'),
|
|
|
|
name: request.input('name'),
|
2020-03-17 22:16:34 +00:00
|
|
|
password: request.input('password'),
|
|
|
|
avatar:
|
|
|
|
`https://api.adorable.io/avatars/285/${request.input('email')}.png`
|
2020-02-15 17:34:42 +00:00
|
|
|
});
|
|
|
|
if (user.id == 1) {
|
|
|
|
user.is_admin = true;
|
2020-01-18 20:46:06 +00:00
|
|
|
}
|
2020-02-15 17:34:42 +00:00
|
|
|
await user.save();
|
2020-05-13 01:30:11 +00:00
|
|
|
await auth.login(user);
|
|
|
|
await EmailUtils.sendWelcomeEmail(user);
|
2020-02-15 17:34:42 +00:00
|
|
|
response.redirect('/');
|
|
|
|
}
|
2020-01-18 20:46:06 +00:00
|
|
|
|
2020-02-15 17:34:42 +00:00
|
|
|
async login({request, response, auth, session}) {
|
|
|
|
console.log('login');
|
|
|
|
const {email, password} = request.all()
|
|
|
|
try {
|
|
|
|
const token = await auth.attempt(email, password);
|
2020-03-17 22:16:34 +00:00
|
|
|
const user = auth.user;
|
2020-04-12 14:25:42 +00:00
|
|
|
// user.last_logged_in = new Date();
|
|
|
|
// await user.save();
|
2020-02-15 17:34:42 +00:00
|
|
|
console.log('logged in');
|
|
|
|
} catch (e) {
|
2020-03-17 22:16:34 +00:00
|
|
|
console.error(e);
|
2020-02-15 17:34:42 +00:00
|
|
|
session.withErrors({loginError: 'Invalid Credentials'}).flashAll()
|
|
|
|
return response.redirect('back')
|
2020-01-18 20:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-15 17:34:42 +00:00
|
|
|
response.redirect('/');
|
|
|
|
}
|
|
|
|
|
2020-05-18 00:39:53 +00:00
|
|
|
async resetPasswordRequest({request, response, session}) {
|
2020-05-13 01:30:11 +00:00
|
|
|
const email = request.body.email;
|
2020-05-18 00:39:53 +00:00
|
|
|
// check if user exists
|
|
|
|
const queryResp = (await User.query().where({email}).fetch()).rows;
|
|
|
|
if (!queryResp.length) {
|
|
|
|
session.withErrors({userNotFound: 'No User with that email'}).flashAll();
|
|
|
|
return response.redirect('back');
|
|
|
|
}
|
|
|
|
const user = queryResp[0];
|
|
|
|
//
|
|
|
|
const tokenPayload = {id: user.id, email: user.email};
|
|
|
|
const token =
|
|
|
|
await RedisUtils.ExpiredToken.generateResetPasswordToken(tokenPayload);
|
2020-05-13 01:30:11 +00:00
|
|
|
const sent =
|
2020-05-18 00:39:53 +00:00
|
|
|
await EmailUtils.sendResetPassword({name: user.name, email}, token);
|
2020-05-13 01:30:11 +00:00
|
|
|
if (sent) {
|
2020-05-18 00:39:53 +00:00
|
|
|
session.flash({notification: `Email sent to ${email} !`});
|
2020-05-13 01:30:11 +00:00
|
|
|
response.redirect('/');
|
|
|
|
return;
|
|
|
|
}
|
2020-05-18 00:39:53 +00:00
|
|
|
session.withErrors({message: 'Email provider error'}).flashAll();
|
2020-05-13 01:30:11 +00:00
|
|
|
return response.redirect('back')
|
|
|
|
}
|
2020-05-18 00:39:53 +00:00
|
|
|
async resetPasswordRequestIndex({request, auth, response, view}) {
|
2020-05-13 01:30:11 +00:00
|
|
|
if (auth.user) {
|
|
|
|
response.redirect('/');
|
|
|
|
return;
|
|
|
|
}
|
2020-05-18 00:39:53 +00:00
|
|
|
return view.render('reset-password-request');
|
|
|
|
}
|
|
|
|
async resetPasswordIndex({request, response, session, view}) {
|
|
|
|
const token = request.params.token;
|
|
|
|
const tokenPayload = await RedisUtils.ExpiredToken.getTokenPayload(token);
|
|
|
|
if (!tokenPayload) {
|
|
|
|
session.withErrors({InvalidToken: 'Token Expired or Invalid...'})
|
|
|
|
.flashAll();
|
|
|
|
response.redirect('/');
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return view.render('reset-password', {token});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async resetPassword({request, view, session, response}) {
|
|
|
|
console.log('In function...');
|
|
|
|
const {token, password, confirm} = request.all();
|
|
|
|
if (password != confirm) {
|
|
|
|
session.withErrors({confirm: 'Please make sure passwords match'})
|
|
|
|
.flashAll();
|
|
|
|
return response.redirect('back');
|
|
|
|
}
|
|
|
|
const tokenPayload = await RedisUtils.ExpiredToken.getTokenPayload(token);
|
|
|
|
if (!tokenPayload) {
|
|
|
|
session.withErrors({InvalidToken: 'Token Expired or Invalid...'})
|
|
|
|
.flashAll();
|
|
|
|
response.redirect('/');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const user = await User.find(tokenPayload.id);
|
|
|
|
user.password = password;
|
|
|
|
await user.save();
|
|
|
|
/// TODO: send an email to notify user about the change;
|
|
|
|
await RedisUtils.ExpiredToken.remove(token);
|
|
|
|
session.flash({notification: `Password changed successfully`});
|
|
|
|
return response.redirect('/');
|
2020-05-13 01:30:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 17:34:42 +00:00
|
|
|
async logout({auth, response}) {
|
|
|
|
await auth.logout();
|
|
|
|
response.redirect('/');
|
|
|
|
}
|
2020-01-18 20:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AuthController
|