Auth redirects, language switch, basic profile
This commit is contained in:
@@ -6,8 +6,9 @@ const BASE = 'https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/';
|
||||
let instance: ApiImpl;
|
||||
|
||||
class ApiImpl {
|
||||
//FIXME: PRIVATE when reauth token exists
|
||||
public token?: string;
|
||||
private token?: string;
|
||||
private refreshToken?: string;
|
||||
private self?: User;
|
||||
|
||||
constructor() {
|
||||
if (instance) {
|
||||
@@ -19,18 +20,26 @@ class ApiImpl {
|
||||
}
|
||||
|
||||
public hasAuth = () => this.token !== undefined;
|
||||
public isAdmin = () => this.hasAuth() && this.self?.isAdmin;
|
||||
public getAuthenticatedUser = () => this.self;
|
||||
public getCurrentSession = () => [this.token, this.refreshToken];
|
||||
|
||||
//FIXME: Currently returns Auth token, switch to reauth token
|
||||
public logIn = async (email: string, password: string): Promise<[User, string]> => {
|
||||
public logIn = async (email: string, password: string): Promise<void> => {
|
||||
const { user, token } = await (await this.post('login', { email, password })).json();
|
||||
this.self = user;
|
||||
this.isAdmin = user.isAdmin;
|
||||
this.token = token;
|
||||
|
||||
return [user, token];
|
||||
};
|
||||
|
||||
public logOut = async (): Promise<boolean> => {
|
||||
this.token = undefined;
|
||||
return await (await this.postAuth('logout')).json();
|
||||
try {
|
||||
return await (await this.postAuth('logout')).json();
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
this.self = undefined;
|
||||
this.token = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
public posts = async (page?: number): Promise<PostListNonAuth | PostListAuth> => {
|
||||
@@ -41,6 +50,10 @@ class ApiImpl {
|
||||
return await (await this.get(url)).json();
|
||||
};
|
||||
|
||||
public user = async (id?: number): Promise<User> => {
|
||||
return await (await this.getAuth(`users/${id ?? this.self?.id}`)).json();
|
||||
};
|
||||
|
||||
private post = async (
|
||||
endpoint: string,
|
||||
body: Record<string, unknown> | undefined = undefined,
|
||||
|
||||
Reference in New Issue
Block a user