Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getPermissions support in authProvider #50

Merged
merged 7 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/ra-supabase-core/src/authProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Provider, SupabaseClient, User } from '@supabase/supabase-js';

export const supabaseAuthProvider = (
client: SupabaseClient,
{ getIdentity, redirectTo }: SupabaseAuthProviderOptions
{ getIdentity, getPermissions, redirectTo }: SupabaseAuthProviderOptions
): SupabaseAuthProvider => {
return {
async login(params) {
Expand Down Expand Up @@ -139,7 +139,22 @@ export const supabaseAuthProvider = (
return Promise.resolve();
},
async getPermissions() {
return;
const {
data,
error,
} = await client.auth.getUser()
if (error) {
throw error;
}
if (data.user == null) {
throw new Error();
}

if (typeof getPermissions === "function") {
const permissions = await getPermissions(data.user);
return permissions;
}
return undefined;
},
async getIdentity() {
const { data } = await client.auth.getUser();
Expand All @@ -159,8 +174,10 @@ export const supabaseAuthProvider = (
};

export type GetIdentity = (user: User) => Promise<UserIdentity>;
export type GetPermissions = (user: User) => Promise<any>;
export type SupabaseAuthProviderOptions = {
getIdentity?: GetIdentity;
getPermissions?: GetPermissions;
redirectTo?: string;
};

Expand Down