From 70a4139b931c9548305cf4600d455aef0f6c6021 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 13 Mar 2024 19:08:14 -0400 Subject: [PATCH] feat: add cookie support for auth --- app/data/auth-provider.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/data/auth-provider.ts b/app/data/auth-provider.ts index aa1b078..65f659d 100644 --- a/app/data/auth-provider.ts +++ b/app/data/auth-provider.ts @@ -3,6 +3,7 @@ import type {AuthProvider} from "@refinedev/core" // @ts-ignore import type {AuthActionResponse, CheckResponse, OnErrorResponse} from "@refinedev/core/dist/interfaces/bindings/auth" import {Sdk} from "@lumeweb/portal-sdk"; +import Cookies from 'universal-cookie'; export type AuthFormRequest = { email: string; @@ -18,11 +19,9 @@ export type RegisterFormRequest = { } export class PortalAuthProvider implements RequiredAuthProvider { - private apiUrl: string; private sdk: Sdk; constructor(apiUrl: string) { - this.apiUrl = apiUrl; this.sdk = Sdk.create(apiUrl); const methods: Array = [ @@ -38,15 +37,20 @@ export class PortalAuthProvider implements RequiredAuthProvider { ]; methods.forEach((method) => { - this[method] = this[method]?.bind(this); + this[method] = this[method]?.bind(this) as any; }); } async login(params: AuthFormRequest): Promise { + const cookies = new Cookies(); const ret = await this.sdk.account().login({ email: params.email, password: params.password, }) + + if (ret) { + cookies.set('jwt', this.sdk.account().jwtToken, { path: '/' }); + } return { success: ret, redirectTo: ret ? "/dashboard" : undefined, @@ -55,13 +59,29 @@ export class PortalAuthProvider implements RequiredAuthProvider { async logout(params: any): Promise { let ret = await this.sdk.account().logout(); + if (ret) { + const cookies = new Cookies(); + cookies.remove('jwt'); + } return {success: ret, redirectTo: "/login"}; } async check(params?: any): Promise { + const cookies = new Cookies(); + + const jwtCookie = cookies.get('jwt'); + + if (jwtCookie) { + this.sdk.setAuthToken(jwtCookie); + } + 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 {