@@ -15,15 +15,27 @@ export class AuthService {
15
15
@Inject ( KEYCLOAK_LOGIN_OPTIONS ) @Optional ( ) private _loginOptions : KeycloakLoginOptions = { }
16
16
) { }
17
17
18
+ /**
19
+ * Redirects to login form.
20
+ * @param options Login options.
21
+ */
18
22
login ( options ?: KeycloakLoginOptions ) : Promise < void > {
19
23
const loginOptions = Object . assign ( { } , this . _loginOptions , options ) ;
20
24
return this . _toNativePromise ( this . keycloak . login ( loginOptions ) ) ;
21
25
}
22
26
27
+ /**
28
+ * Redirects to logout.
29
+ * @param options Logout options.
30
+ * @param options.redirectUri Specifies the uri to redirect to after logout.
31
+ */
23
32
logout ( options ?: any ) : Promise < void > {
24
33
return this . _toNativePromise ( this . keycloak . logout ( options ) ) ;
25
34
}
26
35
36
+ /**
37
+ * Is true if the user is authenticated, false otherwise.
38
+ */
27
39
authenticated ( ) : boolean {
28
40
return this . keycloak . authenticated ;
29
41
}
@@ -37,28 +49,36 @@ export class AuthService {
37
49
return this . _toNativePromise ( this . keycloak . updateToken ( minValidity ) ) ;
38
50
}
39
51
52
+ /**
53
+ * Returns the current token.
54
+ */
40
55
getToken ( ) : string {
41
56
return this . keycloak . token ;
42
57
}
43
58
59
+ /**
60
+ * Returns an instance of HttpHeaders with the Authorization entry
61
+ * or an empty instance of HttpHeaders, if the token is not available.
62
+ */
44
63
getAuthHeader ( ) : HttpHeaders {
45
64
const authToken = this . getToken ( ) ;
46
- return new HttpHeaders ( ) . set ( 'Authorization' , `Bearer ${ authToken } ` ) ;
65
+ return authToken
66
+ ? new HttpHeaders ( ) . set ( 'Authorization' , `Bearer ${ authToken } ` )
67
+ : new HttpHeaders ( ) ;
47
68
}
48
69
70
+ /**
71
+ * Returns or loads the user profile information.
72
+ * If no user is authenticated, returns an observable of undefined.
73
+ */
49
74
getUserInfo ( ) : Observable < KeycloakProfile | undefined > {
50
- if ( ! this . authenticated ( ) || this . keycloak . profile ) {
75
+ if ( ! this . authenticated ( ) ) {
76
+ return of ( undefined ) ;
77
+ } else if ( this . keycloak . profile ) {
51
78
return of ( this . keycloak . profile ) ;
79
+ } else {
80
+ return from ( this . _toNativePromise ( this . keycloak . loadUserProfile ( ) ) ) ;
52
81
}
53
-
54
- return from (
55
- new Promise ( ( resolve , reject ) => {
56
- this . keycloak
57
- . loadUserProfile ( )
58
- . success ( resolve )
59
- . error ( err => reject ( err ) ) ;
60
- } )
61
- ) ;
62
82
}
63
83
64
84
private _toNativePromise < TSuccess , TError > (
0 commit comments