Last active
March 2, 2017 14:15
-
-
Save brunokrebs/00f97fbf8bbf64e01050bfa6a7f1b120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import Auth0Lock from 'auth0-lock'; | |
import { tokenNotExpired } from 'angular2-jwt'; | |
// FIXME: replace these with your own Auth0 'Client ID' and 'Domain' | |
const AUTH0_CLIENT_ID = 'YOUR_AUTH0_CLIENT_ID'; | |
const AUTH0_DOMAIN = 'YOUR_AUTH0_DOMAIN'; | |
// this is the key to the JWT in the browser localStorage | |
const ID_TOKEN = 'id_token'; | |
@Injectable() | |
export class AuthService { | |
lock = new Auth0Lock(AUTH0_CLIENT_ID, AUTH0_DOMAIN, {}); | |
constructor() { | |
// listening to 'authenticated' events | |
this.lock.on('authenticated', (authResult) => { | |
localStorage.setItem(ID_TOKEN, authResult.idToken); | |
}); | |
} | |
signIn() { this.lock.show(); } | |
signOut() { localStorage.removeItem(ID_TOKEN); } | |
authenticated() { return tokenNotExpired(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment