refactor: major refactor of the providers and resources to be more dynamic and flexible

This commit is contained in:
Derrick Hammer 2024-03-19 07:20:11 -04:00
parent 2e918e7802
commit ff5e5dec14
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
8 changed files with 177 additions and 148 deletions

View File

@ -0,0 +1,11 @@
import type { DataProvider } from "@refinedev/core";
import {SdkProvider} from "~/data/sdk-provider.js";
export const accountProvider: SdkProvider = {
getList: () => { throw Error("Not Implemented") },
getOne: () => { throw Error("Not Implemented") },
update: () => { throw Error("Not Implemented") },
create: () => { throw Error("Not Implemented") },
deleteOne: () => { throw Error("Not Implemented") },
getApiUrl: () => "",
}

View File

@ -31,134 +31,94 @@ export type Identity = {
email: string; email: string;
} }
export class PortalAuthProvider implements RequiredAuthProvider { export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
constructor(apiUrl: string) { const maybeSetupAuth = (): void => {
this._sdk = Sdk.create(apiUrl); const jwt = sdk.account().jwtToken;
const methods: Array<keyof AuthProvider> = [
'login',
'logout',
'check',
'onError',
'register',
'forgotPassword',
'updatePassword',
'getPermissions',
'getIdentity',
];
methods.forEach((method) => {
this[method] = this[method]?.bind(this) as any;
});
}
private _sdk: Sdk;
get sdk(): Sdk {
return this._sdk;
}
public static create(apiUrl: string): AuthProvider {
return new PortalAuthProvider(apiUrl);
}
async login(params: AuthFormRequest): Promise<AuthActionResponse> {
const ret = await this._sdk.account().login({
email: params.email,
password: params.password,
})
let redirectTo: string | undefined;
if (ret) {
redirectTo = params.redirectTo;
if (!redirectTo) {
redirectTo = ret ? "/dashboard" : "/login";
}
this._sdk.setAuthToken(this._sdk.account().jwtToken);
}
return {
success: ret,
redirectTo,
};
}
async logout(params: any): Promise<AuthActionResponse> {
let ret = await this._sdk.account().logout();
return {success: ret, redirectTo: "/login"};
}
async check(params?: any): Promise<CheckResponse> {
const ret = await this._sdk.account().ping();
if(ret){
this.maybeSetupAuth();
}
return {authenticated: ret, redirectTo: ret ? undefined : "/login"};
}
async onError(error: any): Promise<OnErrorResponse> {
return {logout: true};
}
async register(params: RegisterFormRequest): Promise<AuthActionResponse> {
const ret = await this._sdk.account().register({
email: params.email,
password: params.password,
first_name: params.firstName,
last_name: params.lastName,
});
return {success: ret, redirectTo: ret ? "/dashboard" : undefined};
}
async forgotPassword(params: any): Promise<AuthActionResponse> {
return {success: true};
}
async updatePassword(params: any): Promise<AuthActionResponse> {
return {success: true};
}
async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> {
return {success: true};
}
async getIdentity(params?: Identity): Promise<IdentityResponse> {
this.maybeSetupAuth();
const ret = await this._sdk.account().info();
if (!ret) {
return {identity: null};
}
const acct = ret as AccountInfoResponse;
return {
id: acct.id,
firstName: acct.first_name,
lastName: acct.last_name,
email: acct.email,
};
}
maybeSetupAuth(): void {
const jwt = this._sdk.account().jwtToken
if (jwt) { if (jwt) {
this._sdk.setAuthToken(jwt); sdk.setAuthToken(jwt);
} }
} };
}
export interface RequiredAuthProvider extends AuthProvider { return {
login: AuthProvider['login']; async login(params: AuthFormRequest): Promise<AuthActionResponse> {
logout: AuthProvider['logout']; const ret = await sdk.account().login({
check: AuthProvider['check']; email: params.email,
onError: AuthProvider['onError']; password: params.password,
register: AuthProvider['register']; });
forgotPassword: AuthProvider['forgotPassword'];
updatePassword: AuthProvider['updatePassword']; let redirectTo: string | undefined;
getPermissions: AuthProvider['getPermissions'];
getIdentity: AuthProvider['getIdentity']; if (ret) {
} redirectTo = params.redirectTo;
if (!redirectTo) {
redirectTo = ret ? "/dashboard" : "/login";
}
sdk.setAuthToken(sdk.account().jwtToken);
}
return {
success: ret,
redirectTo,
};
},
async logout(params: any): Promise<AuthActionResponse> {
let ret = await sdk.account().logout();
return { success: ret, redirectTo: "/login" };
},
async check(params?: any): Promise<CheckResponse> {
const ret = await sdk.account().ping();
if (ret) {
maybeSetupAuth();
}
return { authenticated: ret, redirectTo: ret ? undefined : "/login" };
},
async onError(error: any): Promise<OnErrorResponse> {
return { logout: true };
},
async register(params: RegisterFormRequest): Promise<AuthActionResponse> {
const ret = await sdk.account().register({
email: params.email,
password: params.password,
first_name: params.firstName,
last_name: params.lastName,
});
return { success: ret, redirectTo: ret ? "/dashboard" : undefined };
},
async forgotPassword(params: any): Promise<AuthActionResponse> {
return { success: true };
},
async updatePassword(params: any): Promise<AuthActionResponse> {
return { success: true };
},
async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> {
return { success: true };
},
async getIdentity(params?: Identity): Promise<IdentityResponse> {
maybeSetupAuth();
const ret = await sdk.account().info();
if (!ret) {
return { identity: null };
}
const acct = ret as AccountInfoResponse;
return {
id: acct.id,
firstName: acct.first_name,
lastName: acct.last_name,
email: acct.email,
};
},
};
};

View File

@ -1,10 +1,11 @@
import type { DataProvider } from "@refinedev/core"; import type { DataProvider } from "@refinedev/core";
import {SdkProvider} from "~/data/sdk-provider.js";
export const defaultProvider: DataProvider = { export const fileProvider: SdkProvider = {
getList: () => { throw Error("Not Implemented") }, getList: () => { throw Error("Not Implemented") },
getOne: () => { throw Error("Not Implemented") }, getOne: () => { throw Error("Not Implemented") },
update: () => { throw Error("Not Implemented") }, update: () => { throw Error("Not Implemented") },
create: () => { throw Error("Not Implemented") }, create: () => { throw Error("Not Implemented") },
deleteOne: () => { throw Error("Not Implemented") }, deleteOne: () => { throw Error("Not Implemented") },
getApiUrl: () => "", getApiUrl: () => "",
} }

31
app/data/providers.ts Normal file
View File

@ -0,0 +1,31 @@
import type {AuthProvider, DataProvider} from "@refinedev/core";
import {fileProvider} from "~/data/file-provider.js";
import {Sdk} from "@lumeweb/portal-sdk";
import {accountProvider} from "~/data/account-provider.js";
import type {SdkProvider} from "~/data/sdk-provider.js";
import {createPortalAuthProvider} from "~/data/auth-provider.js";
interface DataProviders {
default: SdkProvider;
auth: AuthProvider;
[key: string]: SdkProvider | AuthProvider;
}
let providers: DataProviders;
export function getProviders(sdk: Sdk) {
if (providers) {
return providers;
}
accountProvider.sdk = sdk;
fileProvider.sdk = sdk;
providers = {
default: accountProvider,
auth: createPortalAuthProvider(sdk),
files: fileProvider,
};
return providers;
}

18
app/data/resources.ts Normal file
View File

@ -0,0 +1,18 @@
import type {ResourceProps} from "@refinedev/core";
const resources: ResourceProps[] = [
{
name: 'account',
meta: {
dataProviderName: 'default',
}
},
{
name: 'file',
meta: {
dataProviderName: 'files',
}
}
];
export default resources;

6
app/data/sdk-provider.ts Normal file
View File

@ -0,0 +1,6 @@
import {DataProvider} from "@refinedev/core";
import {Sdk} from "@lumeweb/portal-sdk";
export interface SdkProvider extends DataProvider {
sdk?: Sdk;
}

View File

@ -1,21 +1,16 @@
import { import {Links, Meta, Outlet, Scripts, ScrollRestoration,} from "@remix-run/react";
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";
import stylesheet from "./tailwind.css?url"; import stylesheet from "./tailwind.css?url";
import type { LinksFunction } from "@remix-run/node"; import type {LinksFunction} from "@remix-run/node";
// Supports weights 200-800 // Supports weights 200-800
import '@fontsource-variable/manrope'; import '@fontsource-variable/manrope';
import {Refine} from "@refinedev/core"; import {Refine} from "@refinedev/core";
import {PortalAuthProvider} from "~/data/auth-provider.js";
import routerProvider from "@refinedev/remix-router"; import routerProvider from "@refinedev/remix-router";
import { defaultProvider } from "./data/file-provider";
import {SdkContextProvider} from "~/components/lib/sdk-context.js"; import {SdkContextProvider} from "~/components/lib/sdk-context.js";
import {getProviders} from "~/data/providers.js";
import {Sdk} from "@lumeweb/portal-sdk";
import resources from "~/data/resources.js";
export const links: LinksFunction = () => [ export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet }, { rel: "stylesheet", href: stylesheet },
@ -40,19 +35,17 @@ export function Layout({children}: { children: React.ReactNode }) {
} }
export default function App() { export default function App() {
const auth = PortalAuthProvider.create("https://alpha.pinner.xyz") const sdk = Sdk.create(import.meta.env.VITE_PORTAL_URL)
const providers = getProviders(sdk);
return ( return (
<Refine <Refine
authProvider={auth} authProvider={providers.auth}
routerProvider={routerProvider} routerProvider={routerProvider}
dataProvider={defaultProvider} dataProvider={providers.default}
resources={[ resources={resources}
{ name: 'files' },
{ name: 'users' }
]}
options={{disableTelemetry: true}} options={{disableTelemetry: true}}
> >
<SdkContextProvider sdk={(auth as PortalAuthProvider).sdk}> <SdkContextProvider sdk={sdk}>
<Outlet/> <Outlet/>
</SdkContextProvider> </SdkContextProvider>
</Refine> </Refine>

9
vite-env.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_PORTAL_API_URL: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}