98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
import { gql } from '@apollo/client';
|
|
import { Session } from '@generated/types';
|
|
import { createContext, useContext } from 'react';
|
|
import { sessionFragment } from '../fragments';
|
|
import { useToastMutation } from '../hooks/useToastMutation';
|
|
import { url } from '../main';
|
|
|
|
interface AuthContextProps {
|
|
headers: Session | null;
|
|
setHeaders: (session: Session | null) => void;
|
|
}
|
|
|
|
export const AuthContext = createContext<AuthContextProps | null>(null);
|
|
export const AuthProvider = AuthContext.Provider;
|
|
|
|
interface UseAuthOutput extends AuthContextProps {
|
|
isAuthenticated: boolean;
|
|
login: (value: { email: string; password: string }) => Promise<Session | null | undefined>;
|
|
logout: () => Promise<void>;
|
|
}
|
|
|
|
export function useAuth(): UseAuthOutput {
|
|
const authContext = useContext(AuthContext);
|
|
const { update: login } = useToastMutation<{ login: Session }>({
|
|
mutation: loginMutation,
|
|
successMsg: 'Logged in successfully.'
|
|
});
|
|
|
|
const { update: logout } = useToastMutation({
|
|
mutation: logoutMutation,
|
|
successMsg: 'Logged out successfully.'
|
|
});
|
|
|
|
if (authContext == null) throw new Error('useAuth must be used within an AuthProvider');
|
|
|
|
return {
|
|
...authContext,
|
|
isAuthenticated: authContext.headers != null,
|
|
login: async ({ email, password }: { email: string; password: string }) => {
|
|
const { data } = await login({
|
|
variables: {
|
|
input: {
|
|
email,
|
|
password
|
|
}
|
|
}
|
|
});
|
|
if (data != null) {
|
|
authContext.setHeaders(data.login);
|
|
return data.login;
|
|
}
|
|
return data;
|
|
},
|
|
logout: async () => {
|
|
await logout();
|
|
authContext.setHeaders(null);
|
|
}
|
|
};
|
|
}
|
|
|
|
interface UseRestAuthOutput {
|
|
refreshToken: () => Promise<Session | null | undefined>;
|
|
}
|
|
|
|
export function useRestAuth(): UseRestAuthOutput {
|
|
return {
|
|
refreshToken: async () => {
|
|
try {
|
|
const response = await fetch(`${url}/api/refreshAccessToken`, {
|
|
credentials: 'include'
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`[error]: ${response.status}`);
|
|
}
|
|
const session = await response.json();
|
|
return session;
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
const loginMutation = gql`
|
|
mutation AuthLogin($input: LoginInput!) {
|
|
login(input: $input) {
|
|
...Session
|
|
}
|
|
}
|
|
|
|
${sessionFragment}
|
|
`;
|
|
|
|
const logoutMutation = gql`
|
|
mutation AuthLogout {
|
|
logout
|
|
}
|
|
`;
|