mirror of
https://github.com/sct/overseerr.git
synced 2025-09-17 17:24:35 +02:00
User Context (#51)
* feat(frontend): user Context / useUser hook Adds a UserContext to wrap the app and load/cache the user when the website renders. Also adds the useUser hook to pull in user data anywhere its needed on the site. This commit also adds redirection to the login page for users who are not signed in * fix(frontend): use process.env.PORT for user request on server side (defaults to 3000) * docs(frontend): added documentation/notes for how the user context/login works
This commit is contained in:
31
src/hooks/useUser.ts
Normal file
31
src/hooks/useUser.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import useSwr from 'swr';
|
||||
import { useRef } from 'react';
|
||||
export interface User {
|
||||
id: number;
|
||||
email: string;
|
||||
}
|
||||
|
||||
interface UserHookResponse {
|
||||
user?: User;
|
||||
loading: boolean;
|
||||
error: string;
|
||||
revalidate: () => Promise<boolean>;
|
||||
}
|
||||
|
||||
export const useUser = ({
|
||||
id,
|
||||
initialData,
|
||||
}: { id?: number; initialData?: User } = {}): UserHookResponse => {
|
||||
const initialRef = useRef(initialData);
|
||||
const { data, error, revalidate } = useSwr<User>(
|
||||
id ? `/api/v1/user/${id}` : `/api/v1/auth/me`,
|
||||
{ initialData: initialRef.current }
|
||||
);
|
||||
|
||||
return {
|
||||
user: data,
|
||||
loading: !data && !error,
|
||||
error,
|
||||
revalidate,
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user