so I’ve been watching tutorial about nextAuthv5
, and after finishing the tutorial, I created my own api named appointment
and created a simple get method
but when I run the code, I received HTML
instead of a simple response. It works when I call it on my website(paste the url in google) , but when I tried to call it in my postmark
i received an html. I think it affects after changing the middleware.
api/appointment/route.js
export async function GET(request: Request) {
return NextResponse.json({ msg: 'Hello from server' })
}
This is my folder structure
+-- api
| |
| +-- appointment
| -- route.ts
| +-- auth
|
middleware.ts
export default auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
const isAuthRoute = authRoutes.includes(nextUrl.pathname);
if(isApiAuthRoute){
return ;
}
if(isAuthRoute){
if(isLoggedIn){
return Response.redirect(new URL("/auth/login", nextUrl))
}
return ;
}
if(!isLoggedIn && !isPublicRoute){
return Response.redirect(new URL('/auth/login', nextUrl))
}
return ;
})
// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: ["/((?!.+\.[\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
}
routes.ts
/**
* Array of routes that are acccessible to the public
* These routes do not required authnetication
* @type {string[]}
*/
export const publicRoutes = [
"/",
]
/**
* Array of routes that are acccessible to the public
* These routes will redicretct to looged in uesrs to settings
* @type {string[]}
*/
export const authRoutes = [
"/auth/login",
"/auth/register",
"/auth/error",
// "/auth/reset",
// '/auth/new-password'
]
/**
* The prefix for api authentication routes
* Routes that start with prefix are used for API
* @type {string}
*/
export const apiAuthPrefix = "/api/auth"
/**
* The default rediect after looging in
* @type {string}
*/
export const DEFAULT_LOGIN_REDIRECT = "/admin"
This is what I get from my postmark