Skip to content

Commit c17863a

Browse files
committed
chore: login to phoenix code getAppAuth working
1 parent 443877c commit c17863a

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/nls/root/strings.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1568,5 +1568,9 @@ define({
15681568

15691569
// login
15701570
"SIGNED_OUT": "You have been signed out.",
1571-
"SIGNED_OUT_MESSAGE": "You have been signed out of your {APP_NAME} account. Please sign in again to continue."
1571+
"SIGNED_OUT_MESSAGE": "You have been signed out of your {APP_NAME} account. Please sign in again to continue.",
1572+
"SIGNED_IN_OFFLINE_TITLE": "Offline - Cannot Sign In",
1573+
"SIGNED_IN_OFFLINE_MESSAGE": "Please connect to the internet to sign in to {APP_NAME}.",
1574+
"SIGNED_IN_FAILED_TITLE": "Cannot Sign In",
1575+
"SIGNED_IN_FAILED_MESSAGE": "Something went wrong while trying to sign in. Please try again."
15721576
});

src/services/login.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/services/profile-menu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ define(function (require, exports, module) {
6464
function _handleSignInBtnClick() {
6565
console.log("User clicked sign in button");
6666
closePopup(); // need to close the current popup to show the new one
67-
showProfilePopup();
67+
KernalModeTrust.loginService.signInToAccount();
6868
}
6969

7070
function _handleSignOutBtnClick() {

0 commit comments

Comments
 (0)