-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const adminLogin = require('./login'); | ||
const signupAdmin = require('./signupAdmin'); | ||
|
||
module.exports = { | ||
adminLogin, | ||
signupAdmin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint-disable consistent-return */ | ||
const bcrypt = require('bcrypt'); | ||
const { loginSchema } = require('../../utils/validation/loginSchema'); | ||
const { checkAdminQuery } = require('../../database/quieres'); | ||
const { signToken } = require('../../utils'); | ||
|
||
const adminLogin = async (req, res, next) => { | ||
try { | ||
const { email, password } = req.body; | ||
await loginSchema.validateAsync(req.body); | ||
|
||
const { rows } = await checkAdminQuery(email); | ||
|
||
if (!rows.length) { | ||
return res.status(400).json({ message: 'Invalid email or password' }); | ||
} | ||
|
||
const compared = await bcrypt.compare(password, rows[0].password); | ||
if (!compared) { | ||
return res.status(400).json({ message: 'Invalid email or password' }); | ||
} | ||
const token = await signToken(email, rows[0].id); | ||
return res.cookie('token', token).json({ message: 'You are Logged Successfully' }); | ||
} catch (err) { | ||
if (err.details) { | ||
res.status(400).json({ | ||
message: err.details[0].message, | ||
}); | ||
} else { | ||
return next(err); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = adminLogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { hash } = require('bcrypt'); | ||
const adminSchema = require('../../utils/validation/adminSchema'); | ||
const { signUpAdminQuery } = require('../../database/quieres'); | ||
const { signToken } = require('../../utils'); | ||
|
||
module.exports = async (req, res, next) => { | ||
try { | ||
const { | ||
error, value: { | ||
password, email, username, | ||
}, | ||
} = adminSchema.validate(req.body); | ||
if (error) return res.status(400).json({ message: error.details[0].message }); | ||
const hasedPasword = await hash(password, 10); | ||
await signUpAdminQuery(username, hasedPasword, email); | ||
const token = await signToken(email, username); | ||
return res.status(201).cookie('token', token).json({ message: 'user created' }); | ||
} catch (err) { | ||
if (err.code === '23505') { | ||
return res.status(400).json({ message: 'The user is already exists' }); | ||
} | ||
return next(err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const logout = (req, res) => { | ||
res.clearCookie('token'); | ||
res.clearCookie('token').json({ message: 'Logout Successfully' }); | ||
}; | ||
module.exports = logout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const connection = require('../config/connection'); | ||
|
||
module.exports = (userName, password, email) => connection.query('INSERT INTO admins (username,password, email) VALUES ($1,$2,$3)', [userName, password, email]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const router = require('express').Router(); | ||
const { signupAdmin, adminLogin } = require('../controllers/admins'); | ||
|
||
router.post('/signup', signupAdmin); | ||
router.post('/login', adminLogin); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,30 @@ describe('Tests login route', () => { | |
}); | ||
}); | ||
|
||
describe('Tests login route to admin', () => { | ||
test(' login route /login ', async () => { | ||
const res = await supertest(app) | ||
.post('/api/v1/admin/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: '1234567894455', | ||
}) | ||
.expect(200); | ||
return expect(res.body).toEqual({ message: 'You are Logged Successfully' }); | ||
}); | ||
|
||
test(' login route /login with error in email or password ', async () => { | ||
const res = await supertest(app) | ||
.post('/api/v1/admin/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: '1234566', | ||
}) | ||
.expect(400); | ||
return expect(res.body).toEqual({ message: 'Invalid email or password' }); | ||
}); | ||
}); | ||
|
||
describe('user estates', () => { | ||
test('get users estates', async () => { | ||
const res = await supertest(app) | ||
|
@@ -294,6 +318,20 @@ describe('test Edit Agent data /user/:iduser ', () => { | |
.expect('Content-Type', /json/); | ||
return expect(res.body).toEqual({ | ||
message: 'There\'s no Agent, put correct id', | ||
describe('test signup as admin ', () => { | ||
test('test sign up endpoint when success', async () => { | ||
const res = await supertest(app) | ||
.post('/api/v1/admin/signup') | ||
.send({ | ||
username: 'test', | ||
password: 'test123456', | ||
email: '[email protected]', | ||
}) | ||
.expect(201) | ||
.expect((response) => expect(response.header['set-cookie'][0].split('=')[0]).toBe('token')) | ||
.expect('Content-Type', /json/); | ||
return expect(res.body).toEqual({ | ||
message: 'user created', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const joi = require('joi'); | ||
|
||
module.exports = joi.object({ | ||
username: joi.string().required(), | ||
email: joi.string().email().required(), | ||
password: joi.string().min(5).required(), | ||
}); |