diff options
author | 2024-02-10 18:19:14 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | 7584d2d7a7110aef0331ebfa178b2295842c59fa (patch) | |
tree | 900cd71237e6231b57936fcce77ff229cd459041 /app/Controllers/Http/Dashboard/AccountController.ts | |
parent | upgrade recipes submodule (diff) | |
download | ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.gz ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.zst ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.zip |
refactor: project maintenance
- work in progress
Diffstat (limited to 'app/Controllers/Http/Dashboard/AccountController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/AccountController.ts | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/app/Controllers/Http/Dashboard/AccountController.ts b/app/Controllers/Http/Dashboard/AccountController.ts index 3c4e919..5870f19 100644 --- a/app/Controllers/Http/Dashboard/AccountController.ts +++ b/app/Controllers/Http/Dashboard/AccountController.ts | |||
@@ -1,29 +1,23 @@ | |||
1 | import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | 1 | import type { HttpContext } from '@adonisjs/core/http' |
2 | import { schema, rules, validator } from '@ioc:Adonis/Core/Validator'; | 2 | import { schema, rules, validator } from '@adonisjs/validator' |
3 | import crypto from 'node:crypto'; | 3 | import crypto from 'node:crypto' |
4 | 4 | ||
5 | export default class AccountController { | 5 | export default class AccountController { |
6 | /** | 6 | /** |
7 | * Shows the user account page | 7 | * Shows the user account page |
8 | */ | 8 | */ |
9 | public async show({ auth, view }: HttpContextContract) { | 9 | public async show({ auth, view }: HttpContext) { |
10 | return view.render('dashboard/account', { | 10 | return view.render('dashboard/account', { |
11 | username: auth.user?.username, | 11 | username: auth.user?.username, |
12 | email: auth.user?.email, | 12 | email: auth.user?.email, |
13 | lastname: auth.user?.lastname, | 13 | lastname: auth.user?.lastname, |
14 | }); | 14 | }) |
15 | } | 15 | } |
16 | 16 | ||
17 | /** | 17 | /** |
18 | * Stores user account data | 18 | * Stores user account data |
19 | */ | 19 | */ |
20 | public async store({ | 20 | public async store({ auth, request, response, session, view }: HttpContext) { |
21 | auth, | ||
22 | request, | ||
23 | response, | ||
24 | session, | ||
25 | view, | ||
26 | }: HttpContextContract) { | ||
27 | try { | 21 | try { |
28 | await validator.validate({ | 22 | await validator.validate({ |
29 | schema: schema.create({ | 23 | schema: schema.create({ |
@@ -48,26 +42,26 @@ export default class AccountController { | |||
48 | lastname: schema.string([rules.required()]), | 42 | lastname: schema.string([rules.required()]), |
49 | }), | 43 | }), |
50 | data: request.only(['username', 'email', 'lastname']), | 44 | data: request.only(['username', 'email', 'lastname']), |
51 | }); | 45 | }) |
52 | } catch (error) { | 46 | } catch (error) { |
53 | session.flash(error.messages); | 47 | session.flash(error.messages) |
54 | return response.redirect('/user/account'); | 48 | return response.redirect('/user/account') |
55 | } | 49 | } |
56 | 50 | ||
57 | // Update user account | 51 | // Update user account |
58 | const { user } = auth; | 52 | const { user } = auth |
59 | if (user) { | 53 | if (user) { |
60 | user.username = request.input('username'); | 54 | user.username = request.input('username') |
61 | user.lastname = request.input('lastname'); | 55 | user.lastname = request.input('lastname') |
62 | user.email = request.input('email'); | 56 | user.email = request.input('email') |
63 | if (request.input('password')) { | 57 | if (request.input('password')) { |
64 | const hashedPassword = crypto | 58 | const hashedPassword = crypto |
65 | .createHash('sha256') | 59 | .createHash('sha256') |
66 | .update(request.input('password')) | 60 | .update(request.input('password')) |
67 | .digest('base64'); | 61 | .digest('base64') |
68 | user.password = hashedPassword; | 62 | user.password = hashedPassword |
69 | } | 63 | } |
70 | await user.save(); | 64 | await user.save() |
71 | } | 65 | } |
72 | 66 | ||
73 | return view.render('dashboard/account', { | 67 | return view.render('dashboard/account', { |
@@ -75,6 +69,6 @@ export default class AccountController { | |||
75 | lastname: user?.lastname, | 69 | lastname: user?.lastname, |
76 | email: user?.email, | 70 | email: user?.email, |
77 | success: user !== undefined, | 71 | success: user !== undefined, |
78 | }); | 72 | }) |
79 | } | 73 | } |
80 | } | 74 | } |