Replies: 2 comments 2 replies
-
This looks great. Currently in my project I'm manually managing these things and while it's not the worst, having a higher level API for these things would absolutely help. It definitely take some fumbling around trying to figure out how to set cookies correctly, and just generally felt cumbersome. For example, to verify my users JWT I'm currently doing: import { parse } from 'lightcookie';
import jwt from 'jsonwebtoken';
export async function isLoggedIn(req) {
const cookie = req.headers.get('cookie');
if(cookie) {
const parsed = parse(cookie);
if(parsed.jwt) {
jwt.verify(parsed.jwt, import.meta.env.JWT_SECRET, () => {});
}
}
} It's not the absolute worst thing in the world, but its just kind of unfortunate to have to have a dependency on export async function get({cookieStore}) {
const auth = isLoggedIn(cookieStore);
}
function isLoggedIn(cookieStore) {
const jwt = cookieStore.get('jwt');
jwt.verify(jwt, import.meta.env.JWT_SECRET, () => {});
} Having helpers to set cookies and delete cookies would be tremendously helpful as well. Currently I have functions that look like: export function createHeaders({jwt, location}) {
const expires = sevenDaysFromNow();
const headers = new Headers();
headers.append('Set-Cookie', `jwt=${jwt}; Expires=${expires}; Path=/; HttpOnly; Secure;`);
headers.append('Location', location);
return headers;
} It'd be really nice to have
|
Beta Was this translation helpful? Give feedback.
-
Remix contains higher-level APIs for session management. Should we skip this step and go straight there? https://remix.run/docs/en/v1/api/remix#sessions |
Beta Was this translation helpful? Give feedback.
-
Background
Dealing with cookies is cumbersome. If you want to read a cookie you need to:
Cookie
header from the request.Writing is just as challenge as you need to:
Set-Cookie
header for each cookie you are trying to set.path
.expires
And if you want to delete a cookie you have to set its
expires
to the past.Proposal
The recently introduced Cookie Store API provides a convenient way to read/write cookies through a Map-like interface. Reading a cookie is:
And writing a cookie is:
And deleting a cookie is as simple as:
Astro.cookieStore / APIContext.cookieStore
I propose that this object be added to the
Astro
global in .astro files andAPIContext
in dynamic routes.In API routes this would be used like so:
Links
Beta Was this translation helpful? Give feedback.
All reactions