-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72d764d
commit f2168ad
Showing
16 changed files
with
379 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,51 @@ | ||
export const COOKIE_NAME_INGAME_AUTH_URL_ID = 'ingameAuthUrlId'; | ||
export const COOKIE_NAME_THEME = 'theme'; | ||
import * as v from 'valibot'; | ||
|
||
export class CookieDefinition<T> { | ||
readonly name: string; | ||
readonly schema: v.BaseSchema<string | null | undefined, T, v.BaseIssue<unknown>>; | ||
readonly maxAge: number; | ||
readonly httpOnly: boolean; | ||
readonly sameSite: 'strict' | 'lax' | 'none'; | ||
|
||
constructor({ | ||
name, | ||
schema, | ||
maxAge, | ||
httpOnly, | ||
sameSite, | ||
}: { | ||
name: string; | ||
schema: v.BaseSchema<string | null | undefined, T, v.BaseIssue<unknown>>; | ||
maxAge?: number; | ||
httpOnly?: boolean; | ||
sameSite?: 'strict' | 'lax' | 'none'; | ||
}) { | ||
this.name = name; | ||
this.schema = schema; | ||
this.maxAge = maxAge ?? 60 * 60 * 24 * 365 * 1; // 1 year | ||
this.httpOnly = httpOnly ?? true; | ||
this.sameSite = sameSite ?? 'strict'; | ||
} | ||
} | ||
|
||
export type CookieNameLike = string | CookieDefinition<unknown>; | ||
export function getCookieName(name: CookieNameLike): string { | ||
return typeof name === 'string' ? name : name.name; | ||
} | ||
|
||
export const COOKIE_INGAME_AUTH_URL_ID = new CookieDefinition({ | ||
name: 'ingameAuthUrlId', | ||
schema: v.nullish(v.pipe(v.string(), v.uuid())), | ||
maxAge: 60 * 15, // 15 minutes | ||
sameSite: 'lax', // not quite sure why this needs to be lax, but the cookie seems to be not present after the redirect from the discord login | ||
}); | ||
|
||
export const COOKIE_THEME = new CookieDefinition({ | ||
name: 'theme', | ||
schema: v.pipe( | ||
v.nullish(v.string()), | ||
v.transform((v) => (v === 'light' ? 'light' : 'dark')), | ||
), | ||
maxAge: 60 * 60 * 24 * 365 * 10, // 10 year | ||
httpOnly: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
import { CustomResponse, json, RouterResponseInit } from '@solidjs/router'; | ||
import { CustomResponse, json, redirect, RouterResponseInit } from '@solidjs/router'; | ||
import { ServerCookies } from './cookies-server'; | ||
import { combineHeaders } from './headers'; | ||
|
||
export function jsonWithCookies<T>(data: T, cookies: ServerCookies, init?: RouterResponseInit): CustomResponse<T> { | ||
export function withCookies( | ||
cookies: ServerCookies, | ||
init?: RouterResponseInit | undefined, | ||
): RouterResponseInit | undefined { | ||
let initModified = init; | ||
|
||
const headers = cookies.getSetHeaders(); | ||
console.log({ headers }); | ||
|
||
if (headers.length) { | ||
initModified ??= {}; | ||
initModified.headers = combineHeaders(initModified.headers, headers); | ||
} | ||
|
||
return initModified; | ||
} | ||
|
||
export function jsonWithCookies<T>(data: T, cookies: ServerCookies, init?: RouterResponseInit): CustomResponse<T> { | ||
const initModified = withCookies(cookies, init); | ||
console.log(JSON.stringify({ initModified }, null, 2)); | ||
return json(data, initModified); | ||
} | ||
|
||
export function redirectWithCookies( | ||
url: string, | ||
cookies: ServerCookies, | ||
init?: RouterResponseInit, | ||
): CustomResponse<never> { | ||
const initModified = withCookies(cookies, init); | ||
return redirect(url, initModified); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { APIEvent } from '@solidjs/start/server'; | ||
import { ingameAuthGetByUrlId } from '~/server/ingameauth/get-active-by-url-id-set-cookie'; | ||
|
||
export async function GET({ params }: APIEvent) { | ||
console.log('urkId', params.urlId); | ||
return await ingameAuthGetByUrlId({ urlId: params.urlId }); | ||
} |
Oops, something went wrong.