1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
'use strict'
const User = use('App/Models/User');
const {
validateAll
} = use('Validator');
const atob = require('atob');
class UserController {
// Register a new user
async signup({
request,
response,
auth,
session
}) {
// Validate user input
const validation = await validateAll(request.all(), {
firstname: 'required',
email: 'required|email|unique:users,email',
password: 'required'
});
if (validation.fails()) {
return response.status(401).send({
"message": "Invalid POST arguments",
"status": 401
})
}
const data = request.only(['firstname', 'email', 'password']);
// Create user in DB
let user;
try {
user = await User.create({
email: data.email,
password: data.password,
username: data.firstname
});
} catch (e) {
return response.status(401).send({
"message": "E-Mail Address already in use",
"status": 401
})
}
// Generate new auth token
const token = await auth.generate(user)
return response.send({
"message": "Successfully created account",
"token": token.token
});
}
// Login using an existing user
async login({
request,
response,
auth
}) {
if (!request.header('Authorization')) {
return response.status(401).send({
"message": "Please provide authorization",
"status": 401
})
}
// Get auth data from auth token
const authHeader = atob(request.header('Authorization').replace('Basic ', '')).split(':');
// Check if user with email exists
let user = (await User.query().where('email', authHeader[0]).first());
if (!user || !user.email) {
return response.status(401).send({
"message": "User credentials not valid (Invalid mail)",
"code": "invalid-credentials",
"status": 401
});
}
// Try to login
let token;
try {
token = await auth.attempt(user.email, authHeader[1])
} catch (e) {
return response.status(401).send({
"message": "User credentials not valid",
"code": "invalid-credentials",
"status": 401
});
}
return response.send({
"message": "Successfully logged in",
"token": token.token
});
}
// Return information about the current user
async me({
request,
response,
auth,
session
}) {
try {
await auth.getUser()
} catch (error) {
response.send('Missing or invalid api token')
}
return response.send({
accountType: "individual",
beta: false,
donor: {},
email: auth.user.email,
emailValidated: true,
features: {},
firstname: "Franz",
id: "2acd2aa0-0869-4a91-adab-f700ac256dbe",
isPremium: true,
isSubscriptionOwner: true,
lastname: "Franz",
locale: "en-US"
});
}
}
module.exports = UserController
|