Last active
October 18, 2024 07:27
-
-
Save sirius0486/6869e42ad516564996f8f6b5be80bdbc to your computer and use it in GitHub Desktop.
route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextResponse } from "next/server" | |
export async function GET(request: Request) { | |
try { | |
const headers = request.headers | |
const headersObj: { [key: string]: string } = {} | |
headers.forEach((value, key) => { | |
headersObj[key] = value | |
}) | |
const cookieHeader = headers.get("cookie") | |
let authorization = null | |
if (cookieHeader) { | |
const cookies = Object.fromEntries( | |
cookieHeader.split("; ").map((cookie) => cookie.split("=")) | |
) | |
authorization = cookies["authorization"] | |
} | |
console.log("All Headers:", headersObj) | |
const response = NextResponse.redirect(new URL("/", request.url)) | |
if (authorization) { | |
response.cookies.set({ | |
name: "access_token", | |
value: JSON.stringify(headersObj), | |
path: "/", | |
httpOnly: true, | |
secure: true, | |
maxAge: 60 * 60 * 24, | |
}) | |
} | |
return response | |
} catch (error) { | |
return NextResponse.redirect(new URL("/401", request.url)) | |
} | |
} | |
export async function POST(request: Request) { | |
try { | |
const headers = request.headers | |
const headersObj: { [key: string]: string } = {} | |
headers.forEach((value, key) => { | |
headersObj[key] = value | |
}) | |
const cookieHeader = headers.get("cookie") | |
let authorization = null | |
if (cookieHeader) { | |
const cookies = Object.fromEntries( | |
cookieHeader.split("; ").map((cookie) => cookie.split("=")) | |
) | |
authorization = cookies["authorization"] | |
} | |
console.log("All Headers:", headersObj) | |
const response = NextResponse.redirect(new URL("/", request.url)) | |
if (authorization) { | |
response.cookies.set({ | |
name: "access_token", | |
value: JSON.stringify(headersObj), | |
path: "/", | |
httpOnly: true, | |
secure: true, | |
maxAge: 60 * 60 * 24, | |
}) | |
} | |
return response | |
} catch (error) { | |
return NextResponse.redirect(new URL("/401", request.url)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment