added auth provider to frontend

This commit is contained in:
overflowerror 2021-08-14 23:27:59 +02:00
parent 9cb721ac7d
commit 5ba50cb2f6
2 changed files with 42 additions and 3 deletions

View file

@ -1,11 +1,14 @@
import React from 'react';
import './App.css';
import {AuthProvider} from "./auth/AuthProvider";
function App() {
return (
<div className="App">
<h1>Hi</h1>
</div>
<AuthProvider>
<div className="App">
<h1>Hi</h1>
</div>
</AuthProvider>
);
}

View file

@ -0,0 +1,36 @@
import React, {FunctionComponent, useContext, useState} from "react";
type AuthState = {
loggedIn: boolean
login: (username: string, password: string) => Promise<string>
}
const emptyAuthState = {
loggedIn: false,
login: async () => "not implemented"
}
const AuthContext = React.createContext<AuthState>(emptyAuthState)
export const useAuth = () => {
return useContext(AuthContext)
}
type AuthProviderProps = {
}
export const AuthProvider: FunctionComponent<AuthProviderProps> = ({children}) => {
const defaultAuthState = {
...emptyAuthState
}
const [authState, setAuthState] = useState<AuthState>(defaultAuthState)
return (
<AuthContext.Provider value={authState}>
{children}
</AuthContext.Provider>
)
}