Skip to content

Commit

Permalink
Google login fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tiller1010 committed Dec 31, 2022
1 parent 74cab85 commit 3251b5e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ passport.use(new GoogleStrategy({

try {
let user = await findAndSyncUser(profile.id, 'google');
await sendEmail(newUser.email, `${process.env.SECURED_DOMAIN_WITHOUT_PROTOCOL}> Registration`, "<b>Thank you for registering</b>");

if(user){
if (user) {
return done(null, user);
} else {
let user = await addUser(newUser);
await sendEmail(newUser.email, `${process.env.SECURED_DOMAIN_WITHOUT_PROTOCOL}> Registration`, "<b>Thank you for registering</b>");
return done(null, newUser);
}
} catch(error) {
Expand Down
2 changes: 1 addition & 1 deletion js/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Login extends React.Component {
Register
<FontAwesomeIcon icon={faLongArrowAltRight}/>
</a>
<a href={`/auth/google?backURL=${backURL}`} className="button">
<a href={`/auth/google${backURL ? '?backURL=' + backURL : ''}`} className="button">
Login with Google
<FontAwesomeIcon icon={faGoogle}/>
</a>
Expand Down
2 changes: 1 addition & 1 deletion public/js/main.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions routes/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ module.exports.defineAuthenticationRoutes = function(app) {

// Google login
let googleNativeFlag = false;
let googleBackURL = '';
app.get('/auth/google', (req, res, next) => {
const { backURL, nativeFlag } = req.query;
if(nativeFlag){
if (nativeFlag) {
googleNativeFlag = true;
}
googleBackURL = backURL || '';
next();
}, passport.authenticate('google', { scope: [ 'https://www.googleapis.com/auth/plus.login', 'email' ] }));

app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), (req, res) => {

if(req.user && !req.cookies.jwt){
Expand All @@ -98,7 +101,8 @@ module.exports.defineAuthenticationRoutes = function(app) {
res.redirect(process.env.REACT_NATIVE_APP_URL + '?userID=' + String(req.user._id));
return;
} else {
const successRedirect = backURL ? backURL : '/account-profile';
const successRedirect = googleBackURL ? googleBackURL : '/account-profile';
googleBackURL = '';
res.redirect(successRedirect);
return;
}
Expand Down

0 comments on commit 3251b5e

Please sign in to comment.