Can authenticate using OpenID

This commit is contained in:
2023-06-09 10:45:01 +02:00
parent 94a4ab4f91
commit d55718f4de
6 changed files with 219 additions and 3 deletions

View File

@ -1,8 +1,45 @@
import { atom } from "jotai";
import { APIClient } from "./ApiClient";
const TokenStateKey = "auth-token";
export class AuthApi {
/**
* Check out whether user is signed in or not
*/
static get SignedIn(): boolean {
return false;
return sessionStorage.getItem(TokenStateKey) !== null;
}
static authStatus = atom(this.SignedIn);
/**
* Start OpenID login
*
* @param id The ID of the OIDC provider to use
*/
static async StartOpenIDLogin(id: string): Promise<{ url: string }> {
return (
await APIClient.exec({
uri: "/auth/start_openid_login",
method: "POST",
jsonData: { provider: id },
})
).data;
}
/**
* Finish OpenID login
*/
static async FinishOpenIDLogin(code: string, state: string): Promise<void> {
const res: { user_id: number; token: string } = (
await APIClient.exec({
uri: "/auth/finish_openid_login",
method: "POST",
jsonData: { code: code, state: state },
})
).data;
sessionStorage.setItem(TokenStateKey, res.token);
}
}

View File

@ -40,7 +40,7 @@ export class ServerApi {
/**
* Get cached configuration
*/
static Config(): ServerConfig {
static get Config(): ServerConfig {
if (config === null) throw new Error("Missing configuration!");
return config;
}