InvalidJoker
InvalidJoker
Explore posts from servers
HHono
Created by InvalidJoker on 7/14/2024 in #help
Cookies not available in other Route
r.get("/login/:userId", async (c): Promise<Response> => {
const redirectUri = `${c.env.WORKER_BASE_URL}/callback`;

setCookie(c, "userId", c.req.param("userId"), {
secure: true,
sameSite: "None",
httpOnly: true,
domain: c.env.WORKER_BASE_URL,
});

console.log(getCookie(c, "userId"));

const spotifyAuthUrl =
`https://accounts.spotify.com/authorize?` +
`response_type=code&client_id=${c.env.SPOTIFY_CLIENT_ID}` +
`&scope=playlist-modify-public` +
`&redirect_uri=${redirectUri}`;

return Response.redirect(spotifyAuthUrl, 302);
});

router.get("/callback", async (c): Promise<Response> => {
const { env } = c;
const request = c.req;
const url = new URL(request.url);
const code = url.searchParams.get("code");
if (!code) {
return new Response("No code found", { status: 400 });
}

const userId = getCookie(c, "userId");

if (!userId) {
return new Response("No userId found", { status: 400 });
}

// set auth code to userId
await env.TUNE_STORE.put(userId, code);

console.log(code);

return new Response("Authorized");
});
r.get("/login/:userId", async (c): Promise<Response> => {
const redirectUri = `${c.env.WORKER_BASE_URL}/callback`;

setCookie(c, "userId", c.req.param("userId"), {
secure: true,
sameSite: "None",
httpOnly: true,
domain: c.env.WORKER_BASE_URL,
});

console.log(getCookie(c, "userId"));

const spotifyAuthUrl =
`https://accounts.spotify.com/authorize?` +
`response_type=code&client_id=${c.env.SPOTIFY_CLIENT_ID}` +
`&scope=playlist-modify-public` +
`&redirect_uri=${redirectUri}`;

return Response.redirect(spotifyAuthUrl, 302);
});

router.get("/callback", async (c): Promise<Response> => {
const { env } = c;
const request = c.req;
const url = new URL(request.url);
const code = url.searchParams.get("code");
if (!code) {
return new Response("No code found", { status: 400 });
}

const userId = getCookie(c, "userId");

if (!userId) {
return new Response("No userId found", { status: 400 });
}

// set auth code to userId
await env.TUNE_STORE.put(userId, code);

console.log(code);

return new Response("Authorized");
});
The Cookies are available in /login but not in /callback
18 replies