Pass cookies from a route handler (on another subdomain) to middleware
Unanswered
Dogue Brasileiro posted this in #help-forum
Dogue BrasileiroOP
So i have a route handler on
The problem is i can't set this access_token as a cookie and pass it to the middleware.
This is my code:
/api/auth/google:
middleware:
I am guessing its because the response never sets the cookie in the browser? how do i pass this cookie from route handler to middleware?
localhost:3000/api/auth/google, it receives a refresh_token cookie and creates an access_token.The problem is i can't set this access_token as a cookie and pass it to the middleware.
This is my code:
/api/auth/google:
export async function GET(request: NextRequest) {
console.log(request.url);
const refreshToken = request.cookies.get("refresh_token")?.value;
if (!refreshToken) {
return new Response("Unauthorized", { status: 401 });
}
const tokenData = await getAccessToken(refreshToken);
const response = NextResponse.redirect(`https://app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}/`, { status: 302 });
response.cookies.set(process.env.NEXT_PUBLIC_AUTH_COOKIE_NAME!, JSON.stringify(tokenData), {sameSite:'lax', secure:true})
return response;
}middleware:
if (hostname == `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
const userTokenCk = req.cookies.get(process.env.NEXT_PUBLIC_AUTH_COOKIE_NAME!)?.value
console.log(userTokenCk) //undefined always
}I am guessing its because the response never sets the cookie in the browser? how do i pass this cookie from route handler to middleware?