-
Notifications
You must be signed in to change notification settings - Fork 24
/
Authentication.tsx
62 lines (56 loc) · 1.14 KB
/
Authentication.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { ReactNode, createContext, useState } from "react";
/**
* A simple mock user
*/
type User = {
/**
* The full name of the user
*/
name: string;
/**
* A url to a user avatar image
*/
avatar?: string;
/**
* A username
*/
username?: string;
};
/**
* Stubbed out authentication context
*/
export const AuthenticationContext = createContext<{
/**
* The current user. unauthenticated session if null.
*/
currentUser: User | null;
/**
* Method to log a user in
*/
login: (userData: User) => void;
/**
* Method to log a user out
*/
logout: () => void;
}>({
currentUser: null,
login: (_userData: User) => {},
logout: () => {},
});
/**
* Stubbed out authentication provider
*/
export function AuthenticationProvider({ children }: { children?: ReactNode }) {
const [currentUser, setCurrentUser] = useState<User | null>(null);
function login(userData: User) {
setCurrentUser(userData);
}
function logout() {
setCurrentUser(null);
}
return (
<AuthenticationContext.Provider value={{ currentUser, login, logout }}>
{children}
</AuthenticationContext.Provider>
);
}