1- import axios , { Axios , AxiosError } from "axios" ;
1+ import { AxiosError } from "axios" ;
22import loginWithPassword from "./loginWithPassword" ;
33import loginWithOAuthService from "./loginWithOAuthService" ;
44import loginWithOAuthServiceToken from "./loginWithOAuthServiceToken" ;
55import loginWithResumeToken from "./loginWithResumeToken" ;
6+ import { IRocketChatAuthOptions } from "./IRocketChatAuthOptions" ;
7+ import { Api } from "./Api" ;
68
79class RocketChatAuth {
810 host : string ;
9- api : Axios ;
11+ api : Api ;
1012 currentUser : any ;
1113 lastFetched : Date ;
1214 authListeners : ( ( user : object | null ) => void ) [ ] = [ ] ;
13- constructor ( host : string ) {
15+ deleteToken : ( ) => Promise < void > ;
16+ saveToken : ( token : string ) => Promise < void > ;
17+ getToken : ( ) => Promise < string > ;
18+ constructor ( { host, saveToken, getToken, deleteToken, autoLogin = true } : IRocketChatAuthOptions ) {
1419 this . host = host ;
15- this . api = axios . create ( {
16- baseURL : host ,
17- headers : {
18- "Content-Type" : "application/json"
19- }
20- } ) ;
20+ this . api = new Api ( host ) ;
2121 this . lastFetched = new Date ( 0 ) ;
2222 this . currentUser = null ;
23- this . load ( ) ;
23+ this . getToken = getToken ;
24+ this . saveToken = saveToken ;
25+ this . deleteToken = deleteToken ;
26+ if ( autoLogin ) {
27+ this . load ( ) ;
28+ }
2429 }
2530
2631 /**
@@ -63,9 +68,12 @@ class RocketChatAuth {
6368 * @param oauthService
6469 */
6570 async loginWithOAuthService ( oauthService : any ) {
66- const response = await loginWithOAuthService ( {
67- api : this . api
68- } , oauthService ) ;
71+ const response = await loginWithOAuthService (
72+ {
73+ api : this . api ,
74+ } ,
75+ oauthService
76+ ) ;
6977 }
7078
7179 /**
@@ -74,10 +82,12 @@ class RocketChatAuth {
7482 * @returns
7583 */
7684 async loginWithOAuthServiceToken ( credentials : { [ key : string ] : string ; service : string ; access_token : string ; } ) {
77- const response = await loginWithOAuthServiceToken ( {
78- api : this . api ,
79- } ,
80- credentials )
85+ const response = await loginWithOAuthServiceToken (
86+ {
87+ api : this . api ,
88+ } ,
89+ credentials
90+ ) ;
8191 this . setUser ( response . data )
8292 return this . currentUser ;
8393 }
@@ -127,31 +137,31 @@ class RocketChatAuth {
127137 async setUser ( user : object ) {
128138 this . lastFetched = new Date ( ) ;
129139 this . currentUser = user ;
130- this . save ( )
140+ await this . save ( )
131141 }
132142
133- /**
134- * Save current user to localStorage
135- */
136- save ( ) {
137- localStorage . setItem ( "ec_user" , JSON . stringify ( {
138- user : this . currentUser ,
139- lastFetched : this . lastFetched
140- } ) )
143+ async save ( ) {
144+ // localStorage.setItem("ec_user", JSON.stringify({
145+ // user: this.currentUser,
146+ // lastFetched: this.lastFetched
147+ // }))
148+ await this . saveToken ( this . currentUser . authToken )
141149 this . notifyAuthListeners ( ) ;
142150 }
143151
144152 /**
145153 * Load current user from localStorage
146154 */
147155 async load ( ) {
148- const { user, lastFetched} = JSON . parse ( localStorage . getItem ( "ec_user" ) || "{}" ) ;
156+ // const {user, lastFetched} = JSON.parse(localStorage.getItem("ec_user") || "{}");
157+ const token = await this . getToken ( ) ;
158+ const user = await this . loginWithResumeToken ( token ) ;
149159 if ( user ) {
150- this . lastFetched = new Date ( lastFetched ) ;
160+ this . lastFetched = new Date ( ) ;
151161 this . setUser ( user ) ;
152162 await this . getCurrentUser ( ) ; // refresh the token if needed
153163 }
154- this . notifyAuthListeners ( )
164+ this . notifyAuthListeners ( ) ;
155165 }
156166
157167 /**
@@ -167,8 +177,10 @@ class RocketChatAuth {
167177 } ) ;
168178 } catch ( err ) {
169179 console . error ( err ) ;
170- }
171- localStorage . removeItem ( "ec_user" ) ;
180+ } finally {
181+ await this . deleteToken ( ) ;
182+ }
183+ // localStorage.removeItem("ec_user");
172184 this . lastFetched = new Date ( 0 ) ;
173185 this . currentUser = null ;
174186 this . notifyAuthListeners ( ) ;
0 commit comments