diff options
author | vantezzen <properly@protonmail.com> | 2019-08-22 11:12:36 +0200 |
---|---|---|
committer | vantezzen <properly@protonmail.com> | 2019-08-22 11:12:36 +0200 |
commit | b018adf240679ec59a7344e30be39400f1ecd8af (patch) | |
tree | c076635761667dad302716b498088f1047281e46 /app/Controllers/Http/UserController.js | |
download | ferdium-server-b018adf240679ec59a7344e30be39400f1ecd8af.tar.gz ferdium-server-b018adf240679ec59a7344e30be39400f1ecd8af.tar.zst ferdium-server-b018adf240679ec59a7344e30be39400f1ecd8af.zip |
Initial commit
Diffstat (limited to 'app/Controllers/Http/UserController.js')
-rw-r--r-- | app/Controllers/Http/UserController.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/app/Controllers/Http/UserController.js b/app/Controllers/Http/UserController.js new file mode 100644 index 0000000..88f7ecd --- /dev/null +++ b/app/Controllers/Http/UserController.js | |||
@@ -0,0 +1,94 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const User = use('App/Models/User'); | ||
4 | const atob = require('atob'); | ||
5 | |||
6 | class UserController { | ||
7 | |||
8 | // Register a new user | ||
9 | async signup({ | ||
10 | request, | ||
11 | response, | ||
12 | auth, | ||
13 | session | ||
14 | }) { | ||
15 | const data = request.only(['firstname', 'email', 'password']); | ||
16 | const user = await User.create({ | ||
17 | email: data.email, | ||
18 | password: data.password, | ||
19 | username: data.firstname | ||
20 | }); | ||
21 | const token = await auth.generate(user) | ||
22 | |||
23 | return response.send({ | ||
24 | "message": "Successfully created account", | ||
25 | "token": token.token | ||
26 | }); | ||
27 | } | ||
28 | |||
29 | // Login using an existing user | ||
30 | async login({ | ||
31 | request, | ||
32 | response, | ||
33 | auth | ||
34 | }) { | ||
35 | const authHeader = atob(request.header('Authorization')).split(':'); | ||
36 | |||
37 | let user = (await User.query().where('email', authHeader[0]).limit(1).fetch()).toJSON(); | ||
38 | if (!user[0] || !user[0].email) { | ||
39 | return response.status(401).send({ | ||
40 | "message": "User credentials not valid", | ||
41 | "code": "invalid-credentials", | ||
42 | "status": 401 | ||
43 | }); | ||
44 | } | ||
45 | |||
46 | let token; | ||
47 | try { | ||
48 | // TODO: Login is currently not working as the password is incorrect | ||
49 | token = await auth.attempt(user[0].id, authHeader[1]) | ||
50 | } catch (e) { | ||
51 | return response.status(401).send({ | ||
52 | "message": "User credentials not valid", | ||
53 | "code": "invalid-credentials", | ||
54 | "status": 401 | ||
55 | }); | ||
56 | } | ||
57 | |||
58 | return response.send({ | ||
59 | "message": "Successfully logged in", | ||
60 | "token": token.token | ||
61 | }); | ||
62 | } | ||
63 | |||
64 | // Return information about the current user | ||
65 | async me({ | ||
66 | request, | ||
67 | response, | ||
68 | auth, | ||
69 | session | ||
70 | }) { | ||
71 | try { | ||
72 | await auth.getUser() | ||
73 | } catch (error) { | ||
74 | response.send('Missing or invalid api token') | ||
75 | } | ||
76 | |||
77 | return response.send({ | ||
78 | accountType: "individual", | ||
79 | beta: false, | ||
80 | donor: {}, | ||
81 | email: auth.user.email, | ||
82 | emailValidated: true, | ||
83 | features: {}, | ||
84 | firstname: "Franz", | ||
85 | id: "2acd2aa0-0869-4a91-adab-f700ac256dbe", | ||
86 | isPremium: true, | ||
87 | isSubscriptionOwner: true, | ||
88 | lastname: "Franz", | ||
89 | locale: "en-US" | ||
90 | }); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | module.exports = UserController | ||