mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 19:29:51 -04:00
working authenticationwrapper
This commit is contained in:
parent
3e84fc1b9d
commit
8f11ed7947
|
@ -10,10 +10,10 @@ export type AuthState = {
|
||||||
refreshAuthState: (() => void) | null;
|
refreshAuthState: (() => void) | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const AuthContext = createContext<AuthState>({
|
const Authentication = createContext<AuthState>({
|
||||||
isLoggedIn: false,
|
isLoggedIn: false,
|
||||||
user: null,
|
user: null,
|
||||||
refreshAuthState: null,
|
refreshAuthState: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default AuthContext;
|
export default Authentication;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { getMe } from '../api/api';
|
import { getMe } from '../api/api';
|
||||||
import AuthContext, { AuthState } from './AuthenticationContext';
|
import Authentication, { AuthState } from './AuthenticationContext';
|
||||||
|
|
||||||
export default function AuthenticationWrapper({
|
export default function AuthenticationWrapper({
|
||||||
children,
|
children,
|
||||||
|
@ -18,7 +18,11 @@ export default function AuthenticationWrapper({
|
||||||
const refreshAuthState = useCallback(() => {
|
const refreshAuthState = useCallback(() => {
|
||||||
if (sessionId) {
|
if (sessionId) {
|
||||||
getMe().then((user) => {
|
getMe().then((user) => {
|
||||||
|
if (user) {
|
||||||
setAuthState({ isLoggedIn: true, user, refreshAuthState });
|
setAuthState({ isLoggedIn: true, user, refreshAuthState });
|
||||||
|
} else {
|
||||||
|
setAuthState({ isLoggedIn: false, user: null, refreshAuthState });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
setAuthState({ isLoggedIn: false, user: null, refreshAuthState });
|
setAuthState({ isLoggedIn: false, user: null, refreshAuthState });
|
||||||
|
@ -33,7 +37,9 @@ export default function AuthenticationWrapper({
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
<AuthContext.Provider value={authState}>{children}</AuthContext.Provider>
|
<Authentication.Provider value={authState}>
|
||||||
|
{children}
|
||||||
|
</Authentication.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
import { ION_AUTHORIZATION_ENDPOINT } from '../api/api';
|
|
||||||
import Button from '@material-ui/core/Button';
|
import Button from '@material-ui/core/Button';
|
||||||
|
import { useContext } from 'react';
|
||||||
|
import { ION_AUTHORIZATION_ENDPOINT } from '../api/api';
|
||||||
|
import AuthenticationContext from './AuthenticationContext';
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
|
const { user, isLoggedIn } = useContext(AuthenticationContext);
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
|
@ -12,11 +15,15 @@ export default function Home() {
|
||||||
>
|
>
|
||||||
<h1>Home</h1>
|
<h1>Home</h1>
|
||||||
<div style={{ display: 'flex', flexDirection: 'row' }}>
|
<div style={{ display: 'flex', flexDirection: 'row' }}>
|
||||||
|
{!isLoggedIn ? (
|
||||||
<Button
|
<Button
|
||||||
onClick={() => (window.location.href = ION_AUTHORIZATION_ENDPOINT)}
|
onClick={() => (window.location.href = ION_AUTHORIZATION_ENDPOINT)}
|
||||||
>
|
>
|
||||||
Sign In with Ion
|
Sign In with Ion
|
||||||
</Button>
|
</Button>
|
||||||
|
) : (
|
||||||
|
'Hello ' + user?.first_name + '!'
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="d-flex flex-column">
|
<div className="d-flex flex-column">
|
||||||
<section
|
<section
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
export default function getSessionId() {
|
export default function getSessionId() {
|
||||||
return localStorage.getItem('session_id');
|
return localStorage.getItem('session_token');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user