Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/app/apollo-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { setContext } from "@apollo/client/link/context"

import { type ReactNode, useMemo } from "react"
import { getToken } from "../lib/auth"
import { getToken } from "@/lib/auth"

function createApolloClient() {
const httpLink = createHttpLink({
Expand Down
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Noto_Sans_JP } from "next/font/google"
import "./globals.css"

import { InitColorSchemeScript } from "@mui/material"
import theme from "../theme"
import theme from "@/theme"
import ApolloProviderClient from "./apollo-provider"

const notoSansJp = Noto_Sans_JP({
Expand Down
2 changes: 1 addition & 1 deletion src/app/login.tsx → src/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LoginForm from "../components/login-form"
import LoginForm from "@/components/login-form"

export default function LoginPage() {
return <LoginForm />
Expand Down
2 changes: 1 addition & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Monitor from "../components/monitor"
import Monitor from "@/components/monitor"

export default function Home() {
return <Monitor />
Expand Down
2 changes: 1 addition & 1 deletion src/components/app_bar/account_menu_icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AccountCircleIcon from "@mui/icons-material/AccountCircle"
import { Box, IconButton, Menu, MenuItem } from "@mui/material"
import { useRouter } from "next/navigation"
import { useState } from "react"
import { setToken } from "../../lib/auth"
import { setToken } from "@/lib/auth"

export default function AccountMenuIcon() {
const apolloClient = useApolloClient()
Expand Down
23 changes: 23 additions & 0 deletions src/components/auth-redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client"

import { usePathname, useRouter } from "next/navigation"
import { useEffect } from "react"
import { hasToken } from "@/lib/auth"

export function useAuthRedirect() {
const router = useRouter()
const pathname = usePathname()

useEffect(() => {
const loggedIn = hasToken()

if (!loggedIn && pathname !== "/login") {
router.replace("/login")
return
}

if (loggedIn && pathname === "/login") {
router.replace("/")
}
}, [router, pathname])
}
12 changes: 4 additions & 8 deletions src/components/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import { Box, Button, Container, TextField } from "@mui/material"
import Head from "next/head"
import { useRouter } from "next/navigation"
import { useEffect } from "react"
import { Controller, useForm } from "react-hook-form"
import { hasToken, setToken } from "../lib/auth"
import { setToken } from "@/lib/auth"
import AnonymousAppBar from "./app_bar/anonymous_app_bar"
import { useAuthRedirect } from "./auth-redirect"

export interface LoginValues {
token: string
}

export default function LoginForm() {
useAuthRedirect()

const router = useRouter()
const { control, handleSubmit } = useForm<LoginValues>({
defaultValues: {
Expand All @@ -25,12 +27,6 @@ export default function LoginForm() {
router.push("/")
}

useEffect(() => {
if (hasToken()) {
router.push("/")
}
})

return (
<div>
<Head>
Expand Down
25 changes: 9 additions & 16 deletions src/components/monitor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ import {
Toolbar,
Typography,
} from "@mui/material"
import { useRouter } from "next/navigation"
import { useEffect, useState } from "react"
import MainAppBar from "@/components/app_bar/main_app_bar"
import Co2Chart from "@/components/chart/co2_chart"
import HumidityChart from "@/components/chart/humidity_chart"
import LightChart from "@/components/chart/light_chart"
import TemperatureChart from "@/components/chart/temperature_chart"
import SmokePingChartImage from "@/components/smoke_ping_chart_image"
import dayjs from "@/lib/dayjs"
import MainAppBar from "../components/app_bar/main_app_bar"
import Co2Chart from "../components/chart/co2_chart"
import HumidityChart from "../components/chart/humidity_chart"
import LightChart from "../components/chart/light_chart"
import TemperatureChart from "../components/chart/temperature_chart"
import SmokePingChartImage from "../components/smoke_ping_chart_image"
import { hasToken } from "../lib/auth"
import { useGetMonitorQuery } from "../lib/graphql-types"
import { useGetMonitorQuery } from "@/lib/graphql-types"
import { useAuthRedirect } from "./auth-redirect"

export default function Monitor() {
const router = useRouter()
useAuthRedirect()

const smokePingNames =
process.env.NEXT_PUBLIC_SMOKEPING_NAMES?.split(",") ?? []
Expand Down Expand Up @@ -114,12 +113,6 @@ export default function Monitor() {
? calibratedData?.mhz19Co2?.[co2DataCount - 1]?.value
: undefined

useEffect(() => {
if (!hasToken()) {
router.push("/login/")
}
})

// Refresh data every 1 minute
// Reload page every 60 minutes
useEffect(() => {
Expand Down