Rename AcountSession to ServerSession
Start the work of clearly distingushing client side types, server side types and types shared over the API by renaming "AccountSession" and "Session" names used on the server to "ServerSession".
This commit is contained in:
parent
16191a8dd2
commit
251e83f640
15 changed files with 39 additions and 36 deletions
|
@ -1,6 +1,5 @@
|
|||
import type { H3Event } from "h3";
|
||||
import { nextSessionId, readSessions, readSubscriptions, writeSessions, writeSubscriptions } from "~/server/database";
|
||||
import { Session } from "~/shared/types/account";
|
||||
import { nextSessionId, readSessions, readSubscriptions, ServerSession, writeSessions, writeSubscriptions } from "~/server/database";
|
||||
|
||||
const oneYearSeconds = 365 * 24 * 60 * 60;
|
||||
|
||||
|
@ -13,7 +12,7 @@ async function removeSessionSubscription(sessionId: number) {
|
|||
}
|
||||
}
|
||||
|
||||
async function clearAccountSessionInternal(event: H3Event, sessions: Session[]) {
|
||||
async function clearServerSessionInternal(event: H3Event, sessions: ServerSession[]) {
|
||||
const existingSessionCookie = await getSignedCookie(event, "session");
|
||||
if (existingSessionCookie) {
|
||||
const sessionId = parseInt(existingSessionCookie, 10);
|
||||
|
@ -27,19 +26,19 @@ async function clearAccountSessionInternal(event: H3Event, sessions: Session[])
|
|||
return false;
|
||||
}
|
||||
|
||||
export async function clearAccountSession(event: H3Event) {
|
||||
export async function clearServerSession(event: H3Event) {
|
||||
const sessions = await readSessions();
|
||||
if (await clearAccountSessionInternal(event, sessions)) {
|
||||
if (await clearServerSessionInternal(event, sessions)) {
|
||||
await writeSessions(sessions);
|
||||
}
|
||||
deleteCookie(event, "session");
|
||||
}
|
||||
|
||||
export async function setAccountSession(event: H3Event, accountId: number) {
|
||||
export async function setServerSession(event: H3Event, accountId: number) {
|
||||
const sessions = await readSessions();
|
||||
await clearAccountSessionInternal(event, sessions);
|
||||
await clearServerSessionInternal(event, sessions);
|
||||
|
||||
const newSession: Session = {
|
||||
const newSession: ServerSession = {
|
||||
accountId,
|
||||
id: await nextSessionId(),
|
||||
};
|
||||
|
@ -49,11 +48,11 @@ export async function setAccountSession(event: H3Event, accountId: number) {
|
|||
await setSignedCookie(event, "session", String(newSession.id), oneYearSeconds)
|
||||
}
|
||||
|
||||
export async function refreshAccountSession(event: H3Event, session: Session) {
|
||||
export async function refreshServerSession(event: H3Event, session: ServerSession) {
|
||||
await setSignedCookie(event, "session", String(session.id), oneYearSeconds)
|
||||
}
|
||||
|
||||
export async function getAccountSession(event: H3Event) {
|
||||
export async function getServerSession(event: H3Event) {
|
||||
const sessionCookie = await getSignedCookie(event, "session");
|
||||
if (sessionCookie) {
|
||||
const sessionId = parseInt(sessionCookie, 10);
|
||||
|
@ -62,8 +61,8 @@ export async function getAccountSession(event: H3Event) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function requireAccountSession(event: H3Event) {
|
||||
const session = await getAccountSession(event);
|
||||
export async function requireServerSession(event: H3Event) {
|
||||
const session = await getServerSession(event);
|
||||
if (!session)
|
||||
throw createError({
|
||||
status: 401,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue