@@ -3,7 +3,11 @@ import { AUTH_CONFIG } from './auth0-variables';
33import history from '../history' ;
44
55export default class Auth {
6+ accessToken ;
7+ idToken ;
8+ expiresAt ;
69 userProfile ;
10+ scopes ;
711 requestedScopes = 'openid profile read:messages write:messages' ;
812
913 auth0 = new auth0 . WebAuth ( {
@@ -22,6 +26,8 @@ export default class Auth {
2226 this . isAuthenticated = this . isAuthenticated . bind ( this ) ;
2327 this . userHasScopes = this . userHasScopes . bind ( this ) ;
2428 this . getAccessToken = this . getAccessToken . bind ( this ) ;
29+ this . getIdToken = this . getIdToken . bind ( this ) ;
30+ this . renewSession = this . renewSession . bind ( this ) ;
2531 this . getProfile = this . getProfile . bind ( this ) ;
2632 }
2733
@@ -33,7 +39,6 @@ export default class Auth {
3339 this . auth0 . parseHash ( ( err , authResult ) => {
3440 if ( authResult && authResult . accessToken && authResult . idToken ) {
3541 this . setSession ( authResult ) ;
36- history . replace ( '/home' ) ;
3742 } else if ( err ) {
3843 history . replace ( '/home' ) ;
3944 console . log ( err ) ;
@@ -42,36 +47,46 @@ export default class Auth {
4247 } ) ;
4348 }
4449
50+ getAccessToken ( ) {
51+ return this . accessToken ;
52+ }
53+
54+ getIdToken ( ) {
55+ return this . idToken ;
56+ }
57+
4558 setSession ( authResult ) {
59+ // Set isLoggedIn flag in localStorage
60+ localStorage . setItem ( 'isLoggedIn' , 'true' ) ;
61+
4662 // Set the time that the access token will expire at
47- let expiresAt = JSON . stringify (
48- authResult . expiresIn * 1000 + new Date ( ) . getTime ( )
49- ) ;
50- // If there is a value on the `scope` param from the authResult,
51- // use it to set scopes in the session for the user. Otherwise
52- // use the scopes as requested. If no scopes were requested,
53- // set it to nothing
54- const scopes = authResult . scope || this . requestedScopes || '' ;
55-
56- localStorage . setItem ( 'access_token' , authResult . accessToken ) ;
57- localStorage . setItem ( 'id_token' , authResult . idToken ) ;
58- localStorage . setItem ( 'expires_at' , expiresAt ) ;
59- localStorage . setItem ( 'scopes' , JSON . stringify ( scopes ) ) ;
63+ let expiresAt = ( authResult . expiresIn * 1000 ) + new Date ( ) . getTime ( ) ;
64+ this . accessToken = authResult . accessToken ;
65+ this . idToken = authResult . idToken ;
66+ this . expiresAt = expiresAt ;
67+
68+ // Set the users scopes
69+ this . scopes = authResult . scope || this . requestedScopes || '' ;
70+
6071 // navigate to the home route
6172 history . replace ( '/home' ) ;
6273 }
6374
64- getAccessToken ( ) {
65- const accessToken = localStorage . getItem ( 'access_token' ) ;
66- if ( ! accessToken ) {
67- throw new Error ( 'No access token found' ) ;
68- }
69- return accessToken ;
75+ renewSession ( ) {
76+ this . auth0 . checkSession ( { } , ( err , authResult ) => {
77+ console . log ( err , authResult ) ;
78+ if ( authResult && authResult . accessToken && authResult . idToken ) {
79+ this . setSession ( authResult ) ;
80+ } else if ( err ) {
81+ this . logout ( ) ;
82+ console . log ( err ) ;
83+ alert ( `Could not get a new token (${ err . error } : ${ err . error_description } ).` ) ;
84+ }
85+ } ) ;
7086 }
7187
7288 getProfile ( cb ) {
73- let accessToken = this . getAccessToken ( ) ;
74- this . auth0 . client . userInfo ( accessToken , ( err , profile ) => {
89+ this . auth0 . client . userInfo ( this . accessToken , ( err , profile ) => {
7590 if ( profile ) {
7691 this . userProfile = profile ;
7792 }
@@ -80,25 +95,33 @@ export default class Auth {
8095 }
8196
8297 logout ( ) {
83- // Clear access token and ID token from local storage
84- localStorage . removeItem ( 'access_token' ) ;
85- localStorage . removeItem ( 'id_token' ) ;
86- localStorage . removeItem ( 'expires_at' ) ;
87- localStorage . removeItem ( 'scopes' ) ;
98+ // Remove tokens and expiry time
99+ this . accessToken = null ;
100+ this . idToken = null ;
101+ this . expiresAt = 0 ;
102+
103+ // Remove user scopes
104+ this . scopes = null ;
105+
106+ // Remove user profile
88107 this . userProfile = null ;
108+
109+ // Remove isLoggedIn flag from localStorage
110+ localStorage . removeItem ( 'isLoggedIn' ) ;
111+
89112 // navigate to the home route
90113 history . replace ( '/home' ) ;
91114 }
92115
93116 isAuthenticated ( ) {
94117 // Check whether the current time is past the
95118 // access token's expiry time
96- let expiresAt = JSON . parse ( localStorage . getItem ( 'expires_at' ) ) ;
119+ let expiresAt = this . expiresAt ;
97120 return new Date ( ) . getTime ( ) < expiresAt ;
98121 }
99122
100123 userHasScopes ( scopes ) {
101- const grantedScopes = ( JSON . parse ( localStorage . getItem ( ' scopes' ) ) || '' ) . split ( ' ' ) ;
124+ const grantedScopes = this . scopes . split ( ' ' ) ;
102125 return scopes . every ( scope => grantedScopes . includes ( scope ) ) ;
103126 }
104127}
0 commit comments