aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/UserController.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/Controllers/Http/UserController.js')
-rw-r--r--app/Controllers/Http/UserController.js94
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
3const User = use('App/Models/User');
4const atob = require('atob');
5
6class 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
94module.exports = UserController