RayGG

login/routes.ts

Apr 28th, 2024 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import { cookies } from "next/headers";
  2. import { redirect } from "next/navigation";
  3.  
  4. import { createSession, deleteSession } from "./api";
  5.  
  6. const defaultCookieOptions = {
  7. secure: process.env.NEXT_PUBLIC_BASE_URL?.startsWith("https://"),
  8. httpOnly: true,
  9. sameSite: "none",
  10. domain: process.env.NEXT_PUBLIC_BASE_URL?.split("://")[1],
  11. expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7 * 4)
  12. } as const;
  13.  
  14. export async function GET(request: Request) {
  15. const { searchParams } = new URL(request.url);
  16. const cookieStore = cookies();
  17.  
  18. const logout = searchParams.get("logout");
  19. const session = cookieStore.get("session");
  20.  
  21. if (logout) {
  22.  
  23. if (!session?.value) {
  24. redirect("/");
  25. }
  26.  
  27. const res = await deleteSession(session.value);
  28.  
  29. if (
  30. (res !== true || typeof res !== "boolean" && "statusCode" in res) &&
  31. res?.statusCode !== 401
  32. ) {
  33. const data = { statusCode: 500, message: res?.message || "An error occurred" };
  34. console.log(data);
  35. return Response.json(data);
  36. }
  37.  
  38. cookieStore.set(
  39. "session",
  40. "",
  41. {
  42. expires: new Date(0),
  43. domain: process.env.NEXT_PUBLIC_BASE_URL?.split("://")[1]
  44. }
  45. );
  46.  
  47. cookieStore.set(
  48. "hasSession",
  49. "",
  50. {
  51. expires: new Date(0),
  52. domain: process.env.NEXT_PUBLIC_BASE_URL?.split("://")[1]
  53. }
  54. );
  55.  
  56. redirect("/");
  57. }
  58.  
  59. const invite = searchParams.get("invite");
  60. const code = searchParams.get("code");
  61.  
  62. if (!code) {
  63. const callback = searchParams.get("callback");
  64. const lastpage = cookieStore.get("lastpage");
  65.  
  66. redirect(`${process.env.NEXT_PUBLIC_LOGIN}${invite ? "+bot" : ""}&state=${encodeURIComponent(callback || lastpage?.value || "/")}`);
  67. }
  68.  
  69. const res = await createSession(code, session?.value);
  70.  
  71. if (!res || "statusCode" in res) {
  72. const data = { statusCode: 500, message: res?.message || "An error occurred" };
  73. console.log(data);
  74. return Response.json(data);
  75. }
  76.  
  77. cookieStore.set(
  78. "session",
  79. res.session,
  80. defaultCookieOptions
  81. );
  82.  
  83. cookieStore.set(
  84. "hasSession",
  85. "true",
  86. defaultCookieOptions,
  87. );
  88.  
  89. console.log(cookieStore.getAll());
  90. // RETURN THE 2 COOKIES IN ARRAY []
  91.  
  92. const guildId = searchParams.get("guild_id");
  93. let redirectUrl = decodeURIComponent(searchParams.get("state") || "/");
  94.  
  95. if (redirectUrl.includes("://")) redirectUrl = "/";
  96.  
  97. redirect(
  98. guildId
  99. ? `/dashboard/${guildId}`
  100. : redirectUrl
  101. );
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment