11import auth0 , { WebAuth } from 'auth0-js'
2- import { EventEmitter } from '../models/EventEmmiter'
2+ import { EventEmitter } from '../models/EventEmmiter'
33import Router from '../router/router'
4+ import { ApiService } from '../models/ApiService' ;
45
56
67export default class AuthService {
7- authenticated = this . isAuthenticated ( )
8- auth0 : WebAuth
9- constructor ( hostname :string , port :string , protocol :string ) {
10- this . login = this . login . bind ( this ) ;
11- this . setSession = this . setSession . bind ( this )
12- this . logout = this . logout . bind ( this )
13- this . isAuthenticated = this . isAuthenticated . bind ( this )
14- var stringBuild = protocol + "//" + hostname ;
15- if ( port !== "" )
16- stringBuild += ':' + port
17- stringBuild += '/callback'
18- this . auth0 = new auth0 . WebAuth ( {
19- domain : 'dotnextrussia.eu.auth0.com' ,
20- clientID : 'g7saZcm47evyY3kWWP26ZxifDpxycl9h' ,
21- redirectUri : stringBuild ,
22- audience : 'http://medhelp20171124063439.azurewebsites.net/' ,
23- responseType : 'token id_token' ,
24- scope : 'openid profile read:templates'
25- } ) ;
26- }
27-
8+ authenticated = this . isAuthenticated ( )
9+ auth0 : WebAuth
10+ constructor ( hostname : string , port : string , protocol : string ) {
11+ this . login = this . login . bind ( this ) ;
12+ this . setSession = this . setSession . bind ( this )
13+ this . logout = this . logout . bind ( this )
14+ this . isAuthenticated = this . isAuthenticated . bind ( this )
15+ var stringBuild = protocol + "//" + hostname ;
16+ if ( port !== "" )
17+ stringBuild += ':' + port
18+ stringBuild += '/callback'
19+ this . auth0 = new auth0 . WebAuth ( {
20+ domain : 'dotnextrussia.eu.auth0.com' ,
21+ clientID : 'g7saZcm47evyY3kWWP26ZxifDpxycl9h' ,
22+ redirectUri : stringBuild ,
23+ audience : 'http://medhelp20171124063439.azurewebsites.net/' ,
24+ responseType : 'token id_token' ,
25+ scope : 'openid profile read:templates'
26+ } ) ;
27+ }
2828
2929
3030
31- login ( ) {
32- this . auth0 . authorize ( ) ;
33- }
3431
35- handleAuthentication ( ) {
36- this . auth0 . parseHash ( ( err , authResult ) => {
37- if ( authResult && authResult . accessToken && authResult . idToken ) {
38- this . setSession ( authResult )
39- Router . replace ( 'home' )
40- } else if ( err ) {
41- Router . replace ( 'home' )
42- console . log ( err )
43- alert ( `Error: ${ err . error } . Check the console for further details.` )
44- }
45- } )
46- }
47-
48- setSession ( authResult : any ) {
49- // Set the time that the access token will expire at
50- let expiresAt = JSON . stringify (
51- authResult . expiresIn * 1000 + new Date ( ) . getTime ( )
52- )
53- localStorage . setItem ( 'access_token' , authResult . accessToken )
54- localStorage . setItem ( 'id_token' , authResult . idToken )
55- localStorage . setItem ( 'expires_at' , expiresAt )
56- EventEmitter . emit ( 'authChange' , true )
57- }
32+ login ( ) {
33+ this . auth0 . authorize ( ) ;
34+ }
5835
59- logout ( ) {
60- // Clear access token and ID token from local storage
61- localStorage . removeItem ( 'access_token' )
62- localStorage . removeItem ( 'id_token' )
63- localStorage . removeItem ( 'expires_at' )
64- //this.userProfile = null
65- EventEmitter . emit ( 'authChange' , false )
66- // navigate to the home route
36+ handleAuthentication ( ) {
37+ this . auth0 . parseHash ( ( err , authResult ) => {
38+ if ( authResult && authResult . accessToken && authResult . idToken ) {
39+ this . setSession ( authResult )
40+ Router . replace ( 'home' )
41+ } else if ( err ) {
6742 Router . replace ( 'home' )
43+ console . log ( err )
44+ alert ( `Error: ${ err . error } . Check the console for further details.` )
6845 }
46+ } )
47+ }
6948
70- isAuthenticated ( ) {
71- // Check whether the current time is past the
72- // access token's expiry time
73- let expiresAt = JSON . parse ( localStorage . getItem ( 'expires_at' ) || '{}' )
74- return new Date ( ) . getTime ( ) < expiresAt
75- }
49+ setSession ( authResult : any ) {
50+ // Set the time that the access token will expire at
51+ let expiresAt = JSON . stringify (
52+ authResult . expiresIn * 1000 + new Date ( ) . getTime ( )
53+ )
54+ localStorage . setItem ( 'access_token' , authResult . accessToken )
55+ localStorage . setItem ( 'id_token' , authResult . idToken )
56+ localStorage . setItem ( 'expires_at' , expiresAt )
57+ ApiService . defaults . headers = { authorization : 'Bearer ' + authResult . accessToken } ;
58+ EventEmitter . emit ( 'authChange' , true )
59+ }
60+
61+ logout ( ) {
62+ // Clear access token and ID token from local storage
63+ localStorage . removeItem ( 'access_token' )
64+ localStorage . removeItem ( 'id_token' )
65+ localStorage . removeItem ( 'expires_at' )
66+ //this.userProfile = null
67+ EventEmitter . emit ( 'authChange' , false )
68+ // navigate to the home route
69+ Router . replace ( 'home' )
70+ }
71+
72+ isAuthenticated ( ) {
73+ // Check whether the current time is past the
74+ // access token's expiry time
75+ let expiresAt = JSON . parse ( localStorage . getItem ( 'expires_at' ) || '{}' )
76+ return new Date ( ) . getTime ( ) < expiresAt
77+ }
7678}
0 commit comments