5.20203.748
wijmo.cloud Class OAuth2 Wijmo API Class

OAuth2 Class

Provides a simple way to use Google's GAPI OAuth functionality.

To use, create an instance of the OAuth2 class and add a handler to the userChanged event to monitor the {@link @user} property, which is non-null if a user is currently signed in.

Use the signIn and signOut methods to sign in or out of the Google account.

For example, the code below creates an OAuth2 object and uses it to manage a button used to log users in and out of the application:

import { OAuth2 } from '@grapecity/wijmo.cloud';

// create OAuth2 object
const API_KEY = 'XXXX';
const CLIENT_ID = 'YYYY.apps.googleusercontent.com';
const SCOPES = [ 'https://www.googleapis.com/auth/userinfo.email' ];
const auth = new OAuth2(API_KEY, CLIENT_ID, SCOPES);

// click a button to log in/out
let oAuthBtn = document.getElementById('auth_btn');
oAuthBtn.addEventListener('click', () => {
    if (auth.user) {
        auth.signOut();
    } else {
        auth.signIn();
    }
});

// update button caption and accessToken when user changes
auth.userChanged.addHandler(s => {
    let user = s.user;
    oAuthBtn.textContent = user ? 'Sign Out' : 'Sign In';
    gsNWind.accessToken = user ? s.accessToken : null;
    fsNWind.accessToken = user ? s.accessToken : null;
});

Heirarchy

  • OAuth2

Constructors

constructor

  • new OAuth2(apiKey: string, clientId: string, scopes?: string[], options?: any): OAuth2

Properties

accessToken

accessToken: string

Gets an OAuth access token that can be used to perform authorized requests.

idToken

idToken: string

Gets an OAuth id token that can be passed to the Firebase client libraries.

See https://firebase.google.com/docs/auth/web/google-signin

let credential = firebase.auth.GoogleAuthProvider.credential(id_token);
firebase.auth().signInWithCredential(credential);

user

user: IUser

Gets an object with information about the current user (or null if the user is not signed-in).

Methods

onError

onUserChanged

signIn

  • signIn(): void
  • Signs a user in.

    Returns void

signOut

  • signOut(): void
  • Signs a user out.

    Returns void

Events

error

Occurs when an error happens.

userChanged

userChanged: Event<OAuth2, EventArgs>

Occurs when a user signs in or out.