implement oauth2 login
This commit is contained in:
3
app/api/auth/[...nextauth]/route.ts
Normal file
3
app/api/auth/[...nextauth]/route.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { handlers } from "@/src/lib/auth";
|
||||
|
||||
export const { GET, POST } = handlers;
|
||||
@@ -1,24 +0,0 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { finalizeOAuthLogin } from "@/src/lib/auth/oauth";
|
||||
import { createSession } from "@/src/lib/auth/session";
|
||||
import { config } from "@/src/lib/config";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const url = new URL(request.url);
|
||||
const code = url.searchParams.get("code");
|
||||
const state = url.searchParams.get("state");
|
||||
|
||||
if (!code || !state) {
|
||||
return NextResponse.redirect(new URL("/login?error=invalid_response", config.baseUrl));
|
||||
}
|
||||
|
||||
try {
|
||||
const { user, redirectTo } = await finalizeOAuthLogin(code, state);
|
||||
await createSession(user.id);
|
||||
const destination = redirectTo && redirectTo.startsWith("/") ? redirectTo : "/";
|
||||
return NextResponse.redirect(new URL(destination, config.baseUrl));
|
||||
} catch (error) {
|
||||
console.error("OAuth callback failed", error);
|
||||
return NextResponse.redirect(new URL("/login?error=oauth_failed", config.baseUrl));
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { destroySession } from "@/src/lib/auth/session";
|
||||
import { config } from "@/src/lib/config";
|
||||
import { signOut } from "@/src/lib/auth";
|
||||
|
||||
export async function POST() {
|
||||
await destroySession();
|
||||
return NextResponse.redirect(new URL("/login", config.baseUrl));
|
||||
await signOut({ redirectTo: "/login" });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user