2020-01-18 20:46:06 +00:00
|
|
|
'use strict'
|
|
|
|
const User = use('App/Models/User')
|
|
|
|
class AuthController {
|
2020-02-15 17:34:42 +00:00
|
|
|
async registerIndex({view}) {
|
|
|
|
return view.render('register')
|
|
|
|
}
|
2020-01-18 20:46:06 +00:00
|
|
|
|
2020-02-15 17:34:42 +00:00
|
|
|
async loginIndex({view}) {
|
|
|
|
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();
|
|
|
|
await auth.login(user)
|
|
|
|
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()
|
2020-03-17 22:16:34 +00:00
|
|
|
console.log({email, password})
|
2020-02-15 17:34:42 +00:00
|
|
|
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('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
async logout({auth, response}) {
|
|
|
|
await auth.logout();
|
|
|
|
response.redirect('/');
|
|
|
|
}
|
2020-01-18 20:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AuthController
|