- Added login method for users

This commit is contained in:
Alexis
2020-07-04 23:07:52 +02:00
parent 5d1b44cf6d
commit ba89dc8c5e
2 changed files with 80 additions and 18 deletions

View File

@@ -52,13 +52,13 @@ class UserRepository {
}) })
} }
getOneByEmail(mail) { getOneByEmail(mail, full) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
model.forge() model.forge()
.where({ 'mail': mail}) .where({ 'mail': mail })
.fetch() .fetch()
.then(v => { .then(v => {
resolve(v.toJSON({ omitPivot: true })) resolve(v.toJSON({ omitPivot: true, visibility: !full }))
}) })
.catch(err => { .catch(err => {
reject(new HttpError(500, "Couldn't get user")) reject(new HttpError(500, "Couldn't get user"))
@@ -66,18 +66,6 @@ class UserRepository {
}) })
} }
checkIfEmailAvailable(mail) {
return new Promise((resolve, reject) => {
this.getOneByEmail(mail)
.then(() => {
reject(false)
})
.catch(() => {
resolve(true)
})
})
}
addOne(u) { addOne(u) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
@@ -104,11 +92,21 @@ class UserRepository {
transacting: t transacting: t
}) })
.catch(err => { .catch(err => {
console.log(err) throw err
}) })
}) })
.then(v => { .then(() => {
resolve(this.getOneByUUID(uuid)) return this.getOneByUUID(uuid)
})
.then(newUser => {
resolve({
"message": "Account successfully created !",
"data": {
"uuid": newUser.uuid,
"name": newUser.name,
"mail": newUser.mail,
},
})
}) })
.catch(err => { .catch(err => {
throw err throw err
@@ -120,6 +118,46 @@ class UserRepository {
} }
}) })
} }
// Log user with an email address and a password
logUser(mail, password) {
return new Promise((resolve, reject) => {
this.getOneByEmail(mail, true)
.then(async fetchedUser => {
let match = await bcrypt.compare(password, fetchedUser.password)
if (match) {
resolve({
"message": "User successfully logged in !",
"logged?": true,
"data": {
"uuid": fetchedUser.uuid
},
})
} else {
resolve({
"message": "The email and passwords don't match",
"logged?": false,
})
}
})
.catch(err => {
reject(err)
})
})
}
// Check if one user already has that email
checkIfEmailAvailable(mail) {
return new Promise((resolve, reject) => {
this.getOneByEmail(mail, false)
.then(() => {
reject(false)
})
.catch(() => {
resolve(true)
})
})
}
} }
module.exports = UserRepository module.exports = UserRepository

View File

@@ -63,6 +63,30 @@ router.get('/:uuid/', async (req, res) => {
}) })
// LOG A USER ------------------
const logUser = (mail, password) => {
return Users.logUser(mail, password)
.catch(err => {
console.log(err)
throw err
})
}
router.post('/login', async (req, res) => {
logUser(req.body.mail, req.body.password)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.end(JSON.stringify(v))
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
)
})
})
// CREATE ONE ------------------ // CREATE ONE ------------------
const addUser = (u) => { const addUser = (u) => {
return Users.addOne(u) return Users.addOne(u)