mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-04-16 10:09:07 -05:00
* Kysely initial * Badges initial * Badge routes migrated * Badges migrated * Calendar work * Fix one type problem * Calendar work * findResultsByUserId work * Calendar reworking finished * PlusSuggestions work * Migrated suggestions * Builds progress * Migrated builds * Admin migrated * Migrate articles * User search * Faster getUser * Selectable/insertable as global * Refresh prod db script + patronTier index * identifierToUserId * updateProfile * findByIdentifier * More indexes * User upsert * upsertLite * findAllPlusMembers * updateResultHighlights * updateMany * User finished migration * Fix types * Fix PlusVotingResult typing * PlusVotingRepository WIP * Migrated resultsByMonthYear * Migrated plusVotes (done with db. related migrations) * Plus code to features folder * Fix TODOs * Export * Fix range * Migrate some user pages * Move rest user routes * Move /play * Map list generator * Front page * Move map list generation logic * Move plus voting logic * Info * API * Adjust TODOs * theme * Auth * Remove TODO
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import type { User } from "~/db/types";
|
|
import { IMPERSONATED_SESSION_KEY, SESSION_KEY } from "./authenticator.server";
|
|
import { authSessionStorage } from "./session.server";
|
|
import * as UserRepository from "~/features/user-page/UserRepository.server";
|
|
|
|
export async function getUserId(
|
|
request: Request,
|
|
): Promise<Pick<User, "id"> | undefined> {
|
|
const session = await authSessionStorage.getSession(
|
|
request.headers.get("Cookie"),
|
|
);
|
|
|
|
const userId =
|
|
session.get(IMPERSONATED_SESSION_KEY) ?? session.get(SESSION_KEY);
|
|
|
|
if (!userId) return;
|
|
|
|
return { id: userId };
|
|
}
|
|
|
|
export async function getUser(request: Request) {
|
|
const userId = (await getUserId(request))?.id;
|
|
|
|
if (!userId) return;
|
|
|
|
return UserRepository.findLeanById(userId);
|
|
}
|
|
|
|
export async function requireUserId(request: Request) {
|
|
const user = await getUserId(request);
|
|
|
|
if (!user) throw new Response(null, { status: 401 });
|
|
|
|
return user;
|
|
}
|
|
|
|
export async function requireUser(request: Request) {
|
|
const user = await getUser(request);
|
|
|
|
if (!user) throw new Response(null, { status: 401 });
|
|
|
|
return user;
|
|
}
|
|
|
|
export async function isImpersonating(request: Request) {
|
|
const session = await authSessionStorage.getSession(
|
|
request.headers.get("Cookie"),
|
|
);
|
|
|
|
return Boolean(session.get(IMPERSONATED_SESSION_KEY));
|
|
}
|