feat: logout endpoint
This commit is contained in:
41
app/[lang]/(live)/(protected)/logout/route.ts
Normal file
41
app/[lang]/(live)/(protected)/logout/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { AuthError } from "next-auth"
|
||||
|
||||
import { signOut } from "@/auth"
|
||||
import { badRequest } from "@/server/errors/next"
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const returnUrl = request.headers.get("x-returnurl")
|
||||
|
||||
// If all else fails, always redirect to startpage
|
||||
const redirectTo =
|
||||
returnUrl ||
|
||||
request.headers.get("x-redirect-to") ||
|
||||
request.nextUrl.searchParams.get("redirectTo") ||
|
||||
request.headers.get("Referer") ||
|
||||
"/"
|
||||
|
||||
try {
|
||||
/**
|
||||
* Passing `redirect: false` to `signOut` will return a result object
|
||||
* instead of automatically redirecting inside of `signOut`.
|
||||
* https://github.com/nextauthjs/next-auth/blob/3c035ec/packages/next-auth/src/lib/actions.ts#L104
|
||||
*/
|
||||
const obj = await signOut({
|
||||
redirectTo,
|
||||
redirect: false,
|
||||
})
|
||||
|
||||
if (obj) {
|
||||
return NextResponse.redirect(obj.redirect)
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
console.log({ signOutAuthError: error })
|
||||
} else {
|
||||
console.log({ signOutError: error })
|
||||
}
|
||||
}
|
||||
|
||||
return badRequest()
|
||||
}
|
||||
Reference in New Issue
Block a user