2022-11-11 22:17:44 -05:00
|
|
|
import { getToken } from "next-auth/jwt"
|
|
|
|
import { withAuth } from "next-auth/middleware"
|
|
|
|
import { NextResponse } from "next/server"
|
2022-03-21 03:46:15 -04:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
export default withAuth(
|
|
|
|
async function middleware(req) {
|
|
|
|
const token = await getToken({ req })
|
2022-11-14 02:02:31 -05:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
const isAuth = !!token
|
|
|
|
const isAuthPage =
|
|
|
|
req.nextUrl.pathname.startsWith("/signup") ||
|
|
|
|
req.nextUrl.pathname.startsWith("/signin")
|
2022-03-21 03:46:15 -04:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
if (isAuthPage) {
|
|
|
|
if (isAuth) {
|
|
|
|
return NextResponse.redirect(new URL("/new", req.url))
|
|
|
|
}
|
2022-03-21 03:46:15 -04:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
return null
|
2022-04-09 20:48:19 -04:00
|
|
|
}
|
2022-04-12 19:54:02 -04:00
|
|
|
|
2022-11-11 22:17:44 -05: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-12 19:54:02 -04:00
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
}
|
2022-11-11 22:17:44 -05:00
|
|
|
)
|
2022-11-08 03:23:28 -05: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
|
|
|
// "/",
|
2022-11-08 03:23:28 -05:00
|
|
|
"/signin",
|
|
|
|
"/signup",
|
|
|
|
"/new",
|
|
|
|
]
|
|
|
|
}
|