diff --git a/.env.example b/.env.example index 7196c5b..eaf9314 100644 --- a/.env.example +++ b/.env.example @@ -26,6 +26,9 @@ GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET= GOOGLE_CALLBACK_URL= +# Redirect URL to client app (preferably the login page) +CLIENT_REDIRECT_URL= + # sessions details COOKIE_SECRET= diff --git a/server/config/passport.js b/server/config/passport.js index c5038a6..09a38e4 100644 --- a/server/config/passport.js +++ b/server/config/passport.js @@ -6,7 +6,7 @@ import { env, validateConfigVariable } from '../helpers/utils'; import logger from '../helpers/logger'; import models from '../models'; -const { User } = models; +const { User, Profile } = models; // Ensure that ENV config variables is set validateConfigVariable([ @@ -34,13 +34,23 @@ validateConfigVariable([ export const generateOrFindUser = async (accessToken, refreshToken, profile, done) => { if (profile.emails[0]) { + const email = profile.emails[0].value; try { - await User.findOrCreate({ where: { email: profile.emails[0].value } }) + await User.findOrCreate({ where: { email } }) /* the "spread" divides the array that findOrCreate method returns into 2 parts and passes them as arguments to the callback function, which treats them as "user" and "created". */ - .spread((user, created) => { + .spread(async (user, created) => { + // create user profile + const [firstname, lastname] = profile.displayName.split(' '); + user.firstname = firstname; + user.lastname = lastname; + user.image = profile.photos[0].value; + user.userId = user.id; + user.bio = ''; + user.username = email; + await Profile.create(user); done(null, user); }); } catch (err) { diff --git a/server/controllers/authController.js b/server/controllers/authController.js index 117816f..3dde918 100644 --- a/server/controllers/authController.js +++ b/server/controllers/authController.js @@ -1,4 +1,4 @@ -import { generateToken } from '../helpers/utils'; +import { generateToken, env } from '../helpers/utils'; import logger from '../helpers/logger'; /** @@ -16,18 +16,8 @@ export default class authController { static async socialAuth(req, res) { const { dataValues } = req.user; try { - const userToken = await generateToken({ email: dataValues.email, id: dataValues.id }); - const { - id, - email, - } = dataValues; - return res.status(200).json({ - user: { - id, - email, - token: userToken, - } - }); + const token = await generateToken({ email: dataValues.email, id: dataValues.id }); + res.redirect(`${env('CLIENT_REDIRECT_URL')}?token=${token}`); } catch (err) { logger.log(err); } diff --git a/server/controllers/usersController.js b/server/controllers/usersController.js index 50eecf6..f9d1985 100644 --- a/server/controllers/usersController.js +++ b/server/controllers/usersController.js @@ -34,7 +34,6 @@ class UsersController { const user = await User.create(body); const role = await models.Role.findOne({ where: { name: 'user' } }); await user.setRole(role); - const token = await generateToken({ user }); // generate confirm token const confirmationToken = await generateToken({ email: user.email }); // generate confirm link @@ -57,6 +56,7 @@ class UsersController { await models.Profile.create(user); // create a user default settings await models.Setting.create({ userId: user.userId }); + const token = await generateToken({ user }); Response.send(response, STATUS.CREATED, { token, id: user.id }); await Mail.sendMail(data); return; diff --git a/server/routes/auth.js b/server/routes/auth.js index 0f9eb07..644acc6 100644 --- a/server/routes/auth.js +++ b/server/routes/auth.js @@ -1,10 +1,14 @@ import express from 'express'; import passport from '../config/passport'; import authController from '../controllers/authController'; - +import { env } from '../helpers/utils'; const router = express.Router(); +const failureRedirect = () => ({ + failureRedirect: `${env('CLIENT_REDIRECT_URL')}?failure=true` +}); + /** * @swagger * definitions: @@ -53,7 +57,7 @@ router.get( router.get( '/facebook/redirect', - passport.authenticate('facebook', { failureRedirect: '/' }), + passport.authenticate('facebook', failureRedirect()), authController.socialAuth ); @@ -84,7 +88,7 @@ router.get( router.get( '/google/redirect', - passport.authenticate('google', { failureRedirect: '/' }), + passport.authenticate('google', failureRedirect()), authController.socialAuth ); @@ -110,7 +114,7 @@ router.get( router.get( '/twitter/redirect', - passport.authenticate('twitter', { failureRedirect: '/' }), + passport.authenticate('twitter', failureRedirect()), authController.socialAuth ); diff --git a/test/integrations/routes/auth.test.js b/test/integrations/routes/auth.test.js index 40e3e09..5110020 100644 --- a/test/integrations/routes/auth.test.js +++ b/test/integrations/routes/auth.test.js @@ -1,3 +1,4 @@ +/* eslint-disable no-unused-expressions */ import chai, { expect } from 'chai'; import chaiHttp from 'chai-http'; import app from '../../../server';