Skip to content

Commit

Permalink
feat: logout user
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitarcitano committed Jul 4, 2021
1 parent d7f07a9 commit 1459321
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/auth/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { createContext, useContext, useState } from 'react';
import type { AuthContextData, User, WithChildren } from './interfaces';
import { getStorageItem } from '../storage';
import { getStorageItem, removeStorageItem } from '../storage';

const AuthContext = createContext<AuthContextData>({} as AuthContextData);

Expand All @@ -19,12 +19,22 @@ export function AuthProvider({ children }: WithChildren): JSX.Element {
}
}

async function logout(): Promise<void> {
try {
await removeStorageItem('userApiToken');
await checkIfUserIsLoggedIn();
} catch (err) {
console.log(err);
}
}

return (
<AuthContext.Provider
value={{
checkIfUserIsLoggedIn,
setUser,
user,
logout,
}}
>
{children}
Expand Down
1 change: 1 addition & 0 deletions src/auth/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface AuthContextData {
user: User;
setUser: Dispatch<React.SetStateAction<User>>;
checkIfUserIsLoggedIn: () => void;
logout: () => void;
}

export type WithChildren = {
Expand Down

0 comments on commit 1459321

Please sign in to comment.