Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create end point login #15 #33

Merged
merged 7 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"airbnb-base"
],
"parserOptions": {
"ecmaVersion": "13"
"ecmaVersion": "latest"
},
"rules": {
}
Expand Down
1 change: 0 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const compression = require('compression');
const cookieParser = require('cookie-parser');
const { notFoundError, serverError } = require('./controllers/errors');
const router = require('./routes');
require('env2')('.env');

const { env: { PORT, NODE_ENV } } = process;

Expand Down
6 changes: 4 additions & 2 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { userEstateshandler } = require('./users');
const { userEstateshandler, login } = require('./users');
const getAllUsers = require('./users/getAllUsers');
const logout = require('./logout');

module.exports = { getAllUsers, userEstateshandler, logout };
module.exports = {
getAllUsers, userEstateshandler, logout, login,
};
1 change: 0 additions & 1 deletion server/controllers/users/getAllUsers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-console */
const { getAllUsersQuery } = require('../../database/quieres');

const getAllUsers = async (req, res, next) => {
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/users/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const userEstateshandler = require('./userEstates');
const getAllUsers = require('./getAllUsers');
const login = require('./login');

module.exports = { getAllUsers, userEstateshandler };
module.exports = { getAllUsers, userEstateshandler, login };
34 changes: 34 additions & 0 deletions server/controllers/users/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable consistent-return */
const bcrypt = require('bcrypt');
const { loginSchema } = require('../../utils/validation/loginSchema');
const { checkEmailQuery } = require('../../database/quieres');
const { signToken } = require('../../utils');

const login = async (req, res, next) => {
try {
const { email, password } = req.body;
await loginSchema.validateAsync(req.body);

const { rows } = await checkEmailQuery(email);

if (!rows.length) {
throw new Error({ message: 'Invalid email or password' });
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
}
const compared = await bcrypt.compare(password, rows[0].password);
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
if (!compared) {
throw new Error({ message: 'Invalid email or password' });
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
}
const token = await signToken(email, rows[0].id, rows[0].name, rows[0].phone, rows[0].avater);
return res.cookie('token', token).json({ message: 'You are Logged Successfully' });
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
if (err.details) {
res.status(400).json({
message: err.details[0].message,
});
} else {
return next(err);
}
}
};

module.exports = login;
5 changes: 5 additions & 0 deletions server/database/quieres/checkEmailQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const connection = require('../connection');

const checkEmailQuery = (email) => connection.query('SELECT * FROM agents WHERE email= ($1)', [email]);

module.exports = checkEmailQuery;
4 changes: 3 additions & 1 deletion server/database/quieres/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const userEstatesQuery = require('./userEstatesQuiery');
const userEstatesQuery = require('./UserEstatesQuiery');
const getAllUsersQuery = require('./getAllUsersQuery');
const checkEmailQuery = require('./checkEmailQuery');

module.exports = {
getAllUsersQuery,
userEstatesQuery,
checkEmailQuery,

};
3 changes: 2 additions & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const router = require('express').Router();
const { userEstateshandler } = require('../controllers');
const { getAllUsers, logout } = require('../controllers');
const { getAllUsers, logout, login } = require('../controllers');

router.get('/users/:userId/estates', userEstateshandler);
router.post('/login', login);
router.get('/users', getAllUsers);
router.get('/logout', logout);

Expand Down
22 changes: 22 additions & 0 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ describe('Tests Server', () => {
.expect('Content-Type', /json/);
return expect(6).toEqual(res.body.data.length);
});

test(' login route /login ', async () => {
const res = await supertest(app)
.post('/api/v1/login')
.send({
email: '[email protected]',
password: '6D29E04CD937DC37CB72E42736CA238C',
})
.expect(200);
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/login')
.send({
email: '[email protected]',
password: '123456987',
})
.expect(400);
expect(res.body).toEqual({ message: 'Invalid email or password' });
});
});

describe('user estates', () => {
Expand Down
8 changes: 8 additions & 0 deletions server/utils/validation/loginSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const joi = require('joi');

const loginSchema = joi.object({
email: joi.string().email().required(),
password: joi.string().min(5).required(),
});

module.exports = { loginSchema };