OAuth 2.0 is a very useful authorization service that provides authorization for web, mobile and desktop apps. Using OAuth we can easily implement the facebook, twitter , google sign in feature to our express node app.
OAuth 2.0 works by delegating the authentication process to the service that hosts the user account. Once the authentication is implemented. The app users can easily sign in with their google accounts, FB account, etc. It greatly saves a lot of time for the user.
Authenticating with Google using the OAuth 2.0 API.
Install OAuth 2.0
npm install passport-google-oauth20
- Create An application in Google Developers Console
- Click create new app, give the app name
- Now go to OAuth consent screen
- Give App name, Email
- New Configure scopes: select name, email, openid
- If we want more like youtube, maps data of the user the select Google API Library link and find those.
Now go to Credentials Menu
Click on create credentials
- Select OAuth client ID
- Select App type (here we are selecting web app)
- Give name, URLs (request and redirect URLs)
request origin url: http://localhost:3000
redirect url: http://localhost:3000/auth/google/secrets
After filling the necessary fields –> Click Create
- Now we will get Client Id and secret
Copy the CLIENT_ID , CLIENT_SECRET to the .env file of the Node APP Project.
npm i mongoose-findorcreate