Skip to content

Commit ed16b36

Browse files
committed
add redirect state
1 parent d363041 commit ed16b36

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

site/src/xServices/auth/authXService.ts

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export interface AuthContext {
8989
sshKey?: TypesGen.GitSSHKey
9090
getSSHKeyError?: Error | unknown
9191
regenerateSSHKeyError?: Error | unknown
92+
host?: TypesGen.GetAppHostResponse["host"]
9293
}
9394

9495
export type AuthEvent =
@@ -159,6 +160,13 @@ export const authMachine =
159160
},
160161
},
161162
},
163+
signedOutAllDomains: {
164+
on: {
165+
SIGN_IN: {
166+
target: "signingIn",
167+
},
168+
},
169+
},
162170
signingIn: {
163171
entry: "clearAuthError",
164172
invoke: {
@@ -416,10 +424,44 @@ export const authMachine =
416424
},
417425
on: {
418426
SIGN_OUT: {
419-
target: "signingOut",
427+
target: "checkingAppHost",
420428
},
421429
},
422430
},
431+
checkingAppHost: {
432+
invoke: {
433+
src: "checkHost",
434+
id: "checkHost",
435+
onDone: [
436+
{
437+
cond: "hasHost",
438+
actions: ["assignHost"],
439+
target: "signingOutAndRedirecting",
440+
},
441+
{
442+
target: "signingOut",
443+
},
444+
],
445+
}
446+
},
447+
signingOutAndRedirecting: {
448+
invoke: {
449+
src: "signOutAndRedirect",
450+
id: "signOutAndRedirect",
451+
onDone: [
452+
{
453+
actions: ["unassignMe", "clearAuthError"],
454+
target: "signedOutAllDomains",
455+
},
456+
],
457+
onError: [
458+
{
459+
actions: "assignAuthError",
460+
target: "signedIn",
461+
},
462+
],
463+
}
464+
},
423465
signingOut: {
424466
invoke: {
425467
src: "signOut",
@@ -469,24 +511,27 @@ export const authMachine =
469511
signIn: async (_, event) => {
470512
return await API.login(event.email, event.password)
471513
},
472-
signOut: async () => {
514+
checkHost: async () => {
515+
const appHost = await API.getApplicationsHost()
516+
return appHost.host
517+
},
518+
signOutAndRedirect: async (context) => {
473519
// Get app hostname so we can see if we need to log out of app URLs.
474520
// We need to load this before we log out of the API as this is an
475521
// authenticated endpoint.
476-
const appHost = await API.getApplicationsHost()
477522
await API.logout()
478-
479-
if (appHost.host) {
523+
if (context.host) {
480524
const redirect_uri = encodeURIComponent(window.location.href)
481525
// The path doesn't matter but we use /api because the dev server
482526
// proxies /api to the backend.
483-
const uri = `${window.location.protocol}//${appHost.host.replace(
527+
const uri = `${window.location.protocol}//${context.host.replace(
484528
"*",
485529
"coder-logout",
486530
)}/api/logout?redirect_uri=${redirect_uri}`
487531
window.location.replace(uri)
488532
}
489533
},
534+
signOut: API.logout,
490535
getMe: API.getUser,
491536
getMethods: API.getAuthMethods,
492537
updateProfile: async (context, event) => {
@@ -594,9 +639,13 @@ export const authMachine =
594639
notifySuccessSSHKeyRegenerated: () => {
595640
displaySuccess(Language.successRegenerateSSHKey)
596641
},
642+
assignHost: () => assign({
643+
host: (_, event) => event
644+
})
597645
},
598646
guards: {
599647
isTrue: (_, event) => event.data,
648+
hasHost: (_, event) => Boolean(event),
600649
},
601650
},
602651
)

0 commit comments

Comments
 (0)