Giter Site home page Giter Site logo

ktaka-ccmp / google-oauth2-example Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 1.04 MB

Example implementation for Sign-in with Google

Python 55.43% HTML 10.00% JavaScript 23.46% Shell 1.73% Svelte 9.38%
django-rest-framework fastapi reactjs sign-in-with-google sveltejs

google-oauth2-example's Introduction

Sign in with Google Example

This repository provides example app implementations using Google OAuth as the authentication mechanism.

The frontend app is implemented using the following technologies:

  • React.js
  • Svelte.js

The backend API servers are implemented using the following technologies:

  • Django REST framework(DRF)
  • Fastapi

The noauth directory contains example apps without authentication.

noauth/
├── Howto.md
├── Readme.md
├── backend-django
├── backend-fastapi
├── frontend-react
└── frontend-svelte

The google-oauth directory contains example apps using Google OAuth as the authentication mechanism.

google-oauth/
├── Detailed.md
├── Readme.md
├── ReadmeSSL.md
├── backend-django
├── backend-fastapi
├── frontend-react01
├── frontend-react02
└── frontend-svelte

Sessions

As for maintaining sessions, I implemented the backend API servers so that they set session cookies after verification of the ID token provided by Google.

The authentication follows the following steps:

  1. A user visits a restricted page.
  2. The frontend app redirects the user to the login page.
  3. The user authenticates them at the Google OAuth endpoint. Google OAuth API returns an ID token as a JSON Web Token(JWT).
  4. The frontend app receives the JWT from Google and sends it to the backend API servers.
  5. The backend API servers verify the ID token's signature using a Google public certificate and trust the user's information in the payload.
  6. The backend API servers create the user if it does not exist in the database and return the user's information while setting a new session cookie in the response header.
  7. The frontend app regards the user as authenticated and sets a property to always send a request with the session cookie in the following API communications.
  8. The backend API servers allow what is allowed for the user as long as the session is valid.

Many examples on the Internet use JWT access tokens provided by Google or the backend API server as they are. However, there are arguments that JWT tokens should not be used for sessions. Therefore, the apps in this repository use an auth backend using the "battle-tested" traditional session cookie mechanism.

How the example apps look like

How the app works without authentication

The app in this repository consists of two pages, "Top" and "Customer."

Top no-auth

Customer no-auth

The Customer page, which fetches the data from the API server, is not restricted. Therefore the page can be seen by unauthenticated users.

How the app works with authentication

With authentication implemented, anonymous users' access is redirected to the login page, where they can sign in with their Google account.

Login page

The Customer page can only be seen after successful authentication.

Customer page for authenticated users

Please note that the authentication mechanism protects the Customer page and prevents the React.js frontend app from fetching the data from the API server.

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.