-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathauth.service.ts
46 lines (35 loc) · 1.08 KB
/
auth.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
import { Router } from '@angular/router';
declare var Auth0Lock: any;
@Injectable()
export class AuthService {
lock = new Auth0Lock('YOUR-AUTH0-CLIENT-ID', 'YOUR-AUTH0-DOMAIN.auth0.com');
constructor(private router: Router) {
this.lock.on('authenticated', (authResult: any) => {
localStorage.setItem('id_token', authResult.idToken);
this.lock.getProfile(authResult.idToken, (error: any, profile: any) => {
if (error) {
console.log(error);
}
localStorage.setItem('profile', JSON.stringify(profile));
this.router.navigateByUrl('/home');
});
this.lock.hide();
});
}
login() {
this.lock.show();
}
logout() {
// To log out, just remove the token and profile
// from local storage
localStorage.removeItem('profile');
localStorage.removeItem('id_token');
// Send the user back to the dashboard after logout
this.router.navigateByUrl('/login');
}
loggedIn() {
return tokenNotExpired();
}
}