aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/auth
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/routes/auth')
-rw-r--r--src/api/routes/auth/loginPOST.js9
-rw-r--r--src/api/routes/auth/registerPOST.js9
2 files changed, 18 insertions, 0 deletions
diff --git a/src/api/routes/auth/loginPOST.js b/src/api/routes/auth/loginPOST.js
index 7e85812..eaf09e8 100644
--- a/src/api/routes/auth/loginPOST.js
+++ b/src/api/routes/auth/loginPOST.js
@@ -15,12 +15,21 @@ class loginPOST extends Route {
const { username, password } = req.body;
if (!username || !password) return res.status(401).json({ message: 'Invalid body provided' });
+ /*
+ Checks if the user exists
+ */
const user = await db.table('users').where('username', username).first();
if (!user) return res.status(401).json({ message: 'Invalid authorization' });
+ /*
+ Checks if the password is right
+ */
const comparePassword = await bcrypt.compare(password, user.password);
if (!comparePassword) return res.status(401).json({ message: 'Invalid authorization.' });
+ /*
+ Create the jwt with some data
+ */
const jwt = JWT.sign({
iss: 'lolisafe',
sub: user.id,
diff --git a/src/api/routes/auth/registerPOST.js b/src/api/routes/auth/registerPOST.js
index dad45fd..d3532f4 100644
--- a/src/api/routes/auth/registerPOST.js
+++ b/src/api/routes/auth/registerPOST.js
@@ -24,9 +24,15 @@ class registerPOST extends Route {
return res.status(400).json({ message: 'Password must have 6-64 characters' });
}
+ /*
+ Make sure the username doesn't exist yet
+ */
const user = await db.table('users').where('username', username).first();
if (user) return res.status(401).json({ message: 'Username already exists' });
+ /*
+ Hash the supplied password
+ */
let hash;
try {
hash = await bcrypt.hash(password, 10);
@@ -36,6 +42,9 @@ class registerPOST extends Route {
return res.status(401).json({ message: 'There was a problem processing your account' });
}
+ /*
+ Create the user
+ */
const now = moment.utc().toDate();
await db.table('users').insert({
username,