Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added redirect and user context opt-out for private pages. #120

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const UserContext = React.createContext();

function App() {
// this default value avoids crashing for navbar and accessing private pages when not login, should be changed in the future
const [user, setUser] = useState({username: "no-user", email: "", icon: process.env.PUBLIC_URL + '/images/avatar-1.png'})
const [user, setUser] = useState({username: "", email: "", icon: process.env.PUBLIC_URL + '/images/avatar-1.png'})

// load user context if the existing cookies hasnt expire
useEffect(() => {
Expand All @@ -35,6 +35,8 @@ function App() {
})
}, (error) => {
console.log("No valid user detected")
// opt-out current user if it the token is expired
setUser({username: "no-user", email: "", icon: process.env.PUBLIC_URL + '/images/avatar-1.png'})
})
}, [])

Expand Down
13 changes: 12 additions & 1 deletion client/src/pages/CreateContest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from 'react'
import React, { useContext, useEffect } from 'react'
import { Grid, Paper } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles'

import CreateContestForm from '../components/CreateContestForm'
import { UserContext } from '../App'
import { Redirect } from 'react-router-dom'

const useStyles = makeStyles((theme) => ({
pageContainer: {
Expand All @@ -21,6 +23,15 @@ const useStyles = makeStyles((theme) => ({

export default function CreateContest() {
const classes = useStyles()
const {user, setUser} = useContext(UserContext)

useEffect(() => {
if (user.username === "no-user") {
return (
<Redirect to='/login'/>
)
}
}, [user])

return (
<Grid container justify='center' className={classes.pageContainer}>
Expand Down
10 changes: 9 additions & 1 deletion client/src/pages/SocketioConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UserContext } from '../App';
import { getMsgLog } from '../apiCalls';
import { Button, Form, InputGroup } from 'react-bootstrap';
import { makeStyles, Typography } from '@material-ui/core';
import { useHistory, useLocation } from 'react-router-dom';
import { Redirect, useHistory, useLocation } from 'react-router-dom';

let socket = io.connect(null, {port:5000, rememberTransport: false});

Expand Down Expand Up @@ -82,6 +82,14 @@ function Socketio() {
history.push('/profile/'+room.user.username)
}

useEffect( () => {
if (user.username === "no-user") {
return (
<Redirect to='/login'/>
)
}
}, [user])

return (
<CurrentSessionContext.Provider value={{room, setRoom}}>
<SessionsContext.Provider value={{sessions, setSessions}}>
Expand Down
15 changes: 13 additions & 2 deletions client/src/pages/SubmitDesign.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react'
import { useHistory } from 'react-router-dom'
import React, { useContext, useEffect } from 'react'
import { Redirect, useHistory } from 'react-router-dom'
import { makeStyles } from '@material-ui/core/styles'
import { Paper, Typography, Button, Box } from '@material-ui/core'
import { useDropzone } from 'react-dropzone'
import { UserContext } from '../App'

const useStyles = makeStyles((theme) => ({
pageContainer: {
Expand Down Expand Up @@ -63,6 +64,7 @@ const useStyles = makeStyles((theme) => ({
export default function SubmitDesign(props) {
const history = useHistory()
const classes = useStyles()
const {user, setUser} = useContext(UserContext);
const {
acceptedFiles,
fileRejections,
Expand Down Expand Up @@ -115,6 +117,15 @@ export default function SubmitDesign(props) {
})
}

useEffect(() => {
if (user.username === "no-user") {
return (
<Redirect to='/login'/>
)
}
}, [user])


return (
<div className={classes.pageContainer}>
<div {...getRootProps({ className: 'dropzone' })}>
Expand Down