mirror of
https://github.com/immich-app/immich.git
synced 2025-12-26 09:14:58 +03:00
refactor(server): cookies (#8920)
This commit is contained in:
36
server/src/utils/response.ts
Normal file
36
server/src/utils/response.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { CookieOptions, Response } from 'express';
|
||||
import { Duration } from 'luxon';
|
||||
import { CookieResponse, ImmichCookie } from 'src/dtos/auth.dto';
|
||||
|
||||
export const respondWithCookie = <T>(res: Response, body: T, { isSecure, values }: CookieResponse) => {
|
||||
const defaults: CookieOptions = {
|
||||
path: '/',
|
||||
sameSite: 'lax',
|
||||
httpOnly: true,
|
||||
secure: isSecure,
|
||||
maxAge: Duration.fromObject({ days: 400 }).toMillis(),
|
||||
};
|
||||
|
||||
const cookieOptions: Record<ImmichCookie, CookieOptions> = {
|
||||
[ImmichCookie.AUTH_TYPE]: defaults,
|
||||
[ImmichCookie.ACCESS_TOKEN]: defaults,
|
||||
// no httpOnly so that the client can know the auth state
|
||||
[ImmichCookie.IS_AUTHENTICATED]: { ...defaults, httpOnly: false },
|
||||
[ImmichCookie.SHARED_LINK_TOKEN]: { ...defaults, maxAge: Duration.fromObject({ days: 1 }).toMillis() },
|
||||
};
|
||||
|
||||
for (const { key, value } of values) {
|
||||
const options = cookieOptions[key];
|
||||
res.cookie(key, value, options);
|
||||
}
|
||||
|
||||
return body;
|
||||
};
|
||||
|
||||
export const respondWithoutCookie = <T>(res: Response, body: T, cookies: ImmichCookie[]) => {
|
||||
for (const cookie of cookies) {
|
||||
res.clearCookie(cookie);
|
||||
}
|
||||
|
||||
return body;
|
||||
};
|
||||
Reference in New Issue
Block a user