diff options
author | 2024-02-10 18:37:40 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | e1c47572a6235fd8fd20af888ac3a11c7ae1369d (patch) | |
tree | 2dccff36a441916d7014037cef3f7ce84a790cad /app/Controllers/Http/Dashboard/LoginController.ts | |
parent | refactor: project maintenance (diff) | |
download | ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.gz ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.zst ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.zip |
updates
Diffstat (limited to 'app/Controllers/Http/Dashboard/LoginController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/LoginController.ts | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/app/Controllers/Http/Dashboard/LoginController.ts b/app/Controllers/Http/Dashboard/LoginController.ts index 5a54448..3367a2f 100644 --- a/app/Controllers/Http/Dashboard/LoginController.ts +++ b/app/Controllers/Http/Dashboard/LoginController.ts | |||
@@ -1,15 +1,15 @@ | |||
1 | import type { HttpContext } from '@adonisjs/core/http' | 1 | import type { HttpContext } from '@adonisjs/core/http'; |
2 | import { schema, rules, validator } from '@adonisjs/validator' | 2 | import { schema, rules, validator } from '@adonisjs/validator'; |
3 | import User from '#app/Models/User' | 3 | import User from '#app/Models/User'; |
4 | import crypto from 'node:crypto' | 4 | import crypto from 'node:crypto'; |
5 | import { handleVerifyAndReHash } from '../../../../helpers/PasswordHash.js' | 5 | import { handleVerifyAndReHash } from '../../../../helpers/PasswordHash.js'; |
6 | 6 | ||
7 | export default class LoginController { | 7 | export default class LoginController { |
8 | /** | 8 | /** |
9 | * Display the login form | 9 | * Display the login form |
10 | */ | 10 | */ |
11 | public async show({ view }: HttpContext) { | 11 | public async show({ view }: HttpContext) { |
12 | return view.render('dashboard/login') | 12 | return view.render('dashboard/login'); |
13 | } | 13 | } |
14 | 14 | ||
15 | /** | 15 | /** |
@@ -23,51 +23,54 @@ export default class LoginController { | |||
23 | password: schema.string([rules.required()]), | 23 | password: schema.string([rules.required()]), |
24 | }), | 24 | }), |
25 | data: request.only(['mail', 'password']), | 25 | data: request.only(['mail', 'password']), |
26 | }) | 26 | }); |
27 | } catch { | 27 | } catch { |
28 | session.flash({ | 28 | session.flash({ |
29 | type: 'danger', | 29 | type: 'danger', |
30 | message: 'Invalid mail or password', | 30 | message: 'Invalid mail or password', |
31 | }) | 31 | }); |
32 | session.flashExcept(['password']) | 32 | session.flashExcept(['password']); |
33 | 33 | ||
34 | return response.redirect('/user/login') | 34 | return response.redirect('/user/login'); |
35 | } | 35 | } |
36 | 36 | ||
37 | try { | 37 | try { |
38 | const { mail, password } = request.all() | 38 | const { mail, password } = request.all(); |
39 | 39 | ||
40 | // Check if user with email exists | 40 | // Check if user with email exists |
41 | const user = await User.query().where('email', mail).first() | 41 | const user = await User.query().where('email', mail).first(); |
42 | if (!user?.email) { | 42 | if (!user?.email) { |
43 | throw new Error('User credentials not valid (Invalid email)') | 43 | throw new Error('User credentials not valid (Invalid email)'); |
44 | } | 44 | } |
45 | 45 | ||
46 | const hashedPassword = crypto.createHash('sha256').update(password).digest('base64') | 46 | const hashedPassword = crypto |
47 | .createHash('sha256') | ||
48 | .update(password) | ||
49 | .digest('base64'); | ||
47 | 50 | ||
48 | // Verify password | 51 | // Verify password |
49 | let isMatchedPassword = false | 52 | let isMatchedPassword = false; |
50 | try { | 53 | try { |
51 | isMatchedPassword = await handleVerifyAndReHash(user, hashedPassword) | 54 | isMatchedPassword = await handleVerifyAndReHash(user, hashedPassword); |
52 | } catch (error) { | 55 | } catch (error) { |
53 | return response.internalServerError({ message: error.message }) | 56 | return response.internalServerError({ message: error.message }); |
54 | } | 57 | } |
55 | 58 | ||
56 | if (!isMatchedPassword) { | 59 | if (!isMatchedPassword) { |
57 | throw new Error('User credentials not valid (Invalid password)') | 60 | throw new Error('User credentials not valid (Invalid password)'); |
58 | } | 61 | } |
59 | 62 | ||
60 | await auth.use('web').login(user) | 63 | await auth.use('web').login(user); |
61 | 64 | ||
62 | return response.redirect('/user/account') | 65 | return response.redirect('/user/account'); |
63 | } catch { | 66 | } catch { |
64 | session.flash({ | 67 | session.flash({ |
65 | type: 'danger', | 68 | type: 'danger', |
66 | message: 'Invalid mail or password', | 69 | message: 'Invalid mail or password', |
67 | }) | 70 | }); |
68 | session.flashExcept(['password']) | 71 | session.flashExcept(['password']); |
69 | 72 | ||
70 | return response.redirect('/user/login') | 73 | return response.redirect('/user/login'); |
71 | } | 74 | } |
72 | } | 75 | } |
73 | } | 76 | } |