How to use @auth0/auth0-spa-js - 10 common examples

To help you get started, we’ve selected a few @auth0/auth0-spa-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github auth0-samples / auth0-angular-samples / 02-Calling-an-API / src / app / auth / auth.service.ts View on Github external
import { Injectable } from '@angular/core';
import createAuth0Client from '@auth0/auth0-spa-js';
import Auth0Client from '@auth0/auth0-spa-js/dist/typings/Auth0Client';
import * as config from '../../../auth_config.json';
import { from, of, Observable, BehaviorSubject, combineLatest, throwError } from 'rxjs';
import { tap, catchError, concatMap, shareReplay } from 'rxjs/operators';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  // Create an observable of Auth0 instance of client
  auth0Client$ = (from(
    createAuth0Client({
      domain: config.domain,
      client_id: config.clientId,
      redirect_uri: `${window.location.origin}`,
      audience: config.audience
    })
  ) as Observable).pipe(
    shareReplay(1), // Every subscription receives the same shared value
    catchError(err => throwError(err))
  );
  // Define observables for SDK methods that return promises by default
  // For each Auth0 SDK method, first ensure the client instance is ready
  // concatMap: Using the client instance, call SDK method; SDK returns a promise
  // from: Convert that resulting promise into an observable
  isAuthenticated$ = this.auth0Client$.pipe(
    concatMap((client: Auth0Client) => from(client.isAuthenticated())),
    tap(res => this.loggedIn = res)
github auth0-samples / auth0-angular-samples / 01-Login / src / app / auth / auth.service.ts View on Github external
import { Injectable } from '@angular/core';
import createAuth0Client from '@auth0/auth0-spa-js';
import Auth0Client from '@auth0/auth0-spa-js/dist/typings/Auth0Client';
import * as config from '../../../auth_config.json';
import { from, of, Observable, BehaviorSubject, combineLatest, throwError } from 'rxjs';
import { tap, catchError, concatMap, shareReplay } from 'rxjs/operators';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  // Create an observable of Auth0 instance of client
  auth0Client$ = (from(
    createAuth0Client({
      domain: config.domain,
      client_id: config.clientId,
      redirect_uri: `${window.location.origin}`
    })
  ) as Observable).pipe(
    shareReplay(1), // Every subscription receives the same shared value
    catchError(err => throwError(err))
  );
  // Define observables for SDK methods that return promises by default
  // For each Auth0 SDK method, first ensure the client instance is ready
  // concatMap: Using the client instance, call SDK method; SDK returns a promise
  // from: Convert that resulting promise into an observable
  isAuthenticated$ = this.auth0Client$.pipe(
    concatMap((client: Auth0Client) => from(client.isAuthenticated())),
    tap(res => this.loggedIn = res)
  );
github TalesMUD / talesmud / public / app / src / auth.js View on Github external
onMount(async () => {
    auth0 = await createAuth0Client(config);

    // Not all browsers support this, please program defensively!
    const params = new URLSearchParams(window.location.search);

    // Check if something went wrong during login redirect
    // and extract the error message
    if (params.has("error")) {
      authError.set(new Error(params.get("error_description")));
    }

    // if code then login success
    if (params.has("code")) {
      // Let the Auth0 SDK do it's stuff - save some state, etc.
      await auth0.handleRedirectCallback();
      // Can be smart here and redirect to original path instead of root
      window.history.replaceState({}, document.title, "/");
github sandrinodimattia / use-auth0-hooks / src / provider.tsx View on Github external
const initAuth0 = async (): Promise => {
      try {
        // Get the client.
        const initializedClient = await createClient(options);
        setClient(initializedClient);

        // If the user was redirect from Auth0, we need to handle the exchange or throw an error.
        if (window.location.search.includes('state=') || (window.location.search.includes('error=')
          || window.location.search.includes('code='))) {
          const { appState } = await initializedClient.handleRedirectCallback();
          redirectAfterLogin(appState, onRedirectCallback);
        }

        // Authentication success.
        const user = await initializedClient.getUser();
        setState({
          ...initialState,
          user,
          isLoading: false,
          isAuthenticated: !!user
github auth0-samples / auth0-angular-samples / 03-Calling-an-API / src / app / auth / auth.service.ts View on Github external
async getAuth0Client(): Promise {
    if (!this.auth0Client) {
      this.auth0Client = await createAuth0Client({
        domain: config.domain,
        client_id: config.clientId,
        audience: config.audience
      });

      try {
        this.token.next(await this.auth0Client.getTokenSilently());

        this.isAuthenticated.next(await this.auth0Client.isAuthenticated());

        this.isAuthenticated.subscribe(async isAuthenticated => {
          if (isAuthenticated) {
            return this.profile.next(await this.auth0Client.getUser());
          }

          this.profile.next(null);
github taniarascia / takenote / src / auth / index.tsx View on Github external
const initAuth0 = async () => {
      const auth0FromHook = await createAuth0Client(initOptions)
      setAuth0(auth0FromHook)

      if (window.location.search.includes('code=')) {
        const { appState } = await auth0FromHook.handleRedirectCallback()
        onRedirectCallback(appState)
      }

      const isAuthenticated = await auth0FromHook.isAuthenticated()

      setIsAuthenticated(isAuthenticated)

      if (isAuthenticated) {
        const user = await auth0FromHook.getUser()
        setUser(user)
      }
github auth0-samples / auth0-angular-samples / 01-Login / src / app / auth / auth.service.ts View on Github external
async getAuth0Client(): Promise {
    if (!this.auth0Client) {
      this.auth0Client = await createAuth0Client({
        domain: config.domain,
        client_id: config.clientId
      });

      try {
        await this.auth0Client.getTokenSilently();

        this.isAuthenticated.next(await this.auth0Client.isAuthenticated());

        this.isAuthenticated.subscribe(async isAuthenticated => {
          if (isAuthenticated) {
            return this.profile.next(await this.auth0Client.getUser());
          }

          this.profile.next(null);
        });
github auth0-samples / auth0-react-samples / 01-Login / src / react-auth0-spa.js View on Github external
const initAuth0 = async () => {
      const auth0FromHook = await createAuth0Client(initOptions);
      setAuth0(auth0FromHook);

      if (window.location.search.includes("code=")) {
        const { appState } = await auth0FromHook.handleRedirectCallback();
        onRedirectCallback(appState);
      }

      const isAuthenticated = await auth0FromHook.isAuthenticated();

      setIsAuthenticated(isAuthenticated);

      if (isAuthenticated) {
        const user = await auth0FromHook.getUser();
        setUser(user);
      }
github Lambda-School-Labs / designhub-fe / client / src / auth-wrapper.js View on Github external
const initAuth0 = async () => {
      const auth0FromHook = await createAuth0Client(initOptions);
      setAuth0(auth0FromHook);

      if (window.location.search.includes('code=')) {
        const { appState } = await auth0FromHook.handleRedirectCallback();
        onRedirectCallback(appState);
      }

      const isAuthenticated = await auth0FromHook.isAuthenticated();
      setIsAuthenticated(isAuthenticated);

      if (isAuthenticated) {
        const user = await auth0FromHook.getUser();
        const token = await auth0FromHook.getTokenSilently();

        localStorage.setItem('token', token);
github auth0-samples / auth0-react-samples / 02-Calling-an-API / src / react-auth0-spa.js View on Github external
const initAuth0 = async () => {
      const auth0FromHook = await createAuth0Client(initOptions);
      setAuth0(auth0FromHook);

      if (window.location.search.includes("code=")) {
        const { appState } = await auth0FromHook.handleRedirectCallback();
        onRedirectCallback(appState);
      }

      const isAuthenticated = await auth0FromHook.isAuthenticated();

      setIsAuthenticated(isAuthenticated);

      if (isAuthenticated) {
        const user = await auth0FromHook.getUser();
        setUser(user);
      }

@auth0/auth0-spa-js

Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE

MIT
Latest version published 1 year ago

Package Health Score

77 / 100
Full package analysis

Popular @auth0/auth0-spa-js functions