@@ -23,6 +23,7 @@ define(function (require, exports, module) {
2323 Dialogs = require ( "widgets/Dialogs" ) ,
2424 DefaultDialogs = require ( "widgets/DefaultDialogs" ) ,
2525 Strings = require ( "strings" ) ,
26+ NativeApp = require ( "utils/NativeApp" ) ,
2627 ProfileMenu = require ( "./profile-menu" ) ;
2728
2829 const KernalModeTrust = window . KernalModeTrust ;
@@ -122,6 +123,54 @@ define(function (require, exports, module) {
122123 // maybe some intermittent network error, ERR_RETRY_LATER is here. do nothing
123124 }
124125
126+ // never rejects.
127+ async function _getAppAuthSession ( ) {
128+ const authPortURL = "9797/abc" ; // todo autho auth later
129+ const appName = encodeURIComponent ( `${ Strings . APP_NAME } Desktop on ${ Phoenix . platform } ` ) ;
130+ const resolveURL = `${ Phoenix . config . account_url } getAppAuthSession?autoAuthPort=${ authPortURL } &appName=${ appName } ` ;
131+ // {"isSuccess":true,"appSessionID":"a uuid...","validationCode":"SWXP07"}
132+ try {
133+ const response = await fetch ( resolveURL ) ;
134+ if ( response . ok ) {
135+ const { appSessionID, validationCode} = await response . json ( ) ;
136+ if ( ! appSessionID || ! validationCode ) {
137+ throw new Error ( "Invalid response from getAppAuthSession API endpoint" + resolveURL ) ;
138+ }
139+ return { appSessionID, validationCode} ;
140+ }
141+ return null ;
142+ } catch ( e ) {
143+ console . error ( e , "Failed to call getAppAuthSession API endpoint" , resolveURL ) ;
144+ // todo raise metrics/log
145+ return null ;
146+ }
147+ }
148+
149+ async function signInToAccount ( ) {
150+ if ( ! navigator . onLine ) {
151+ Dialogs . showModalDialog (
152+ DefaultDialogs . DIALOG_ID_ERROR ,
153+ Strings . SIGNED_IN_OFFLINE_TITLE ,
154+ Strings . SIGNED_IN_OFFLINE_MESSAGE
155+ ) ;
156+ return ;
157+ }
158+ const appAuthSession = await _getAppAuthSession ( ) ;
159+ if ( ! appAuthSession ) {
160+ Dialogs . showModalDialog (
161+ DefaultDialogs . DIALOG_ID_ERROR ,
162+ Strings . SIGNED_IN_FAILED_TITLE ,
163+ Strings . SIGNED_IN_FAILED_MESSAGE
164+ ) ;
165+ return ;
166+ }
167+ const { appSessionID, validationCode} = appAuthSession ;
168+ const appSignInURL = `${ Phoenix . config . account_url } authorizeApp?appSessionID=${ appSessionID } ` ;
169+ // show a dialog here with the 6 letter validation code and a button to copy the validation code and another
170+ // button to open the sign in code
171+ NativeApp . openURLInDefaultBrowser ( appSignInURL ) ;
172+ }
173+
125174 function init ( ) {
126175 ProfileMenu . init ( ) ;
127176 if ( ! Phoenix . isNativeApp ) {
@@ -143,6 +192,8 @@ define(function (require, exports, module) {
143192
144193 // kernal exports
145194 secureExports . isLoggedIn = isLoggedIn ;
195+ secureExports . signInToAccount = signInToAccount ;
196+
146197 // public exports
147198 exports . isLoggedIn = isLoggedIn ;
148199
0 commit comments