Skip to content

Commit

Permalink
Merge pull request #48 from andela/bg-fix-all-bugs-branch
Browse files Browse the repository at this point in the history
bug(social-login): fix social login issue
  • Loading branch information
Dom58 committed Aug 29, 2019
2 parents 5d41907 + 5c6ff4b commit 751e3f0
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
35 changes: 10 additions & 25 deletions src/controllers/socialLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}

Expand All @@ -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}`);
}
}

Expand All @@ -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`,
Expand All @@ -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}`);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/db/migrations/20190625140719-create-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions src/db/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class mail {
subject: 'password reset',
text: `Dear, ${username} please use the provided link to reset your password`,
html: `<div>Dear ${username},<br>You have requested to reset your password.<br></div>
<a href="https://ah-lobos-backend-swagger.herokuapp.com/api/users/reset-password/${token}">Please click here to reset your password</a>
<a href="${process.env.FRONT_END_URL}/newpassword/${token}">Please click here to reset your password</a>
<br><h6>Thank you for using Authors haven.</h6>`,
};
const sent = await sendGridMail.send(message);
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/middlewares/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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
Expand Down

0 comments on commit 751e3f0

Please sign in to comment.