11import { createPKCECodes , PKCECodePair } from './pkce'
22import { toUrlEncoded } from './util'
33
4+ import jwtDecode from 'jwt-decode'
5+
46export interface AuthServiceProps {
57 clientId : string
68 clientSecret ?: string
@@ -12,11 +14,18 @@ export interface AuthServiceProps {
1214}
1315
1416export interface AuthTokens {
15- idToken : string
16- accessToken : string
17- refreshToken : string
18- expiresIn : number
19- tokenType : string
17+ id_token : string
18+ access_token : string
19+ refresh_token : string
20+ expires_in : number
21+ token_type : string
22+ }
23+
24+ export interface JWTIDToken {
25+ given_name : string
26+ family_name : string
27+ name : string
28+ email : string
2029}
2130
2231export class AuthService {
@@ -26,6 +35,13 @@ export class AuthService {
2635 this . props = props
2736 }
2837
38+ getUser ( ) : { } {
39+ const t = this . getAuthTokens ( )
40+ if ( null === t ) return { }
41+ const decoded = jwtDecode ( t . id_token ) as { }
42+ return decoded
43+ }
44+
2945 getCodeFromLocation ( location : Location ) : string | null {
3046 const split = location . toString ( ) . split ( '?' )
3147 if ( split . length < 2 ) {
@@ -93,16 +109,20 @@ export class AuthService {
93109 return window . localStorage . getItem ( 'auth' ) !== null
94110 }
95111
96- logout ( ) : void {
112+ async logout ( ) : Promise < void > {
97113 const { location } = this . props
98114 this . removeItem ( 'pkce' )
99115 this . removeItem ( 'auth' )
100116 location . reload ( )
101117 }
102118
119+ async login ( ) : Promise < void > {
120+ this . authorize ( )
121+ }
122+
103123 // this will do a full page reload and to to the OAuth2 provider's login page and then redirect back to redirectUri
104124 authorize ( ) : void {
105- const { clientId, location , provider, redirectUri, scopes } = this . props
125+ const { clientId, provider, redirectUri, scopes } = this . props
106126
107127 const pkce = createPKCECodes ( )
108128 window . localStorage . setItem ( 'pkce' , JSON . stringify ( pkce ) )
@@ -119,7 +139,7 @@ export class AuthService {
119139 }
120140 // Responds with a 302 redirect
121141 const url = `${ provider } /authorize?${ toUrlEncoded ( query ) } `
122- location . replace ( url )
142+ window . location . href = url
123143 }
124144
125145 // this happens after a full page reload. Read the code from localstorage
0 commit comments