#Type 'Promise<{ id: string; username: string; role: Role; } | null>' is missing the following proper
25 messages · Page 1 of 1 (latest)
can you share a more complete example? what are getRequestEvent and validateSessionToken?
`(alias) function getRequestEvent(): RequestEvent<Record<string, never>, "/" | "/demo" | "/demo/lucia" | "/demo/lucia/login" | null>
import getRequestEvent
Returns the current RequestEvent. Can be used inside server hooks, server load functions, actions, and endpoints (and functions called by them).
In environments without AsyncLocalStorage, this must be called synchronously (i.e. not after an await).
@since — 2.20.0
`
validateSessionToken just returns user object.
export async function validateSessionToken(token: string) {
const sessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(token)));
const [result] = await db
.select({
// Adjust user table here to tweak returned data
user: { id: table.user.id, username: table.user.username, role: table.user.role },
session: table.session
})
.from(table.session)
.innerJoin(table.user, eq(table.session.userId, table.user.id))
.where(eq(table.session.id, sessionId));
if (!result) {
return { session: null, user: null };
}
const { session, user } = result;
const sessionExpired = Date.now() >= session.expiresAt.getTime();
if (sessionExpired) {
await db.delete(table.session).where(eq(table.session.id, session.id));
return { session: null, user: null };
}
const renewSession = Date.now() >= session.expiresAt.getTime() - DAY_IN_MS * 15;
if (renewSession) {
session.expiresAt = new Date(Date.now() + DAY_IN_MS * 30);
await db
.update(table.session)
.set({ expiresAt: session.expiresAt })
.where(eq(table.session.id, session.id));
}
return { session, user };
}
@molten mirage
maybe i await validatesession and make it async?
sorry but you're probably going to need to share a reproduction of the error to get help. the typescript playground is nice for that. it would also be helpful if you could share the complete error message/stack trace (it probably says something lke …is missing the following properties from 'Thing': blah, blah, blah)
Type 'Promise<{ id: string; username: string; role: Role; } | null>' is missing the following properties from type '{ id: string; username: string; role: Role; }': id, username, rolets(2739)
making it async seems to fixed the error
@molten mirage sorry but i only want to return this if there is user instead of allowing null response too, do u recommend making new function that requiresuser and throws error if null or what is best?
if this is the only place you need that check you can just do it here in this function. but if you find yourself wanting to do the same null check elsewhere then it might make sense to extract it out into a reusable function
how would i change function to do this
you can check if (user === null) for example. you might want to read about narrowing
Understand how TypeScript uses JavaScript knowledge to reduce the amount of type syntax in your projects.
export async function getUser() {
const { locals } = getRequestEvent();
if (!locals.session) error(401);
if (!locals.user) {
const result = await validateSessionToken(locals.session.id);
locals.user = result?.user ?? null;
}
if (!locals.user) error(401)
return locals.user;
}
is okay?
what does the error function look like?
just returns error
w status code
if you want narrowing to work properly there it would need to return never (which means like throwing an exception)
do you have strict turned on? if not i would highly suggest enabling it
how to turn on
strict: true can be set in your tsconfig.json under compilerOptions
What will it do
all tsconfig options are explained on https://www.typescriptlang.org/tsconfig. i linked to the docs for strict in my prior message
From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.
if you haven't already, i would suggest reading the handbook to get introduced to typescript