Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gasparrobi/node-google-oauth2-jwt
sample nodejs api with mongodb and passportjs authentication, (google-oauth2, jwt)
https://github.com/gasparrobi/node-google-oauth2-jwt
es6 express-js expressjs jwt mongodb mongoose nodejs passport-google passportjs
Last synced: 2 months ago
JSON representation
sample nodejs api with mongodb and passportjs authentication, (google-oauth2, jwt)
- Host: GitHub
- URL: https://github.com/gasparrobi/node-google-oauth2-jwt
- Owner: gasparrobi
- Created: 2018-01-26T21:32:13.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-02-08T07:31:56.000Z (almost 5 years ago)
- Last Synced: 2024-10-29T23:14:51.047Z (3 months ago)
- Topics: es6, express-js, expressjs, jwt, mongodb, mongoose, nodejs, passport-google, passportjs
- Language: JavaScript
- Homepage:
- Size: 47.9 KB
- Stars: 46
- Watchers: 5
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-google-oauth2-jwt
| nodejs | express | mongodb | passport | jwt | oauth2 | googleAuth |
| :------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------: |
| | | | | | | |sample nodejs api with mongodb database and passportjs for authentication using passport-google-oauth20 and jwt tokens.
## Prerequisites
- Obtain credentials from google. [Follow these instructions if you need help](https://youtu.be/9x66l93iEW0?t=1m4s)
- You will also need a mongoDB database to save users. [Instructions for a free mlab database ](https://youtu.be/ySFXduSdpxs)
- Add google api key and mongodb uri in `.env` file.
```bash
touch .env
``````
CLIENT_ID=''
CLIENT_SECRET=''
MONGO_URI=''
``````javascript
npm install
``````javascript
npm start
```## How it works
Use [Postman](https://www.getpostman.com/) 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.
![](https://github.com/gasparrobi/node-google-oauth2-jwt/blob/master/postman.png?raw=true)
## Useful beginner resources
[mongoDB youtube tutorial](https://youtu.be/pWbMrx5rVBE)
[mongoose docs](http://mongoosejs.com/docs/)
[passportjs-google-oauth2-strategy youtube tutorial](https://www.youtube.com/watch?v=sakQbeRjgwg&list=PL4cUxeGkcC9jdm7QX143aMLAqyM-jTZ2x)
[passport-jwt youtube tutorial](https://youtu.be/7nafaH9SddU)