Refactor authentication method to requireUser
Replaced the requireAuth method with requireUser to improve clarity and modified all instances where it was used. Renamed the import throughout multiple files and services and made changes accordingly, thus making it more specific and understandable that a logged-in user is needed. The return type of the method was also updated from Session to User to more accurately reflect the information it provides.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { Session, SupabaseClient } from '@supabase/supabase-js';
|
||||
import type { SupabaseClient, User } from '@supabase/supabase-js';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
@@ -15,18 +15,14 @@ const SIGN_IN_PATH = z
|
||||
.parse(process.env.SIGN_IN_PATH);
|
||||
|
||||
/**
|
||||
* @name requireAuth
|
||||
* @name requireUser
|
||||
* @description Require a session to be present in the request
|
||||
* @param client
|
||||
* @param verifyFromServer
|
||||
*/
|
||||
export async function requireAuth(
|
||||
client: SupabaseClient,
|
||||
verifyFromServer = true,
|
||||
): Promise<
|
||||
export async function requireUser(client: SupabaseClient): Promise<
|
||||
| {
|
||||
error: null;
|
||||
data: Session;
|
||||
data: User;
|
||||
}
|
||||
| (
|
||||
| {
|
||||
@@ -41,9 +37,9 @@ export async function requireAuth(
|
||||
}
|
||||
)
|
||||
> {
|
||||
const { data, error } = await client.auth.getSession();
|
||||
const { data, error } = await client.auth.getUser();
|
||||
|
||||
if (!data.session || error) {
|
||||
if (!data.user || error) {
|
||||
return {
|
||||
data: null,
|
||||
error: new AuthenticationError(),
|
||||
@@ -63,21 +59,9 @@ export async function requireAuth(
|
||||
};
|
||||
}
|
||||
|
||||
if (verifyFromServer) {
|
||||
const { data: user, error } = await client.auth.getUser();
|
||||
|
||||
if (!user || error) {
|
||||
return {
|
||||
data: null,
|
||||
error: new AuthenticationError(),
|
||||
redirectTo: SIGN_IN_PATH,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
error: null,
|
||||
data: data.session,
|
||||
data: data.user,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user