refactor(auth): switch to class-based social login and v4 route handler
Migrate from function-based social login helpers (`facebookLogin`, `googleLogin`) to class-based services (`new FacebookLogin().signIn()`, `new GoogleLogin().signIn()`), updating call sites in `AuthFacebookPanel` and `SplashButton` with try/catch error handling. The NextAuth route handler is also refactored to the v4 pattern, importing pre-built `GET` / `POST` exports from `@/lib/auth/nextauth` instead of constructing the handler inline with `NextAuth(authOptions)`.
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
/**
|
||||
* NextAuth App Router 入口
|
||||
* NextAuth App Router 入口(v4 模式)
|
||||
*
|
||||
* 路径:/api/auth/[...nextauth]
|
||||
* 自动处理 NextAuth 所有内置路由:
|
||||
* v4 用单个 handler 函数,需手动 re-export 为 `GET` / `POST`:
|
||||
* /api/auth/signin/[provider]
|
||||
* /api/auth/callback/[provider]
|
||||
* /api/auth/signout
|
||||
@@ -10,10 +9,6 @@
|
||||
* /api/auth/csrf
|
||||
* /api/auth/providers
|
||||
*/
|
||||
import NextAuth from "next-auth";
|
||||
import { GET, POST } from "@/lib/auth/nextauth";
|
||||
|
||||
import { authOptions } from "@/lib/auth/config";
|
||||
|
||||
const handler = NextAuth(authOptions);
|
||||
|
||||
export { handler as GET, handler as POST };
|
||||
export { GET, POST };
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/**
|
||||
* 路由信号 cookie 管理端点
|
||||
*
|
||||
* 用途:写/清 `cozsweet_authed` httpOnly cookie,作为 `src/router/proxy.ts`
|
||||
* 的"用户已登录"信号源。
|
||||
*
|
||||
* 与 `next-auth.session-token` 的区别:
|
||||
* - NextAuth cookie 仅社交登录设置,邮箱登录不写入
|
||||
* - 本端点由所有登录出口(社交 / 邮箱)统一调用,proxy 只需读这一个 cookie
|
||||
*
|
||||
* 调用方:
|
||||
* - 社交登录出口(nextauth-helpers.persistBackendSession,**server 端**直接用
|
||||
* `next/headers.cookies()` 写,不走本端点)
|
||||
* - 邮箱登录出口(auth-machine.emailLoginActor / emailRegisterThenLoginActor,
|
||||
* **client 端** fetch POST)
|
||||
* - 登出(nextauth-helpers.logout,client 端 fetch DELETE)
|
||||
*
|
||||
* 安全:cookie 设为 `HttpOnly` + `SameSite=Lax`,prod 额外 `Secure`。
|
||||
*/
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const COOKIE_NAME = "cozsweet_authed";
|
||||
const ONE_WEEK = 60 * 60 * 24 * 7;
|
||||
|
||||
/** 设置 httpOnly 路由信号 cookie(任何登录成功后调用) */
|
||||
export async function POST() {
|
||||
const res = NextResponse.json({ ok: true });
|
||||
res.cookies.set({
|
||||
name: COOKIE_NAME,
|
||||
value: "1",
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
maxAge: ONE_WEEK,
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
/** 清除路由信号 cookie(登出时调用) */
|
||||
export async function DELETE() {
|
||||
const res = NextResponse.json({ ok: true });
|
||||
res.cookies.set({
|
||||
name: COOKIE_NAME,
|
||||
value: "",
|
||||
path: "/",
|
||||
maxAge: 0,
|
||||
});
|
||||
return res;
|
||||
}
|
||||
Reference in New Issue
Block a user