From 3dbf02c97242dbc8bd8a58d61405998061aa3e99 Mon Sep 17 00:00:00 2001 From: mohammedsalah7 Date: Thu, 28 Oct 2021 12:43:43 +0300 Subject: [PATCH 1/5] create end point login #15 --- server/app.js | 2 +- server/controllers/index.js | 6 ++-- server/controllers/users/index.js | 3 +- server/controllers/users/login.js | 38 ++++++++++++++++++++++ server/database/quieres/checkEmailQuery.js | 5 +++ server/database/quieres/index.js | 4 ++- server/routes/index.js | 3 +- server/utils/validation/loginSchema.js | 8 +++++ 8 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 server/controllers/users/login.js create mode 100644 server/database/quieres/checkEmailQuery.js create mode 100644 server/utils/validation/loginSchema.js diff --git a/server/app.js b/server/app.js index 3d0aac1..6956b28 100644 --- a/server/app.js +++ b/server/app.js @@ -1,10 +1,10 @@ +require('env2')('.env'); const express = require('express'); const { join } = require('path'); 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; diff --git a/server/controllers/index.js b/server/controllers/index.js index d076472..dc82888 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -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, +}; diff --git a/server/controllers/users/index.js b/server/controllers/users/index.js index 79a3394..0f91d85 100644 --- a/server/controllers/users/index.js +++ b/server/controllers/users/index.js @@ -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 }; diff --git a/server/controllers/users/login.js b/server/controllers/users/login.js new file mode 100644 index 0000000..4da4139 --- /dev/null +++ b/server/controllers/users/login.js @@ -0,0 +1,38 @@ +/* eslint-disable no-console */ +/* 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; + console.log(req.body); + await loginSchema.validateAsync(req.body); + + const { rows } = await checkEmailQuery(email); + console.log(rows); + + if (!rows.length) { + throw new Error({ message: 'Invalid email or password' }); + } + const compared = await bcrypt.compare(password, rows[0].password); + if (!compared) { + throw new Error({ message: 'Invalid email or password' }); + } + const token = await signToken(email, rows[0].id, rows[0].name, rows[0].phone, rows[0].avater); + res.cookie('token', token); + return res.json({ message: 'logged in successfully' }); + } catch (err) { + if (err.details) { + res.status(400).json({ + message: err.details[0].message, + }); + } else { + return next(err); + } + } +}; + +module.exports = login; diff --git a/server/database/quieres/checkEmailQuery.js b/server/database/quieres/checkEmailQuery.js new file mode 100644 index 0000000..9423b86 --- /dev/null +++ b/server/database/quieres/checkEmailQuery.js @@ -0,0 +1,5 @@ +const connection = require('../connection'); + +const checkEmailQuery = (email) => connection.query('SELECT * FROM agents WHERE email= ($1)', [email]); + +module.exports = checkEmailQuery; diff --git a/server/database/quieres/index.js b/server/database/quieres/index.js index 386595b..1302794 100644 --- a/server/database/quieres/index.js +++ b/server/database/quieres/index.js @@ -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, }; diff --git a/server/routes/index.js b/server/routes/index.js index 92cf993..7c90092 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -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); diff --git a/server/utils/validation/loginSchema.js b/server/utils/validation/loginSchema.js new file mode 100644 index 0000000..74139cd --- /dev/null +++ b/server/utils/validation/loginSchema.js @@ -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 }; From 0f568d20e21c3cdf368bb0764b8a617230ca68bc Mon Sep 17 00:00:00 2001 From: mohammedsalah7 Date: Thu, 28 Oct 2021 16:13:30 +0300 Subject: [PATCH 2/5] create test for /login #15 --- .eslintrc.json | 2 +- server/controllers/users/login.js | 4 +--- server/test/index.test.js | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index c00798a..7276943 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -9,7 +9,7 @@ "airbnb-base" ], "parserOptions": { - "ecmaVersion": "13" + "ecmaVersion": "latest" }, "rules": { } diff --git a/server/controllers/users/login.js b/server/controllers/users/login.js index 4da4139..066a757 100644 --- a/server/controllers/users/login.js +++ b/server/controllers/users/login.js @@ -8,11 +8,9 @@ const { signToken } = require('../../utils'); const login = async (req, res, next) => { try { const { email, password } = req.body; - console.log(req.body); await loginSchema.validateAsync(req.body); const { rows } = await checkEmailQuery(email); - console.log(rows); if (!rows.length) { throw new Error({ message: 'Invalid email or password' }); @@ -23,7 +21,7 @@ const login = async (req, res, next) => { } const token = await signToken(email, rows[0].id, rows[0].name, rows[0].phone, rows[0].avater); res.cookie('token', token); - return res.json({ message: 'logged in successfully' }); + return res.json({ message: 'You are Logged Successfully' }); } catch (err) { if (err.details) { res.status(400).json({ diff --git a/server/test/index.test.js b/server/test/index.test.js index d5dd994..052f134 100644 --- a/server/test/index.test.js +++ b/server/test/index.test.js @@ -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: 'kallport0@patch.com', + 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: 'kallport0@patch.com', + password: '123456987', + }) + .expect(400); + expect(res.body).toEqual({ message: 'Invalid email or passwor' }); + }); }); describe('user estates', () => { From 7116e473ff457992fd4b2ac002b99ac7bb65768e Mon Sep 17 00:00:00 2001 From: mohammedsalah7 Date: Fri, 29 Oct 2021 20:26:55 +0300 Subject: [PATCH 3/5] edit Suggested changes #15 --- server/app.js | 1 - server/controllers/users/getAllUsers.js | 1 - server/controllers/users/login.js | 4 +--- server/test/index.test.js | 2 +- 4 files changed, 2 insertions(+), 6 deletions(-) diff --git a/server/app.js b/server/app.js index 6956b28..7bc115b 100644 --- a/server/app.js +++ b/server/app.js @@ -1,4 +1,3 @@ -require('env2')('.env'); const express = require('express'); const { join } = require('path'); const compression = require('compression'); diff --git a/server/controllers/users/getAllUsers.js b/server/controllers/users/getAllUsers.js index b5cb32f..bf179b7 100644 --- a/server/controllers/users/getAllUsers.js +++ b/server/controllers/users/getAllUsers.js @@ -1,4 +1,3 @@ -/* eslint-disable no-console */ const { getAllUsersQuery } = require('../../database/quieres'); const getAllUsers = async (req, res, next) => { diff --git a/server/controllers/users/login.js b/server/controllers/users/login.js index 066a757..41e3786 100644 --- a/server/controllers/users/login.js +++ b/server/controllers/users/login.js @@ -1,4 +1,3 @@ -/* eslint-disable no-console */ /* eslint-disable consistent-return */ const bcrypt = require('bcrypt'); const { loginSchema } = require('../../utils/validation/loginSchema'); @@ -20,8 +19,7 @@ const login = async (req, res, next) => { throw new Error({ message: 'Invalid email or password' }); } const token = await signToken(email, rows[0].id, rows[0].name, rows[0].phone, rows[0].avater); - res.cookie('token', token); - return res.json({ message: 'You are Logged Successfully' }); + return res.cookie('token', token).json({ message: 'You are Logged Successfully' }); } catch (err) { if (err.details) { res.status(400).json({ diff --git a/server/test/index.test.js b/server/test/index.test.js index 052f134..28cb9b4 100644 --- a/server/test/index.test.js +++ b/server/test/index.test.js @@ -35,7 +35,7 @@ describe('Tests Server', () => { password: '123456987', }) .expect(400); - expect(res.body).toEqual({ message: 'Invalid email or passwor' }); + expect(res.body).toEqual({ message: 'Invalid email or password' }); }); }); From ad622a3246d2e56b38cdc53d7a05b39ae31b9b99 Mon Sep 17 00:00:00 2001 From: mohammedsalah7 Date: Sat, 30 Oct 2021 17:39:15 +0200 Subject: [PATCH 4/5] edit Suggested changes #15 --- server/controllers/users/login.js | 7 ++++--- server/database/connection.js | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/server/controllers/users/login.js b/server/controllers/users/login.js index 41e3786..499f678 100644 --- a/server/controllers/users/login.js +++ b/server/controllers/users/login.js @@ -12,13 +12,14 @@ const login = async (req, res, next) => { const { rows } = await checkEmailQuery(email); if (!rows.length) { - throw new Error({ message: 'Invalid email or password' }); + return res.status(400).json({ message: 'Invalid email or password' }); } + const compared = await bcrypt.compare(password, rows[0].password); if (!compared) { - throw new Error({ message: 'Invalid email or password' }); + return res.status(400).json({ message: 'Invalid email or password' }); } - const token = await signToken(email, rows[0].id, rows[0].name, rows[0].phone, rows[0].avater); + const token = await signToken(email, rows[0].id); return res.cookie('token', token).json({ message: 'You are Logged Successfully' }); } catch (err) { if (err.details) { diff --git a/server/database/connection.js b/server/database/connection.js index e2c8673..7ef7a43 100644 --- a/server/database/connection.js +++ b/server/database/connection.js @@ -1,3 +1,4 @@ +require('env2')('.env'); const { Pool } = require('pg'); const { From 8c299c8627524e8e65360d93e7fc7b4273ff474b Mon Sep 17 00:00:00 2001 From: Muhammad Abdulhadi Date: Sun, 31 Oct 2021 12:12:34 +0200 Subject: [PATCH 5/5] fix tests and edit fake date --- server/database/config/fakeData.sql | 2 +- server/test/index.test.js | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/server/database/config/fakeData.sql b/server/database/config/fakeData.sql index 55c810a..2ad51e6 100644 --- a/server/database/config/fakeData.sql +++ b/server/database/config/fakeData.sql @@ -1,6 +1,6 @@ -- INSERT INTO agents (name, email, password , phone) - VALUES ('Kai', 'kallport0@patch.com', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '677-871-7450'), + VALUES ('Kai', 'kallport0@patch.com', '$2b$10$gT8Qb2Qe01W1QMRFmH9IC.3bmbA4PS2yG4XQvdkYWxKday.SbjGI2', '677-871-7450'), ('Trixie', 'tbeadon1@plala.or.jp', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '0599832685'), ('Allina', 'aburford2@tumblr.com', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '630-385-8312'); diff --git a/server/test/index.test.js b/server/test/index.test.js index f36ddff..908b71c 100644 --- a/server/test/index.test.js +++ b/server/test/index.test.js @@ -53,7 +53,7 @@ describe('user estates', () => { agent_id: 3, title: 'rhoncus sed vestibulum', price: '84726.00', - description: 'massa volutpat convallismorbi odio odio elementum eu interdum eu tincidunt in', + description: 'massa volutpat convallis morbi odio odio elementum eu interdum eu tincidunt in', type: 'Buy', category: 'House', street: '0824 Mcguire Way', @@ -67,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, + }, ], }); });