- Added email availability checker in API

This commit is contained in:
Alexis
2020-12-19 18:15:39 +01:00
parent eba2669176
commit 20f946a945
2 changed files with 47 additions and 4 deletions

View File

@@ -166,18 +166,36 @@ class UserRepository {
// Check if one user already has that email
checkIfEmailAvailable(mail) {
return new Promise((resolve, reject) => {
if (!this.validateEmail(mail)) {
reject({
"message": "The request isn't a correctly formed email.",
"code": 400,
})
}
this.getOneByEmail(mail, false)
.then(() => {
reject({
resolve({
"message": "This email is already linked to an account.",
"code": 403
"available": false,
"code": 200
})
})
.catch(() => {
resolve(true)
resolve({
"message": "This email is available.",
"available": true,
"code": 200
})
})
})
}
validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
}
module.exports = UserRepository

View File

@@ -38,7 +38,7 @@ router.get('/', async (req, res) => {
})
// GET ONE FORM UUID ------------------
// GET ONE FROM UUID ------------------
const getUserByUUID = (uuid) => {
return Users.getOneByUUID(uuid)
.catch(err => {
@@ -63,6 +63,31 @@ router.get('/:uuid/', async (req, res) => {
})
// CHECK IF MAIL IS AVAILABLE ------------------
const checkIfEmailAvailable = (mail) => {
return Users.checkIfEmailAvailable(mail)
.catch(err => {
console.log(err)
throw err
})
}
router.get('/available/:mail/', async (req, res) => {
checkIfEmailAvailable(req.params.mail)
.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
})
)
})
})
// LOG A USER ------------------
const logUser = (mail, password) => {
return Users.logUser(mail, password)