feat: add cookie support for auth

This commit is contained in:
Derrick Hammer 2024-03-13 19:08:14 -04:00
parent 0d7215ff66
commit 70a4139b93
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 24 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import type {AuthProvider} from "@refinedev/core"
// @ts-ignore // @ts-ignore
import type {AuthActionResponse, CheckResponse, OnErrorResponse} from "@refinedev/core/dist/interfaces/bindings/auth" import type {AuthActionResponse, CheckResponse, OnErrorResponse} from "@refinedev/core/dist/interfaces/bindings/auth"
import {Sdk} from "@lumeweb/portal-sdk"; import {Sdk} from "@lumeweb/portal-sdk";
import Cookies from 'universal-cookie';
export type AuthFormRequest = { export type AuthFormRequest = {
email: string; email: string;
@ -18,11 +19,9 @@ export type RegisterFormRequest = {
} }
export class PortalAuthProvider implements RequiredAuthProvider { export class PortalAuthProvider implements RequiredAuthProvider {
private apiUrl: string;
private sdk: Sdk; private sdk: Sdk;
constructor(apiUrl: string) { constructor(apiUrl: string) {
this.apiUrl = apiUrl;
this.sdk = Sdk.create(apiUrl); this.sdk = Sdk.create(apiUrl);
const methods: Array<keyof AuthProvider> = [ const methods: Array<keyof AuthProvider> = [
@ -38,15 +37,20 @@ export class PortalAuthProvider implements RequiredAuthProvider {
]; ];
methods.forEach((method) => { methods.forEach((method) => {
this[method] = this[method]?.bind(this); this[method] = this[method]?.bind(this) as any;
}); });
} }
async login(params: AuthFormRequest): Promise<AuthActionResponse> { async login(params: AuthFormRequest): Promise<AuthActionResponse> {
const cookies = new Cookies();
const ret = await this.sdk.account().login({ const ret = await this.sdk.account().login({
email: params.email, email: params.email,
password: params.password, password: params.password,
}) })
if (ret) {
cookies.set('jwt', this.sdk.account().jwtToken, { path: '/' });
}
return { return {
success: ret, success: ret,
redirectTo: ret ? "/dashboard" : undefined, redirectTo: ret ? "/dashboard" : undefined,
@ -55,13 +59,29 @@ export class PortalAuthProvider implements RequiredAuthProvider {
async logout(params: any): Promise<AuthActionResponse> { async logout(params: any): Promise<AuthActionResponse> {
let ret = await this.sdk.account().logout(); let ret = await this.sdk.account().logout();
if (ret) {
const cookies = new Cookies();
cookies.remove('jwt');
}
return {success: ret, redirectTo: "/login"}; return {success: ret, redirectTo: "/login"};
} }
async check(params?: any): Promise<CheckResponse> { async check(params?: any): Promise<CheckResponse> {
const cookies = new Cookies();
const jwtCookie = cookies.get('jwt');
if (jwtCookie) {
this.sdk.setAuthToken(jwtCookie);
}
const ret = await this.sdk.account().ping(); const ret = await this.sdk.account().ping();
return {authenticated: ret}; if (!ret) {
cookies.remove('jwt');
}
return {authenticated: ret, redirectTo: ret ? undefined: "/login"};
} }
async onError(error: any): Promise<OnErrorResponse> { async onError(error: any): Promise<OnErrorResponse> {