Skip to content
Robert Gaspar edited this page Jan 27, 2018 · 3 revisions

node-google-oauth2-jwt

sample nodejs api with mongodb and passportjs authentication, (google-oauth2, jwt)

Prerequisites

module.exports = {
    google: {
        clientID: '',
        clientSecret: ''
    },
    mongo: {
        mongodbUri: ''
    }
};
npm install
npm start

How it works

Use Postman or any other api testing tool for the following section.

First we are going to make a GET request to our google authentication endpoint /auth/google which will redirect the user to the google login page. After google successfully authenticates the user it will redirect to our callback endpoint with the user's profile information such as googleId, email etc... now we can save the user in the database or retrieve existing user info and redirect to our auth-success endpoint /auth-success where we issue a jwt token.

We can verify the token validity by making a GET request to the /verify endpoint. Don't forget to add the attach the authorization header to the request.

Useful beginner resources

mongoDB youtube tutorial

mongoose docs

passportjs-google-oauth2-strategy youtube tutorial

passport-jwt youtube tutorial

Clone this wiki locally