CoastalCommitsPastes/client/middleware.ts

47 lines
916 B
TypeScript
Raw Normal View History

import { getToken } from "next-auth/jwt"
import { withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"
export default withAuth(
async function middleware(req) {
const token = await getToken({ req })
const isAuth = !!token
const isAuthPage =
req.nextUrl.pathname.startsWith("/signup") ||
req.nextUrl.pathname.startsWith("/signin")
if (isAuthPage) {
if (isAuth) {
return NextResponse.redirect(new URL("/new", req.url))
}
return null
2022-04-09 20:48:19 -04:00
}
if (!isAuth) {
return NextResponse.redirect(new URL("/signin", req.url))
}
},
{
callbacks: {
async authorized() {
// This is a work-around for handling redirect on auth pages.
// We return true here so that the middleware function above
// is always called.
return true
}
}
2022-04-09 20:48:19 -04:00
}
)
export const config = {
2022-11-14 21:39:42 -05:00
matcher: [
2022-11-10 02:11:36 -05:00
// "/signout",
2022-11-09 21:38:05 -05:00
// "/",
"/signin",
"/signup",
"/new",
]
}