2022-11-08 03:28:19 -05:00
|
|
|
import { NextFetchEvent, NextResponse } from "next/server"
|
|
|
|
import type { NextRequest } from "next/server"
|
2022-03-21 03:46:15 -04:00
|
|
|
|
2022-04-06 19:31:41 -04:00
|
|
|
const PUBLIC_FILE = /\.(.*)$/
|
2022-03-21 03:46:15 -04:00
|
|
|
|
2022-04-06 12:08:51 -04:00
|
|
|
export function middleware(req: NextRequest, event: NextFetchEvent) {
|
2022-04-09 20:48:19 -04:00
|
|
|
const pathname = req.nextUrl.pathname
|
2022-11-08 03:28:19 -05:00
|
|
|
const signedIn = req.cookies.get("drift-token")
|
2022-04-09 20:48:19 -04:00
|
|
|
const getURL = (pageName: string) => new URL(`/${pageName}`, req.url).href
|
|
|
|
const isPageRequest =
|
|
|
|
!PUBLIC_FILE.test(pathname) &&
|
|
|
|
// header added when next/link pre-fetches a route
|
|
|
|
!req.headers.get("x-middleware-preflight")
|
2022-03-21 03:46:15 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
if (!req.headers.get("x-middleware-preflight") && pathname === "/signout") {
|
|
|
|
// If you're signed in we remove the cookie and redirect to the home page
|
|
|
|
// If you're not signed in we redirect to the home page
|
|
|
|
if (signedIn) {
|
|
|
|
const resp = NextResponse.redirect(getURL(""))
|
2022-11-08 03:28:19 -05:00
|
|
|
resp.cookies.delete("drift-token")
|
|
|
|
resp.cookies.delete("drift-userid")
|
2022-04-09 20:48:19 -04:00
|
|
|
const signoutPromise = new Promise((resolve) => {
|
|
|
|
fetch(`${process.env.API_URL}/auth/signout`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `Bearer ${signedIn}`,
|
|
|
|
"x-secret-key": process.env.SECRET_KEY || ""
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
event.waitUntil(signoutPromise)
|
2022-04-02 03:45:26 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
} else if (isPageRequest) {
|
|
|
|
if (signedIn) {
|
|
|
|
if (
|
|
|
|
pathname === "/" ||
|
|
|
|
pathname === "/signin" ||
|
|
|
|
pathname === "/signup"
|
|
|
|
) {
|
|
|
|
return NextResponse.redirect(getURL("new"))
|
|
|
|
}
|
|
|
|
} else if (!signedIn) {
|
2022-04-18 17:48:08 -04:00
|
|
|
if (pathname.startsWith("/new")) {
|
2022-04-09 20:48:19 -04:00
|
|
|
return NextResponse.redirect(getURL("signin"))
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 19:54:02 -04:00
|
|
|
|
|
|
|
if (pathname.includes("/protected/") || pathname.includes("/private/")) {
|
2022-04-13 00:14:10 -04:00
|
|
|
const urlWithoutVisibility = pathname
|
|
|
|
.replace("/protected/", "/")
|
|
|
|
.replace("/private/", "/")
|
|
|
|
.substring(1)
|
2022-04-12 19:54:02 -04:00
|
|
|
return NextResponse.redirect(getURL(urlWithoutVisibility))
|
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
}
|
2022-04-06 17:42:18 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return NextResponse.next()
|
2022-03-21 03:46:15 -04:00
|
|
|
}
|
2022-11-08 03:28:19 -05:00
|
|
|
|
|
|
|
export const config = {
|
|
|
|
match: [
|
|
|
|
"/signout",
|
|
|
|
"/",
|
|
|
|
"/signin",
|
|
|
|
"/signup",
|
|
|
|
"/new",
|
|
|
|
"/protected/:path*",
|
|
|
|
"/private/:path*"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|