Skip to content

Commit

Permalink
Merge f718fd8 into ca8ea14
Browse files Browse the repository at this point in the history
  • Loading branch information
kevoese committed Aug 6, 2019
2 parents ca8ea14 + f718fd8 commit c28d36a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
4 changes: 0 additions & 4 deletions controllers/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ export default {
}
},

home: async (req, res) => res.status(200).send({
user: req.user
}),

changeEmailNotification: async (req, res) => {
const { user } = req;
const notifyMe = user.emailNotify;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"consola": "^2.9.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"cryptr": "^4.0.2",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"express-fileupload": "^1.1.5",
Expand Down
12 changes: 9 additions & 3 deletions routes/oauth/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import express from 'express';
import dotenv from 'dotenv';
import passport from '../../middlewares/passport';
import { encryptQuery } from '../../utils';

dotenv.config();

const router = express.Router();

const redirect = (req, res) => res.redirect(`${process.env.FRONTEND_STAGING_URL}/#/signin?token=${encryptQuery(req.user.token)}&username=${req.user.username}`);

// google login
router.get('/google', passport.authenticate('google', { scope: ['profile', 'email'] }));
router.get(
Expand All @@ -11,7 +17,7 @@ router.get(
failureRedirect: '/api/v1/error'
}),
/* istanbul ignore next */
(req, res) => res.redirect('/api/v1/users/home')
redirect
);

// facebook login
Expand All @@ -22,7 +28,7 @@ router.get(
failureRedirect: '/api/v1/error'
}),
/* istanbul ignore next */
(req, res) => res.redirect('/api/v1/users/home')
redirect
);

// twiiter login
Expand All @@ -33,7 +39,7 @@ router.get(
failureRedirect: '/api/v1/error'
}),
/* istanbul ignore next */
(req, res) => res.redirect('/api/v1/users/home')
redirect
);

export default router;
1 change: 0 additions & 1 deletion routes/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ router.post('/signout', Middleware.authenticate, Middleware.isblackListedToken,
router.post('/reset-password', Validation.resetPassword, User.resetPassword);
router.put('/change-password', Validation.changePassword, User.changePassword);
router.put('/activate/:token', User.activate);
router.get('/home', User.home);
router.get(
'/',
Middleware.authenticate,
Expand Down
8 changes: 0 additions & 8 deletions tests/routes/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,6 @@ describe('USER AUTHENTICATION', () => {
expect(res.body.error).to.be.a('string');
expect(res.body.error).to.include('Invalid activation Link');
});

it('should call home route', async () => {
const res = await chai
.request(app)
.get('/api/v1/users/home')
.send();
expect(res).to.have.status(200);
});
});

describe('Get all Users', () => {
Expand Down
48 changes: 38 additions & 10 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jwt from 'jsonwebtoken';
import { v2 as cloudinary } from 'cloudinary';
import Cryptr from 'cryptr';
import { validations } from 'indicative';
import { Vanilla } from 'indicative/builds/formatters';
import Validator from 'indicative/builds/validator';
Expand All @@ -13,6 +14,12 @@ cloudinary.config({
api_secret: process.env.API_SECRET
});

export const encryptQuery = (string) => {
const cryptr = new Cryptr(process.env.SECRET);
const encryptedString = cryptr.encrypt(string);
return encryptedString;
};

export const uploadImage = (img, publicId) => new Promise((resolve, reject) => {
cloudinary.uploader.upload(img.tempFilePath,
{ public_id: publicId }, (err, res) => (err ? reject(err) : resolve(res.url)));
Expand Down Expand Up @@ -45,23 +52,44 @@ export const blackListThisToken = async (token) => {

export const createUserFromSocials = async (data) => {
const {
email, firstName, lastName, username, image
email, firstName, lastName, image
} = data;

const username = `${firstName}${lastName}`.toLowerCase();

let user = await db.User.findOne({
where: { email },
});

if (!user) {
user = await db.User.create({
email,
firstName,
lastName,
username,
social: true,
image,
});
try {
if (!user) {
user = await db.User.create({
email,
firstName,
lastName,
username,
social: true,
image,
});
}
} catch (e) {
if (e.name === 'SequelizeUniqueConstraintError') {
const getRandomInt = max => (Math.floor(Math.random() * Math.floor(max)));
const newUsername = `${username}-${getRandomInt(100)}`;
user = await db.User.create({
email,
firstName,
lastName,
username: newUsername,
social: true,
image,
});
return user.response();
}
throw e;
}


return user.response();
};

Expand Down

0 comments on commit c28d36a

Please sign in to comment.