From 5c6ff4bf5367cddf370011e25d251c4f6acc2725 Mon Sep 17 00:00:00 2001 From: Karangwa Hirwa Julien Date: Mon, 19 Aug 2019 10:08:39 +0200 Subject: [PATCH] bug(social-login): fix social login issue - fix bugs for the required attributes to not required - change urls to proper frontend urls --- README.md | 2 +- src/controllers/socialLogin.js | 35 ++++++------------- src/controllers/userControllers.js | 2 +- .../migrations/20190625140719-create-users.js | 4 +-- src/db/models/users.js | 6 ++-- src/helpers/mail.js | 2 +- src/index.js | 2 ++ src/middlewares/signup.js | 7 ++-- 8 files changed, 22 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 45573e8..04780c5 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ However, for the purpose of testing and configurations, one endpoint has been cr > npm install ``` -> create database to postgresql +> create dataabase to postgresql ``` > createdb authorsHavenDb ``` diff --git a/src/controllers/socialLogin.js b/src/controllers/socialLogin.js index 1536a75..5fe4d19 100644 --- a/src/controllers/socialLogin.js +++ b/src/controllers/socialLogin.js @@ -28,14 +28,9 @@ class UserInfo { hash: process.env.DEFAULT_PASSWORD }); if (newUser) { - const { dataValues: { id, username, email } } = newUser; - const token = await processToken.signToken(newUser.dataValues); - return res.status(200).json({ - message: `Welcome to Authors Haven ${displayName} `, - data: { - token, id, username, email - }, - }); + const { dataValues } = newUser; + const generatedToken = await processToken.signToken(dataValues); + return res.redirect(`${process.env.FRONT_END_URL}/social-login?token=${generatedToken}`); } } @@ -57,14 +52,8 @@ class UserInfo { hash: process.env.DEFAULT_PASSWORD }); if (newUser) { - const { dataValues: { id, username, email } } = newUser; const token = await processToken.signToken(newUser.dataValues); - return res.status(200).json({ - message: `Welcome to Authors Haven ${displayName} `, - data: { - token, id, username, email - }, - }); + return res.redirect(`${process.env.FRONT_END_URL}/social-login?token=${token}`); } } @@ -75,8 +64,6 @@ class UserInfo { * @return {Object} user logged in */ static async twitterLogin(req, res) { - const { displayName } = req.user; - // eslint-disable-next-line no-unused-expressions const newUser = await Users.create({ username: req.user.username, email: `${req.user.username}@gmail.com`, @@ -87,14 +74,12 @@ class UserInfo { hash: process.env.DEFAULT_PASSWORD }); if (newUser) { - const { dataValues: { id, username } } = newUser; - const token = await processToken.signToken(newUser.dataValues); - return res.status(200).json({ - message: `Welcome to Authors Haven ${displayName} `, - data: { - token, id, username - }, - }); + const { dataValues: payload } = newUser; + /** + * Twitter redirect + */ + const token = await processToken.signToken(payload); + return res.redirect(`${process.env.FRONT_END_URL}/social-login?token=${token}`); } } } diff --git a/src/controllers/userControllers.js b/src/controllers/userControllers.js index eea8ab4..cc8e0b3 100644 --- a/src/controllers/userControllers.js +++ b/src/controllers/userControllers.js @@ -163,7 +163,7 @@ class UserManager { error: resetEmail, }); } - return res.status(404).json({ error: `email: ${req.body.email} not found, please check your email and try again`, }); + return res.status(404).json({ error: 'We couldn not find your account with that information,Please check and try again', }); } /** diff --git a/src/db/migrations/20190625140719-create-users.js b/src/db/migrations/20190625140719-create-users.js index 1072ce6..9a3daf3 100644 --- a/src/db/migrations/20190625140719-create-users.js +++ b/src/db/migrations/20190625140719-create-users.js @@ -8,11 +8,11 @@ module.exports = { }, firstName: { type: Sequelize.STRING, - allowNull: false + allowNull: true }, lastName: { type: Sequelize.STRING, - allowNull: false + allowNull: true }, username: { type: Sequelize.STRING, diff --git a/src/db/models/users.js b/src/db/models/users.js index a26ab00..03b49e3 100644 --- a/src/db/models/users.js +++ b/src/db/models/users.js @@ -6,15 +6,13 @@ export default (sequelize, DataTypes) => { firstName: { type: DataTypes.STRING, allowNull: { - args: false, - message: 'Please enter you firstname' + args: true } }, lastName: { type: DataTypes.STRING, allowNull: { - args: false, - message: 'Please enter you lastname' + args: true } }, username: { diff --git a/src/helpers/mail.js b/src/helpers/mail.js index c2b3340..f3152c8 100644 --- a/src/helpers/mail.js +++ b/src/helpers/mail.js @@ -24,7 +24,7 @@ class mail { subject: 'password reset', text: `Dear, ${username} please use the provided link to reset your password`, html: `
Dear ${username},
You have requested to reset your password.
- Please click here to reset your password + Please click here to reset your password
Thank you for using Authors haven.
`, }; const sent = await sendGridMail.send(message); diff --git a/src/index.js b/src/index.js index 6bf2769..7a143e9 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ import swaggerUI from 'swagger-ui-express'; import cron from 'node-cron'; import passport from 'passport'; import session from 'express-session'; +import cors from 'cors'; import swaggerJSDoc from '../swagger.json'; import routes from './routes/api/index'; import config from './db/config/envirnoment'; @@ -15,6 +16,7 @@ import './helpers/eventListener'; const app = express(); // setup express application +app.use(cors()); app.use(logger('dev')); // log requests to the console // Parse incoming requests data diff --git a/src/middlewares/signup.js b/src/middlewares/signup.js index 6109a19..1efaf7a 100644 --- a/src/middlewares/signup.js +++ b/src/middlewares/signup.js @@ -15,8 +15,6 @@ class SignupValidation { */ static async signupvalidator(req, res, next) { const signupSchema = Joi.object().keys({ - firstName: Joi.string().regex(/^\S+/).min(4).required(), - lastName: Joi.string().regex(/^\S+/).min(4).required(), username: Joi.string().regex(/^\S+$/).min(4).required() .options({ language: { string: { regex: { base: 'please remove spaces between word!' } } } }) .label('Username'), @@ -26,8 +24,9 @@ class SignupValidation { .label('Password'), }); const user = { - firstName: req.body.firstName, - lastName: req.body.lastName, + /** + * signup body + */ username: req.body.username, email: req.body.email, password: req.body.password