Initial commit
This commit is contained in:
41
app/controllers/admin/auth_controller.js
Normal file
41
app/controllers/admin/auth_controller.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import User from '#models/user';
|
||||
import env from '#start/env';
|
||||
export default class AuthController {
|
||||
async showLogin({ view }) {
|
||||
return view.render('admin/login');
|
||||
}
|
||||
async login({ request, response, auth, session }) {
|
||||
const { email, password } = request.only(['email', 'password']);
|
||||
if (email === env.get('USER_LOGIN') && password === env.get('USER_PASSWORD')) {
|
||||
let user = await User.findBy('email', email);
|
||||
if (!user) {
|
||||
user = await User.create({
|
||||
email,
|
||||
password,
|
||||
fullName: 'Admin',
|
||||
});
|
||||
}
|
||||
await auth.use('web').login(user);
|
||||
return response.redirect('/admin');
|
||||
}
|
||||
try {
|
||||
const user = await User.verifyCredentials(email, password);
|
||||
await auth.use('web').login(user);
|
||||
return response.redirect('/admin');
|
||||
}
|
||||
catch (error) {
|
||||
const isInvalidCredentials = error && typeof error === 'object' && 'code' in error && error.code === 'E_INVALID_CREDENTIALS';
|
||||
if (isInvalidCredentials) {
|
||||
session.flashExcept(['_csrf', '_method', 'password']);
|
||||
session.flashErrors({ error: 'Identifiants invalides. Vérifiez votre email et mot de passe.' });
|
||||
return response.redirect().back();
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
async logout({ response, auth }) {
|
||||
await auth.use('web').logout();
|
||||
return response.redirect('/admin/login');
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=auth_controller.js.map
|
||||
Reference in New Issue
Block a user