added log out
This commit is contained in:
parent
b47b0f3752
commit
b5280b70a1
2 changed files with 45 additions and 19 deletions
|
|
@ -7,7 +7,7 @@ import { url } from '../main';
|
|||
|
||||
interface AuthContextProps {
|
||||
headers: Session | null;
|
||||
setHeaders: (session: Session) => void;
|
||||
setHeaders: (session: Session | null) => void;
|
||||
}
|
||||
|
||||
export const AuthContext = createContext<AuthContextProps | null>(null);
|
||||
|
|
@ -16,6 +16,7 @@ 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 {
|
||||
|
|
@ -25,6 +26,11 @@ export function useAuth(): UseAuthOutput {
|
|||
successMsg: 'Logged in successfully.'
|
||||
});
|
||||
|
||||
const { update: logout } = useToastMutation({
|
||||
mutation: logoutMutation,
|
||||
successMsg: 'Logged out successfully.'
|
||||
});
|
||||
|
||||
if (!authContext) throw new Error('useAuth must be used within an AuthProvider');
|
||||
|
||||
return {
|
||||
|
|
@ -45,6 +51,10 @@ export function useAuth(): UseAuthOutput {
|
|||
return data.login;
|
||||
}
|
||||
return data;
|
||||
},
|
||||
logout: async () => {
|
||||
await logout();
|
||||
authContext.setHeaders(null);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -81,3 +91,9 @@ const loginMutation = gql`
|
|||
|
||||
${sessionFragment}
|
||||
`;
|
||||
|
||||
const logoutMutation = gql`
|
||||
mutation AuthLogout {
|
||||
logout
|
||||
}
|
||||
`;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue