diff options
author | vantezzen <properly@protonmail.com> | 2019-09-04 09:59:25 +0200 |
---|---|---|
committer | vantezzen <properly@protonmail.com> | 2019-09-04 09:59:25 +0200 |
commit | ea03e3766efffeb5e6b9bb90f566e64bf44640f3 (patch) | |
tree | 36f52de9554a08456949cc4a7fa5b40adc84ca94 /app/Controllers/Http | |
parent | Better response when recipe creation is disabled (diff) | |
download | ferdium-server-ea03e3766efffeb5e6b9bb90f566e64bf44640f3.tar.gz ferdium-server-ea03e3766efffeb5e6b9bb90f566e64bf44640f3.tar.zst ferdium-server-ea03e3766efffeb5e6b9bb90f566e64bf44640f3.zip |
Add user dashboard
Diffstat (limited to 'app/Controllers/Http')
-rw-r--r-- | app/Controllers/Http/DashboardController.js | 155 | ||||
-rw-r--r-- | app/Controllers/Http/UserController.js | 11 |
2 files changed, 163 insertions, 3 deletions
diff --git a/app/Controllers/Http/DashboardController.js b/app/Controllers/Http/DashboardController.js new file mode 100644 index 0000000..aa8127f --- /dev/null +++ b/app/Controllers/Http/DashboardController.js | |||
@@ -0,0 +1,155 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const { | ||
4 | validateAll | ||
5 | } = use('Validator'); | ||
6 | |||
7 | const crypto = require('crypto'); | ||
8 | |||
9 | class DashboardController { | ||
10 | async login({ | ||
11 | request, | ||
12 | response, | ||
13 | auth, | ||
14 | session | ||
15 | }) { | ||
16 | const validation = await validateAll(request.all(), { | ||
17 | mail: 'required|email', | ||
18 | password: 'required', | ||
19 | }); | ||
20 | if (validation.fails()) { | ||
21 | session.withErrors({ | ||
22 | type: 'danger', | ||
23 | message: 'Invalid mail or password' | ||
24 | }).flashExcept(['password']); | ||
25 | return response.redirect('back'); | ||
26 | } | ||
27 | |||
28 | let { | ||
29 | mail, | ||
30 | password | ||
31 | } = request.all() | ||
32 | |||
33 | const hashedPassword = crypto.createHash('sha256').update(password).digest('base64'); | ||
34 | |||
35 | try { | ||
36 | await auth.authenticator('session').attempt(mail, hashedPassword) | ||
37 | } catch (error) { | ||
38 | session.flash({ | ||
39 | type: 'danger', | ||
40 | message: 'Invalid mail or password' | ||
41 | }) | ||
42 | return response.redirect('back'); | ||
43 | } | ||
44 | return response.redirect('/user/account'); | ||
45 | } | ||
46 | |||
47 | async account({ | ||
48 | auth, | ||
49 | view | ||
50 | }) { | ||
51 | try { | ||
52 | await auth.check() | ||
53 | } catch (error) { | ||
54 | return response.redirect('/user/login'); | ||
55 | } | ||
56 | |||
57 | return view.render('dashboard.account', { | ||
58 | username: auth.user.username, | ||
59 | email: auth.user.email | ||
60 | }); | ||
61 | } | ||
62 | |||
63 | async edit({ | ||
64 | auth, | ||
65 | request, | ||
66 | session, | ||
67 | view, | ||
68 | response | ||
69 | }) { | ||
70 | let validation = await validateAll(request.all(), { | ||
71 | username: 'required', | ||
72 | email: 'required' | ||
73 | }); | ||
74 | if (validation.fails()) { | ||
75 | session.withErrors(validation.messages()).flashExcept(['password']); | ||
76 | return response.redirect('back'); | ||
77 | } | ||
78 | |||
79 | // Check new username | ||
80 | if (request.input('username') !== auth.user.username) { | ||
81 | validation = await validateAll(request.all(), { | ||
82 | username: 'required|unique:users,username', | ||
83 | email: 'required' | ||
84 | }); | ||
85 | if (validation.fails()) { | ||
86 | session.withErrors(validation.messages()).flashExcept(['password']); | ||
87 | return response.redirect('back'); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | // Check new email | ||
92 | if (request.input('email') !== auth.user.email) { | ||
93 | validation = await validateAll(request.all(), { | ||
94 | username: 'required', | ||
95 | email: 'required|email|unique:users,email' | ||
96 | }); | ||
97 | if (validation.fails()) { | ||
98 | session.withErrors(validation.messages()).flashExcept(['password']); | ||
99 | return response.redirect('back'); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | // Update user account | ||
104 | auth.user.username = request.input('username'); | ||
105 | auth.user.email = request.input('email'); | ||
106 | if (!!request.input('password')) { | ||
107 | const hashedPassword = crypto.createHash('sha256').update(request.input('password')).digest('base64'); | ||
108 | auth.user.password = hashedPassword; | ||
109 | } | ||
110 | auth.user.save(); | ||
111 | |||
112 | return view.render('dashboard.account', { | ||
113 | username: auth.user.username, | ||
114 | email: auth.user.email, | ||
115 | success: true | ||
116 | }); | ||
117 | } | ||
118 | |||
119 | async data({ | ||
120 | auth, | ||
121 | view | ||
122 | }) { | ||
123 | const general = auth.user; | ||
124 | const services = (await auth.user.services().fetch()).toJSON(); | ||
125 | const workspaces = (await auth.user.workspaces().fetch()).toJSON(); | ||
126 | |||
127 | return view.render('dashboard.data', { | ||
128 | username: general.username, | ||
129 | mail: general.email, | ||
130 | created: general.created_at, | ||
131 | updated: general.updated_at, | ||
132 | services, | ||
133 | workspaces, | ||
134 | }); | ||
135 | } | ||
136 | |||
137 | logout({ | ||
138 | auth, | ||
139 | response | ||
140 | }) { | ||
141 | auth.authenticator('session').logout(); | ||
142 | return response.redirect('/user/login'); | ||
143 | } | ||
144 | |||
145 | delete({ | ||
146 | auth, | ||
147 | response | ||
148 | }) { | ||
149 | auth.user.delete(); | ||
150 | auth.authenticator('session').logout(); | ||
151 | return response.redirect('/user/login'); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | module.exports = DashboardController | ||
diff --git a/app/Controllers/Http/UserController.js b/app/Controllers/Http/UserController.js index 2a75f6e..ced27bb 100644 --- a/app/Controllers/Http/UserController.js +++ b/app/Controllers/Http/UserController.js | |||
@@ -192,9 +192,9 @@ class UserController { | |||
192 | 192 | ||
193 | if(Env.get('CONNECT_WITH_FRANZ') == 'false') { | 193 | if(Env.get('CONNECT_WITH_FRANZ') == 'false') { |
194 | await User.create({ | 194 | await User.create({ |
195 | email: userInf.email, | 195 | email, |
196 | password: hashedPassword, | 196 | password: hashedPassword, |
197 | username: userInf.firstname | 197 | username: 'Franz' |
198 | }); | 198 | }); |
199 | 199 | ||
200 | return response.send('Your account has been created but due to this server\'s configuration, we could not import your Franz account data.\n\nIf you are the server owner, please set CONNECT_WITH_FRANZ to true to enable account imports.') | 200 | return response.send('Your account has been created but due to this server\'s configuration, we could not import your Franz account data.\n\nIf you are the server owner, please set CONNECT_WITH_FRANZ to true to enable account imports.') |
@@ -231,13 +231,18 @@ class UserController { | |||
231 | } | 231 | } |
232 | 232 | ||
233 | // Get user information | 233 | // Get user information |
234 | let userInf; | 234 | let userInf = false; |
235 | try { | 235 | try { |
236 | userInf = await franzRequest('me', 'GET', token) | 236 | userInf = await franzRequest('me', 'GET', token) |
237 | console.log('A', userInf) | ||
237 | } catch (e) { | 238 | } catch (e) { |
238 | const errorMessage = 'Could not get your user info from Franz. Please check your credentials or try again later.\nError: ' + e; | 239 | const errorMessage = 'Could not get your user info from Franz. Please check your credentials or try again later.\nError: ' + e; |
239 | return response.status(401).send(errorMessage) | 240 | return response.status(401).send(errorMessage) |
240 | } | 241 | } |
242 | if (!userInf) { | ||
243 | const errorMessage = 'Could not get your user info from Franz. Please check your credentials or try again later.\nError: ' + e; | ||
244 | return response.status(401).send(errorMessage) | ||
245 | } | ||
241 | 246 | ||
242 | // Create user in DB | 247 | // Create user in DB |
243 | let user; | 248 | let user; |