@@ -94,6 +94,12 @@ define(function (require, exports, module) {
9494 }
9595 }
9696
97+ async function _resetAccountLogin ( ) {
98+ isLoggedInUser = false ;
99+ ProfileMenu . setNotLoggedIn ( ) ;
100+ await KernalModeTrust . removeCredential ( KernalModeTrust . CRED_KEY_API ) ;
101+ }
102+
97103 async function _verifyLogin ( ) {
98104 const savedUserProfile = await KernalModeTrust . getCredential ( KernalModeTrust . CRED_KEY_API ) ;
99105 if ( ! savedUserProfile ) {
@@ -122,14 +128,13 @@ define(function (require, exports, module) {
122128 }
123129 // some error happened.
124130 if ( resolveResponse . err === ERR_INVALID ) { // the api key is invalid, we need to logout and tell user
125- isLoggedInUser = false ;
126- ProfileMenu . setNotLoggedIn ( ) ;
131+ _resetAccountLogin ( )
132+ . catch ( console . error ) ;
127133 Dialogs . showModalDialog (
128134 DefaultDialogs . DIALOG_ID_ERROR ,
129135 Strings . SIGNED_OUT ,
130136 Strings . SIGNED_OUT_MESSAGE
131137 ) ;
132- await KernalModeTrust . removeCredential ( KernalModeTrust . CRED_KEY_API ) ;
133138 }
134139 // maybe some intermittent network error, ERR_RETRY_LATER is here. do nothing
135140 }
@@ -253,6 +258,50 @@ define(function (require, exports, module) {
253258 NativeApp . openURLInDefaultBrowser ( appSignInURL ) ;
254259 }
255260
261+ async function signOutAccount ( ) {
262+ try {
263+ const resolveURL = `${ Phoenix . config . account_url } logoutSession` ;
264+ let input = {
265+ appSessionID : userProfile . apiKey
266+ } ;
267+
268+ const response = await fetch ( resolveURL , {
269+ method : 'POST' ,
270+ headers : {
271+ 'Content-Type' : 'application/json'
272+ } ,
273+ body : JSON . stringify ( input )
274+ } ) ;
275+
276+ const result = await response . json ( ) ;
277+
278+ if ( ! result . isSuccess ) {
279+ console . error ( 'Error logging out' , result ) ;
280+ Dialogs . showModalDialog (
281+ DefaultDialogs . DIALOG_ID_ERROR ,
282+ Strings . SIGNED_OUT_FAILED_TITLE ,
283+ Strings . SIGNED_OUT_FAILED_MESSAGE
284+ ) ;
285+ return ;
286+ // todo raise metrics
287+ }
288+ await _resetAccountLogin ( ) ;
289+ Dialogs . showModalDialog (
290+ DefaultDialogs . DIALOG_ID_INFO ,
291+ Strings . SIGNED_OUT ,
292+ Strings . SIGNED_OUT_MESSAGE_FRIENDLY
293+ ) ;
294+ } catch ( error ) {
295+ console . error ( "Network error. Could not log out session." , error ) ;
296+ Dialogs . showModalDialog (
297+ DefaultDialogs . DIALOG_ID_ERROR ,
298+ Strings . SIGNED_OUT_FAILED_TITLE ,
299+ Strings . SIGNED_OUT_FAILED_MESSAGE
300+ ) ;
301+ // todo raise metrics
302+ }
303+ }
304+
256305 function init ( ) {
257306 ProfileMenu . init ( ) ;
258307 if ( ! Phoenix . isNativeApp ) {
@@ -273,6 +322,7 @@ define(function (require, exports, module) {
273322 // kernal exports
274323 secureExports . isLoggedIn = isLoggedIn ;
275324 secureExports . signInToAccount = signInToAccount ;
325+ secureExports . signOutAccount = signOutAccount ;
276326
277327 // public exports
278328 exports . isLoggedIn = isLoggedIn ;
0 commit comments