InvalidJoker
Explore posts from serversCookies 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");
});
18 replies