client: redirect unauthed users from all /new paths

This commit is contained in:
Max Leiter 2022-04-18 14:48:08 -07:00
parent 454ea303a6
commit 4177897691
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: A3512F2F2F17EBDA
3 changed files with 3 additions and 3 deletions

View file

@ -18,6 +18,7 @@ import CreatedAgoBadge from "@components/badges/created-ago-badge"
import Cookies from "js-cookie"
import PasswordModalPage from "./password-modal-wrapper"
import VisibilityControl from "@components/badges/visibility-control"
import useSharedState from "@lib/hooks/use-shared-state"
type Props = {
post: Post
@ -34,6 +35,7 @@ const PostPage = ({ post: initialPost, isProtected }: Props) => {
const [isOwner] = useState(
post.users ? post.users[0].id === Cookies.get("drift-userid") : false
)
const [signedIn] = useSharedState<boolean>("signedIn")
const router = useRouter()
const isMobile = useMediaQuery("mobile")

View file

@ -1,5 +1,4 @@
import Cookies from "js-cookie"
import { useRouter } from "next/router"
import { useEffect } from "react"
import useSharedState from "./use-shared-state"
@ -9,7 +8,6 @@ const useSignedIn = () => {
typeof window === "undefined" ? false : !!Cookies.get("drift-token")
)
const token = Cookies.get("drift-token")
const router = useRouter()
const signin = (token: string) => {
setSignedIn(true)
// TODO: investigate SameSite / CORS cookie security

View file

@ -45,7 +45,7 @@ export function middleware(req: NextRequest, event: NextFetchEvent) {
return NextResponse.redirect(getURL("new"))
}
} else if (!signedIn) {
if (pathname === "/new") {
if (pathname.startsWith("/new")) {
return NextResponse.redirect(getURL("signin"))
}
}