Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

feat: Google Authentication via passportjs and one tap - #411

Closed
pdotsani wants to merge 17 commits into
freeCodeCamp:masterfrom
pdotsani:feature/passport-google-auth0
Closed

pdotsani wants to merge 17 commits into
freeCodeCamp:masterfrom
pdotsani:feature/passport-google-auth0

Conversation

@pdotsani

@pdotsani pdotsani commented Oct 11, 2020 •

Copy link
Copy Markdown
Contributor
  • I have read Chapter's contributing guidelines.
  • My pull request has a descriptive title (not a vague title like Update README.md).
  • My pull request targets the master branch of Chapter.

Closes #306

  • Added server side authentication to google auth via passport js library and the auth20 api.
  • Added client side to server side auth via google one tap javascript api
  • Updated user schema to accommodate google profile attributes
  • Added some UI to trigger login

This feature requires new dependencies and a few environment variables to run. You can create these credentials/keys at console.cloud.google.com or ping me on discord.

@pdotsani pdotsani changed the title Google Authentication via passportjs and one tap feat: Google Authentication via passportjs and one tap Oct 11, 2020

@timmyichen timmyichen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking on this monster of a task! I left a few comments/questions. There are quite a few todos in comments - is this PR complete or did I jump the gun on reviewing?


script.onload = () => {
const handleCredentialResponse = async (response: any) => {
console.log('handleCredentialResponse ', response);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console logs should be removed

Comment thread client/.env.local.example Outdated
NEXT_PUBLIC_GOOGLE_API_KEY=<api key here>
# will need to be assigned new values on deployment
NEXT_PUBLIC_TOKEN_AUTH_URL=http://localhost:5000/auth/google/token
NEXT_PUBLIC_IS_AUTH_URL=http://localhost:5000/auth/is-auth/ No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline

Comment thread .env.example Outdated
GOOGLE_CLIENT_ID=id from applications dashboard
GOOGLE_CLIENT_SECRET=secret from applicaitons dashboard
# Will need to be changed for production
GOOGLE_REDIRECT_URI=http://localhost:5000/auth/google/callback No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline

Comment thread client/.env.local.example Outdated
Comment on lines +4 to +5
NEXT_PUBLIC_TOKEN_AUTH_URL=http://localhost:5000/auth/google/token
NEXT_PUBLIC_IS_AUTH_URL=http://localhost:5000/auth/is-auth/ No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do these need to be environment variables?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on deployment. I wasn't sure if the url for the api would change in production. If not, then I guess we could hardcode them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be relative regardless, right? Eg /auth/google/token should work regardless of what the host is

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frontend is on 3000 and the server is on 5000, so I thought I needed to distinguish between the two.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhhhhhhhhhhhh right. in that case, the client should just set like baseUrl to be whatever its meant to be, and then in the code it should just be ${baseUrl}/auth/google/token

although i don't know why they are separate (and don't agree with it), so i'll look to change that soon

Comment thread .env.example Outdated
GOOGLE_CLIENT_ID=id from applications dashboard
GOOGLE_CLIENT_SECRET=secret from applicaitons dashboard
# Will need to be changed for production
GOOGLE_REDIRECT_URI=http://localhost:5000/auth/google/callback No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also - why does this need to be an environment variable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment above about the other env vars.

Comment thread server/auth/google/passport.ts Outdated
Comment on lines +50 to +51
console.log('parsedToken ', parsedToken);
console.log('googleId ', googleId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraneous console logs

Comment thread server/auth/index.ts Outdated
@@ -0,0 +1,23 @@
import express from 'express';
import Setup from './google/passport';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Setup -> setup

Comment thread server/auth/index.ts Outdated
Comment on lines +12 to +13
console.log('is there ', req.isAuthenticated());
console.log('is there ', req.user);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraneous console.logs

Comment thread server/auth/index.ts Resolved
Comment thread server/auth/index.ts Outdated
const router = express.Router();

router.use(
'/is-auth', // add { credentials: 'include' } into fetch request for this endpoint to work

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer this to be spelled, or at least add 'ed', out for clarity

Suggested change
'/is-auth', // add { credentials: 'include' } into fetch request for this endpoint to work
'/is-authed', // add { credentials: 'include' } into fetch request for this endpoint to work

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take care of this locally, then push so I can update the call as well

@Ryuno-Ki

Copy link
Copy Markdown
Contributor

@timmyichen Do we happen to have a logging library already (I like loglevel personally), which should be used instead of console.log()?

@timmyichen

Copy link
Copy Markdown
Contributor

@timmyichen Do we happen to have a logging library already (I like loglevel personally), which should be used instead of console.log()?

not yet i don't think, though i'm not sure if one is needed. what use case were you thinking? we can take this into Discord or into an issue if it's not relevant to this PR

@Ryuno-Ki Ryuno-Ki mentioned this pull request Oct 12, 2020
@Ryuno-Ki

Copy link
Copy Markdown
Contributor

Let's move it to #412, since it is outside the scope of this PR.

@pdotsani

pdotsani commented Oct 15, 2020 •

Copy link
Copy Markdown
Contributor Author

Thanks for taking on this monster of a task! I left a few comments/questions. There are quite a few todos in comments - is this PR complete or did I jump the gun on reviewing?

@timmyichen It's almost there! The ui still needs some logic to prevent the one tap from loading and the google login from showing when you're logged in. Also, a logout endpoint needs to be created and connected to the ui. Should I add those changes to this pr or leave it for another one?

Sorry about the extraneous logs btw. I'll handle those soon.

@timmyichen

Copy link
Copy Markdown
Contributor

Thanks for taking on this monster of a task! I left a few comments/questions. There are quite a few todos in comments - is this PR complete or did I jump the gun on reviewing?

@timmyichen It's almost there! The ui still needs some logic to prevent the one tap from loading and the google login from showing when you're logged in. Also, a logout endpoint needs to be created and connected to the ui. Should I add those changes to this pr or leave it for another one?

Sorry about the extraneous logs btw. I'll handle those soon.

No worries! Same PR makes sense, given it's a related feature and logging out shouldn't be that complex using passport (i think it's just req.logout()). If you want to kick it back into draft mode until it's ready to be reviewed, you can do so under the reviewers - that way you don't get me commenting on a bunch of console logs 😉

image

@pdotsani
pdotsani marked this pull request as draft October 15, 2020 03:19
@pdotsani
pdotsani force-pushed the feature/passport-google-auth0 branch 2 times, most recently from f8e3ea8 to a5a9e0a Compare October 21, 2020 02:40
@pdotsani
pdotsani marked this pull request as ready for review October 21, 2020 02:44

@timmyichen timmyichen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for taking this on! I left a few comments - there are a few older unaddressed comments as well. Please ping me if you have any questions.

const responseBody = await response.json();
const { isAuth } = responseBody;

if (!isAuth) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isAuth will always be false in this hook because it's initialized to be false, and the hook only runs once on mount.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isAuth is refering to the api call in the func.

Comment on lines +106 to +118
if (headerLink.name === 'google' && !isAuth) {
return (
<Grid item key={headerLink.name}>
<a className={styles.link} href={headerLink.href}>
{headerLink.label}
</a>
</Grid>
);
} else if (headerLink.name === 'google' && isAuth) {
return null;
}

if (headerLink.name === 'logout' && isAuth) {
return (
<Grid item key={headerLink.name}>
<a className={styles.link} href={headerLink.href}>
{headerLink.label}
</a>
</Grid>
);
} else if (headerLink.name === 'logout' && !isAuth) {
return null;
}

if (headerLink.name === 'login' && isAuth) {
return null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be rewritten to be easier to understand - maybe something like the following, outside of the .map call:

const links = headerLinks;

if (isAuth) {
  links.push(logout button config object);
} else {
  links.push(login button config object);
}

links.map(link => etc etc

name: 'google',
label: 'google',
href: 'http://localhost:5000/auth/google/',
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment still stands^

Comment thread client/src/constants/Header.ts Outdated
{
name: 'google',
label: 'google',
href: 'http://localhost:5000/auth/google/',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't hardcode localhost:5000

Comment thread db/migrations/1575817780103-User.ts Outdated
Comment on lines +14 to +16
"google_id" character varying DEFAULT NULL,
"google_picture" character varying DEFAULT NULL,
"password_digest" character varying,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't edit existing migrations - if you're changing a model, create a new migration.

Comment thread server/app.ts Outdated

export const main = async (app: Express) => {
await initDB();
app.use(session({ secret: 'secret' }));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumping this comment

Comment thread server/auth/google/index.ts Outdated

const router = express.Router();

router

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in other files we are not chaining multiple routes together - each get request here should be its own statement

Comment thread server/auth/google/index.ts Outdated
(req, res) => {
console.log('GOTHERE! ');
console.log('req.user ', req.user);
res.redirect('http://localhost:3000/');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump

Comment thread server/auth/google/passport.ts Outdated
done(null, user.id);
});

passport.deserializeUser(async (userId: any, done) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we know userId will be a number, so we can type it as such here

@allella

allella commented Oct 24, 2020

Copy link
Copy Markdown
Contributor

@all-contributors please add @pdotsani for code

@allcontributors

Copy link
Copy Markdown
Contributor

@allella

I've put up a pull request to add @pdotsani! 🎉

@pdotsani
pdotsani force-pushed the feature/passport-google-auth0 branch from 46bea20 to 026ea3d Compare October 31, 2020 02:20
@pdotsani
pdotsani force-pushed the feature/passport-google-auth0 branch from f60e5cd to 7fe4410 Compare October 31, 2020 02:56
@pdotsani
pdotsani force-pushed the feature/passport-google-auth0 branch from b3e4641 to 5362e0e Compare October 31, 2020 03:02
@allella

allella commented Nov 7, 2020

Copy link
Copy Markdown
Contributor

Today's meeting included a good deal of conversation about how Google Auth would limit use cases in countries like China or even certain government agencies.

So, we left the conversation leaning very heavily to going back to an email solution. There are some contributors like @timmyichen @vkWeb @AryanJ-NYC from #306 who were part of previous authentication conversations who weren't part of today's conversation, so please see the meeting notes.

If Google Auth is off the table, we'd need to at least :

@allella

allella commented Nov 7, 2020

Copy link
Copy Markdown
Contributor

@QuincyLarson @pdotsani @Zeko369 is my last comment and link to the meeting notes an accurate summary of today's conversation?

@allella

allella commented Nov 11, 2020

Copy link
Copy Markdown
Contributor

Closing since the direction changed to using email-based authentication on #437.

@allella allella closed this Nov 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

As a user I can log in or register via Google OAuth 2.0 authentication.

4 participants