route.ts 988 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { cookies } from 'next/headers';
  3. import { signOut } from '@/lib/auth';
  4. export async function POST(request: NextRequest) {
  5. try {
  6. // 清除NextAuth session
  7. try {
  8. await signOut({ redirect: false });
  9. } catch (error) {
  10. // NextAuth signOut可能会失败,但不影响继续清理
  11. console.log('NextAuth signOut 错误:', error);
  12. }
  13. // 清除我们自己的JWT token cookie
  14. const cookieStore = await cookies();
  15. cookieStore.delete('auth-token');
  16. return NextResponse.json(
  17. { message: 'Logout successful' },
  18. {
  19. status: 200,
  20. headers: {
  21. 'Set-Cookie': 'authToken=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure; SameSite=Strict'
  22. }
  23. }
  24. );
  25. } catch (error) {
  26. console.error('退出登录错误:', error);
  27. return NextResponse.json(
  28. { error: '退出登录失败' },
  29. { status: 500 }
  30. );
  31. }
  32. }