Add hosted login persistence

This commit is contained in:
2026-07-15 15:33:47 -05:00
parent 02ffa1dfc4
commit e62ec49c59
9 changed files with 508 additions and 38 deletions
+47
View File
@@ -65,6 +65,16 @@ export type WorkspaceSnapshot = {
studySettings: StudySettings
}
export type HostedUser = {
id: number
username: string
}
export type HostedSession = {
authenticated: boolean
user: HostedUser | null
}
export function hostedWorkspaceEnabled() {
return remoteWorkspaceEnabled
}
@@ -223,3 +233,40 @@ async function saveRemoteWorkspaceSnapshot(snapshot: WorkspaceSnapshot) {
throw new Error(`Unable to save hosted workspace: ${response.status} ${response.statusText}`)
}
}
export async function loadHostedSession(): Promise<HostedSession> {
if (!remoteWorkspaceEnabled) return { authenticated: false, user: null }
const response = await fetch(`${remoteWorkspaceApiBase}/session`, {
credentials: 'include',
})
if (!response.ok) {
throw new Error(`Unable to check hosted login: ${response.status} ${response.statusText}`)
}
return response.json() as Promise<HostedSession>
}
export async function loginHostedWorkspace(username: string, password: string): Promise<HostedSession> {
const response = await fetch(`${remoteWorkspaceApiBase}/login`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
})
if (!response.ok) {
throw new Error(response.status === 401 ? 'Invalid username or password.' : `Unable to log in: ${response.status} ${response.statusText}`)
}
return response.json() as Promise<HostedSession>
}
export async function logoutHostedWorkspace(): Promise<void> {
if (!remoteWorkspaceEnabled) return
const response = await fetch(`${remoteWorkspaceApiBase}/logout`, {
method: 'POST',
credentials: 'include',
})
if (!response.ok) {
throw new Error(`Unable to log out: ${response.status} ${response.statusText}`)
}
}