- Added login method for users
This commit is contained in:
@@ -52,13 +52,13 @@ class UserRepository {
|
||||
})
|
||||
}
|
||||
|
||||
getOneByEmail(mail) {
|
||||
getOneByEmail(mail, full) {
|
||||
return new Promise((resolve, reject) => {
|
||||
model.forge()
|
||||
.where({ 'mail': mail})
|
||||
.where({ 'mail': mail })
|
||||
.fetch()
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }))
|
||||
resolve(v.toJSON({ omitPivot: true, visibility: !full }))
|
||||
})
|
||||
.catch(err => {
|
||||
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) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// Checks if body exists and if the model fits, and throws errors if it doesn't
|
||||
@@ -104,11 +92,21 @@ class UserRepository {
|
||||
transacting: t
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
throw err
|
||||
})
|
||||
})
|
||||
.then(v => {
|
||||
resolve(this.getOneByUUID(uuid))
|
||||
.then(() => {
|
||||
return this.getOneByUUID(uuid)
|
||||
})
|
||||
.then(newUser => {
|
||||
resolve({
|
||||
"message": "Account successfully created !",
|
||||
"data": {
|
||||
"uuid": newUser.uuid,
|
||||
"name": newUser.name,
|
||||
"mail": newUser.mail,
|
||||
},
|
||||
})
|
||||
})
|
||||
.catch(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
|
||||
@@ -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 ------------------
|
||||
const addUser = (u) => {
|
||||
return Users.addOne(u)
|
||||
|
||||
Reference in New Issue
Block a user