mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 11:20:17 -04:00
fix session tokens
This commit is contained in:
parent
a494c728eb
commit
934b4dfb38
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,7 +1,7 @@
|
||||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
|
|
||||||
# dependencies
|
# dependencies
|
||||||
/node_modules
|
node_modules
|
||||||
/.pnp
|
/.pnp
|
||||||
.pnp.js
|
.pnp.js
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ export default function AuthenticationWrapper({
|
||||||
}: {
|
}: {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}) {
|
}) {
|
||||||
const sessionId = localStorage.getItem('session_id');
|
const sessionToken = localStorage.getItem('session_token');
|
||||||
// Prevent race conditions
|
// Prevent race conditions
|
||||||
const [authState, setAuthState] = useState<AuthState>({
|
const [authState, setAuthState] = useState<AuthState>({
|
||||||
isLoggedIn: null,
|
isLoggedIn: null,
|
||||||
|
@ -16,7 +16,7 @@ export default function AuthenticationWrapper({
|
||||||
});
|
});
|
||||||
|
|
||||||
const refreshAuthState = useCallback(() => {
|
const refreshAuthState = useCallback(() => {
|
||||||
if (sessionId) {
|
if (sessionToken) {
|
||||||
getMe().then((user) => {
|
getMe().then((user) => {
|
||||||
if (user) {
|
if (user) {
|
||||||
setAuthState({ isLoggedIn: true, user, refreshAuthState });
|
setAuthState({ isLoggedIn: true, user, refreshAuthState });
|
||||||
|
@ -27,7 +27,7 @@ export default function AuthenticationWrapper({
|
||||||
} else {
|
} else {
|
||||||
setAuthState({ isLoggedIn: false, user: null, refreshAuthState });
|
setAuthState({ isLoggedIn: false, user: null, refreshAuthState });
|
||||||
}
|
}
|
||||||
}, [sessionId]);
|
}, [sessionToken]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
refreshAuthState();
|
refreshAuthState();
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
import { useEffect, useState } from 'react';
|
import { useContext, useEffect, useState } from 'react';
|
||||||
import { Redirect, useLocation, useParams } from 'react-router-dom';
|
import { Redirect, useLocation, useParams } from 'react-router-dom';
|
||||||
import { API_ENDPOINT } from '../api/api';
|
import { API_ENDPOINT } from '../api/api';
|
||||||
|
import AuthenticationContext from './AuthenticationContext';
|
||||||
|
|
||||||
export default function Authenticator() {
|
export default function Authenticator() {
|
||||||
const { provider } = useParams<{ provider: string }>();
|
const { provider } = useParams<{ provider: string }>();
|
||||||
const query = new URLSearchParams(useLocation().search);
|
const query = new URLSearchParams(useLocation().search);
|
||||||
const code = query.get('code');
|
const code = query.get('code');
|
||||||
|
const { refreshAuthState } = useContext(AuthenticationContext);
|
||||||
const [status, setStatus] = useState<'pending' | 'errored' | 'authenticated'>(
|
const [status, setStatus] = useState<'pending' | 'errored' | 'authenticated'>(
|
||||||
'pending'
|
'pending'
|
||||||
);
|
);
|
||||||
|
@ -25,6 +27,7 @@ export default function Authenticator() {
|
||||||
response.json().then((json) => {
|
response.json().then((json) => {
|
||||||
if (json.status === 'success') {
|
if (json.status === 'success') {
|
||||||
localStorage.setItem('session_token', json.token);
|
localStorage.setItem('session_token', json.token);
|
||||||
|
refreshAuthState && refreshAuthState();
|
||||||
setStatus('authenticated');
|
setStatus('authenticated');
|
||||||
} else {
|
} else {
|
||||||
setStatus('errored');
|
setStatus('errored');
|
||||||
|
|
|
@ -6,7 +6,7 @@ import CardContent from '@material-ui/core/CardContent';
|
||||||
import { makeStyles } from '@material-ui/core/styles';
|
import { makeStyles } from '@material-ui/core/styles';
|
||||||
import Typography from '@material-ui/core/Typography';
|
import Typography from '@material-ui/core/Typography';
|
||||||
import { useContext, useEffect, useState } from 'react';
|
import { useContext, useEffect, useState } from 'react';
|
||||||
import { API_ENDPOINT } from '../api/api';
|
import { makeAPIGetCall } from '../api/utils';
|
||||||
import AuthenticationContext from './AuthenticationContext';
|
import AuthenticationContext from './AuthenticationContext';
|
||||||
|
|
||||||
const useStyles = makeStyles({
|
const useStyles = makeStyles({
|
||||||
|
@ -21,7 +21,8 @@ const useStyles = makeStyles({
|
||||||
const Profile = () => {
|
const Profile = () => {
|
||||||
const { user, isLoggedIn } = useContext(AuthenticationContext);
|
const { user, isLoggedIn } = useContext(AuthenticationContext);
|
||||||
const [groups, setGroups] = useState<Carpool.Group[]>([]);
|
const [groups, setGroups] = useState<Carpool.Group[]>([]);
|
||||||
const [pools, setPools] = useState([
|
const [pools, setPools] = useState<Carpool.Pool[]>([
|
||||||
|
/*
|
||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
pool_title: 'TJ Carpool',
|
pool_title: 'TJ Carpool',
|
||||||
|
@ -69,19 +70,17 @@ const Profile = () => {
|
||||||
comments: [
|
comments: [
|
||||||
'What is the covid vaccination status of all the participants?',
|
'What is the covid vaccination status of all the participants?',
|
||||||
],
|
],
|
||||||
},
|
},*/
|
||||||
]);
|
]);
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log(process.env);
|
console.log(process.env);
|
||||||
fetch(`${API_ENDPOINT}/my_pools`)
|
makeAPIGetCall('/my_pools').then((response) => {
|
||||||
.then((response) => response.json())
|
if (response.data.data) {
|
||||||
.then((json) => {
|
setPools(response.data.data);
|
||||||
if (json) {
|
}
|
||||||
setPools(json.data);
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
@ -113,14 +112,14 @@ const Profile = () => {
|
||||||
<CardActionArea href={'/pool/' + pool.id}>
|
<CardActionArea href={'/pool/' + pool.id}>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<Typography gutterBottom variant="h5" component="h2">
|
<Typography gutterBottom variant="h5" component="h2">
|
||||||
{pool.pool_title}
|
{pool.title}
|
||||||
</Typography>
|
</Typography>
|
||||||
<Typography
|
<Typography
|
||||||
variant="body2"
|
variant="body2"
|
||||||
color="textSecondary"
|
color="textSecondary"
|
||||||
component="p"
|
component="p"
|
||||||
>
|
>
|
||||||
{pool.pool_text}
|
{pool.description}
|
||||||
</Typography>
|
</Typography>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</CardActionArea>
|
</CardActionArea>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user