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 6 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
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
2 changes: 2 additions & 0 deletions server/controllers/users/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const signup = require('./signup');
const userEstateshandler = require('./userEstates');
const getAllUsers = require('./getAllUsers');
const login = require('./login');

module.exports = {
signup,
getAllUsers,
userEstateshandler,
login,
};
36 changes: 36 additions & 0 deletions server/controllers/users/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-console */
/* eslint-disable consistent-return */
Mu7ammadAbed marked this conversation as resolved.
Show resolved Hide resolved
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) {
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 = login;
2 changes: 1 addition & 1 deletion server/database/config/fakeData.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--
INSERT INTO agents (name, email, password , phone)
VALUES ('Kai', '[email protected]', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '677-871-7450'),
VALUES ('Kai', '[email protected]', '$2b$10$gT8Qb2Qe01W1QMRFmH9IC.3bmbA4PS2yG4XQvdkYWxKday.SbjGI2', '677-871-7450'),
('Trixie', '[email protected]', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '0599832685'),
('Allina', '[email protected]', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '630-385-8312');

Expand Down
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('../config/connection');

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

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

module.exports = {
getAllUsersQuery,
userEstatesQuery,
checkEmailQuery,

};
4 changes: 3 additions & 1 deletion server/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const router = require('express').Router();
const { logout } = require('../controllers');
const { logout, login } = require('../controllers');

router.get('/logout', logout);
router.post('/login', login);

module.exports = router;
43 changes: 42 additions & 1 deletion server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,29 @@ describe('Tests Server', () => {
.get('/api/v1/users')
.expect(200)
.expect('Content-Type', /json/);
return expect(6).toEqual(res.body.data.length);
return expect(3).toEqual(res.body.data.length);
});

test(' login route /login ', async () => {
const res = await supertest(app)
.post('/api/v1/login')
.send({
email: '[email protected]',
password: '12345',
})
.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' });
});
});

Expand Down Expand Up @@ -45,6 +67,25 @@ describe('user estates', () => {
rate: 5,
available: false,
},
{
id: 6,
agent_id: 3,
title: 'vestibulum ante ipsum primis',
price: '194193.55',
description: 'leo odio porttitor id consequat in consequat ut nulla sed',
type: 'Buy',
category: 'House',
street: '0891 7th Park',
city: 'Álimos',
region: 'Greece',
bathrooms: 1,
bedrooms: 3,
rooms: 2,
space: '174',
approved: false,
rate: 1,
available: false,
},
],
});
});
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 };