auth
This commit is contained in:
@@ -2,14 +2,17 @@ import React from 'react';
|
|||||||
import ReactDOM from 'react-dom/client';
|
import ReactDOM from 'react-dom/client';
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import ThemeContextProvider from "./utils/contexts/ThemeContext";
|
import ThemeContextProvider from "./utils/contexts/ThemeContext";
|
||||||
|
import AuthContextProvider from "./utils/contexts/AuthContext";
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(
|
const root = ReactDOM.createRoot(
|
||||||
document.getElementById('root') as HTMLElement
|
document.getElementById('root') as HTMLElement
|
||||||
);
|
);
|
||||||
root.render(
|
root.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<ThemeContextProvider>
|
<AuthContextProvider>
|
||||||
<App />
|
<ThemeContextProvider>
|
||||||
</ThemeContextProvider>
|
<App/>
|
||||||
</React.StrictMode>
|
</ThemeContextProvider>
|
||||||
|
</AuthContextProvider>
|
||||||
|
</React.StrictMode>
|
||||||
);
|
);
|
||||||
@@ -14,8 +14,8 @@ const MainLobby = () => {
|
|||||||
const handleCreateRoom = () => {
|
const handleCreateRoom = () => {
|
||||||
fetch(ROOM_URL, {
|
fetch(ROOM_URL, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
}).then(r => r.json()).then(data => {
|
}).then(res => res.json()).then(room => {
|
||||||
navigateTo(`/room/${data}`)
|
navigateTo(`/room/${room}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
src/utils/contexts/AuthContext.tsx
Normal file
36
src/utils/contexts/AuthContext.tsx
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
const AUTH_URL = `http://${process.env.REACT_APP_API_URL}/auth`;
|
||||||
|
|
||||||
|
interface IAuthContext {
|
||||||
|
sessionToken: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const AuthContext = React.createContext<IAuthContext>({
|
||||||
|
sessionToken: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
export const useAuth = () => React.useContext(AuthContext);
|
||||||
|
|
||||||
|
const AuthContextProvider = ({children}: any) => {
|
||||||
|
const [sessionToken] = React.useState('');
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const token = window.localStorage.getItem('session_token');
|
||||||
|
if (!token) {
|
||||||
|
fetch(AUTH_URL, {
|
||||||
|
method: 'GET'
|
||||||
|
}).then(res => res.json()).then(token => {
|
||||||
|
window.localStorage.setItem('session_token', token);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AuthContext.Provider value={{sessionToken}}>
|
||||||
|
{children}
|
||||||
|
</AuthContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AuthContextProvider;
|
||||||
Reference in New Issue
Block a user