Skip to content

Commit

Permalink
Redirect to sign in page automatically (#498)
Browse files Browse the repository at this point in the history
Automatically redirect to the sign in page if a non-authenticated user tries to access a page for signed in users only.
  • Loading branch information
iamlogand authored Jul 11, 2024
1 parent 0681b52 commit e463970
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
13 changes: 8 additions & 5 deletions frontend/components/GamePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "react"
import Head from "next/head"
import useWebSocket from "react-use-websocket"
import { useRouter } from "next/router"

import Tabs from "@mui/material/Tabs"
import Tab from "@mui/material/Tab"
Expand Down Expand Up @@ -72,6 +73,7 @@ const GamePage = (props: GamePageProps) => {
const [latestTokenRefreshDate, setLatestTokenRefreshDate] =
useState<Date | null>(null)
const [refreshingToken, setRefreshingToken] = useState<boolean>(false)
const router = useRouter()

// Game-specific state
const {
Expand Down Expand Up @@ -115,13 +117,13 @@ const GamePage = (props: GamePageProps) => {

// Add class to body element on this page only, for styling
useEffect(() => {
document.body.classList.add('game');
document.body.classList.add("game")

// Cleanup function
return () => {
document.body.classList.remove('game');
};
}, []);
document.body.classList.remove("game")
}
}, [])

// Establish a WebSocket connection to the game group and provide a state containing the last message
const { lastMessage: lastGameMessage } = useWebSocket(
Expand Down Expand Up @@ -630,7 +632,8 @@ const GamePage = (props: GamePageProps) => {
if (!syncingGameData) {
// Render page error if user is not signed in
if (user === null || props.authFailure) {
return <PageError statusCode={401} />
router.push(`/sign-in?redirect=${router.asPath}`)
return <PageError />
}

// Render page error if game doesn't exist or hasn't started yet
Expand Down
4 changes: 3 additions & 1 deletion frontend/components/PageError.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCookieContext } from "@/contexts/CookieContext"

interface PageErrorProps {
statusCode: number
statusCode?: number
}

// This component is designed to replace the entire `<main>` element of a page with an error message
Expand All @@ -16,6 +16,8 @@ const PageError = (props: PageErrorProps) => {
case 401:
message = "Unauthorized"
break
case undefined:
return <main className="standard-page" /> // If the status code is not recognized, return an empty main element
}

const getSuggestion = () => {
Expand Down
7 changes: 4 additions & 3 deletions frontend/pages/account.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { GetServerSidePropsContext } from "next"
import Head from "next/head"
import Card from "@mui/material/Card"
import { useRouter } from "next/router"

import { useCookieContext } from "@/contexts/CookieContext"
import getInitialCookieData from "@/functions/cookies"
import PageError from "@/components/PageError"
import Breadcrumb from "@/components/Breadcrumb"
import KeyValueList from "@/components/KeyValueList"
import Box from "@mui/material/Box"

/**
* The component for the account page
*/
const AccountPage = () => {
const { user } = useCookieContext()
const router = useRouter()

// Render page error if user is not signed in
if (user === null) {
return <PageError statusCode={401} />
router.push(`/sign-in?redirect=${router.asPath}`)
return <PageError />
}

const pairs = [
Expand Down
22 changes: 14 additions & 8 deletions frontend/pages/games/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,10 @@ const GameLobbyPage = (props: GameLobbyPageProps) => {
}
}, [props.authFailure, setAccessToken, setRefreshToken, setUser])

// Render page error if user is not signed in
// Redirect to sign in if user is not signed in
if (user === null || props.authFailure) {
return <PageError statusCode={401} />
router.push(`/sign-in?redirect=${router.asPath}`)
return <PageError />
}

// Render page error if game doesn't exist
Expand Down Expand Up @@ -540,11 +541,12 @@ const GameLobbyPage = (props: GameLobbyPageProps) => {
</title>
</Head>
<main className="standard-page px-8 mb-8">
<Breadcrumb
customItems={[{ index: 2, text: "Game Lobby" }]}
/>
<Breadcrumb customItems={[{ index: 2, text: "Game Lobby" }]} />

<h2 id="page-title" className="font-semibold text-2xl tracking-tight mb-4">
<h2
id="page-title"
className="font-semibold text-2xl tracking-tight mb-4"
>
{game.name}
</h2>
{game.description && <p className="pb-4">{game.description}</p>}
Expand Down Expand Up @@ -574,7 +576,9 @@ const GameLobbyPage = (props: GameLobbyPageProps) => {
<div
style={{ marginLeft: 16, marginBottom: 6, marginRight: 16 }}
>
<h3 className="pt-2 pb-2 font-semibold text-lg tracking-tight">Players</h3>
<h3 className="pt-2 pb-2 font-semibold text-lg tracking-tight">
Players
</h3>
{!loading ? (
<p style={{ margin: 0 }}>
{players.length} of 6 spaces reserved
Expand Down Expand Up @@ -684,7 +688,9 @@ const GameLobbyPage = (props: GameLobbyPageProps) => {
{!loading && (
<div className="p-6 bg-white dark:bg-neutral-700 rounded">
<div>
<h3 className="pt-0 pb-4 font-semibold text-lg tracking-tight">Actions</h3>
<h3 className="pt-0 pb-4 font-semibold text-lg tracking-tight">
Actions
</h3>
<div className="flex gap-4">
{game.host?.id === user.id && (
<>
Expand Down
7 changes: 5 additions & 2 deletions frontend/pages/games/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useCallback, useEffect } from "react"
import { GetServerSidePropsContext } from "next"
import Head from "next/head"
import Link from "next/link"
import { useRouter } from "next/router"

import Button from "@mui/material/Button"
import { DataGrid, GridColDef, GridRenderCellParams } from "@mui/x-data-grid"
Expand All @@ -26,6 +27,7 @@ interface BrowseGamesPageProps {

// The "Browse Games" page component
const BrowseGamesPage = (props: BrowseGamesPageProps) => {
const router = useRouter()
const {
accessToken,
refreshToken,
Expand Down Expand Up @@ -157,9 +159,10 @@ const BrowseGamesPage = (props: BrowseGamesPageProps) => {
}
}, [props.authFailure, setAccessToken, setRefreshToken, setUser])

// Render page error if user is not signed in
// Redirect to sign in if user is not signed in
if (user === null || props.authFailure) {
return <PageError statusCode={401} />
router.push(`/sign-in?redirect=${router.asPath}`)
return <PageError />
}

return (
Expand Down
5 changes: 3 additions & 2 deletions frontend/pages/games/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ const NewGamePage = () => {
}
}

// Render page error if user is not signed in
// Redirect to sign in if user is not signed in
if (user === null) {
return <PageError statusCode={401} />
router.push(`/sign-in?redirect=${router.asPath}`)
return <PageError />
}

return (
Expand Down

0 comments on commit e463970

Please sign in to comment.