#Type 'Promise<{ id: string; username: string; role: Role; } | null>' is missing the following proper

25 messages · Page 1 of 1 (latest)

ornate yarrow
#

Why is this happen?

export function getUser() {
    const { locals } = getRequestEvent();
    if (!locals.session) return null;
    return locals.user ??= validateSessionToken(locals.session.id).then(r => r?.user);
}
molten mirage
#

can you share a more complete example? what are getRequestEvent and validateSessionToken?

ornate yarrow
# molten mirage can you share a more complete example? what are `getRequestEvent` and `validateS...

`(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?

molten mirage
#

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)

ornate yarrow
#

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?

molten mirage
ornate yarrow
molten mirage
#

you can check if (user === null) for example. you might want to read about narrowing

ornate yarrow
#

is okay?

molten mirage
#

what does the error function look like?

ornate yarrow
#

w status code

molten mirage
#

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

south wagon
molten mirage
#

if you haven't already, i would suggest reading the handbook to get introduced to typescript