Git Product home page Git Product logo

mern-google-login's Introduction

mern-google-login

Authentication flow for MERN stack application using google OAuth 2.0.

This oauth flow is more secure and you don't have to reload your react app and go through multiple redirections to authenticate client.

App flow

Untitled Diagram

And the image blow is from google docs

server_side_code_flow

Installation

1. Clone the repository & install dependencies

git clone https://github.com/Shahzayb/mern-google-login.git

cd mern-google-login

npm install

cd client & npm install

2. Obtain OAuth 2.0 credentials from the Google API Console.

Visit the Google API Console to obtain OAuth 2.0 credentials such as a client ID and client secret that are known to both Google and your application. Don't store your client secret in your React app. You only need client id in your react app to get the authorization grant code. And you should store client secret in node app.

3. Add Authorized JavaScript Origins

Go to Google API Console. And select your project & then add JavaScript Origins Url Capture

This is a url of client side react app

You'll notice in my code that I set the redirect_uri to postmessage, its because we need the authorization grant code without any redirection

4. Client Side Setup

  1. execute these commands
cd client
touch .env.local
  1. open .env.local file & enter REACT_APP_GOOGLE_CLIENT_ID=your client id provided by google

5. Server Side Environment Variables Setup

From the root directory of the project, type:

mkdir config

cd config

touch dev.env

Note: These commands are for Mac/Linux users only. Windows users should type these commands in git bash

Now open the dev.env file and add GOOGLE_CLIENT_ID=value from google apis, GOOGLE_CLIENT_SECRET=value from google apis

6. Run 'dev' script

npm run dev

More information

if you want more information, then please read the one-time-code flow implementation

Contributing

Please help me with the documentation and improve the code. I'll appreciate that. For changes, please open an issue first to discuss what you would like to change.

License

MIT

mern-google-login's People

Contributors

dependabot[bot] avatar shahzayb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.