Giter Club home page Giter Club logo

javascript-auth0-oidc-sso-sample's Introduction

OIDC Single Sign-on Sample

Prerequisites

  1. Go to https://manage.auth0.com/#/clients and create a single-page application client.
  2. Add http://localhost:5000 as allowed callback URLs and as a Web Origin.
  3. Enter your Auth0 domain and client ID in the auth0-variables.js file.

Running the sample

To start the sample, start a web server in the root of this repository at port 5000, or just run:

npm install -g serve
serve

and then browse to http://localhost:5000.

How this sample works

We are checking two things in local storage to detect if a user is logged in or not:

  • accessToken: the Auth0 access token to access our desired API, which in this case is /userinfo
  • expirationDate: the expiration date of this token, calculated using the expires_in response parameter after a successful authentication

If there is no valid access token present in local storage, the user is not logged in to our application, but they might have logged in via SSO to another application.

We can detect if this is the case by calling the checkSession method of auth0.js, which will attempt to silently authenticate the user inside an iframe.

Silent authentication works in the same way as regular authentication (i.e. redirects the user to /authorize), but with some differences (all of this is done by the checkSession function):

  • A prompt=none parameter is added to the redirect to /authorize. This causes Auth0 to return an error in case the user is not logged in via SSO instead of displaying a login page.
  • The redirect happens inside an iframe to preserve our application's state.

Two things can happen after a silent authentication request:

  • The user did not have an active SSO session, so Auth0 calls back with an error (error=login_required)
  • The user did have an active SSO session, so Auth0 calls back with an access token (access_token=...&expires_in=...)

In this sample we are immediately redirecting the user to the login page by using the authorize method of auth0.js if silent authentication failed, or storing the token and its expiration date in local storage if it succeeded.

javascript-auth0-oidc-sso-sample's People

Contributors

aaguiarz avatar chenkie avatar

Watchers

 avatar  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.