fix: error when session expired
This commit is contained in:
@@ -146,15 +146,9 @@ const startServer = async () => {
|
|||||||
try {
|
try {
|
||||||
const sessionId = decodeSessionToken(req.cookies.session);
|
const sessionId = decodeSessionToken(req.cookies.session);
|
||||||
const session = await runWithAlsSession(sessionId, () =>
|
const session = await runWithAlsSession(sessionId, () =>
|
||||||
sessionId
|
|
||||||
? getCache(`validateSession:${sessionId}`, 60 * 5, async () =>
|
|
||||||
validateSessionToken(req.cookies.session),
|
validateSessionToken(req.cookies.session),
|
||||||
)
|
|
||||||
: validateSessionToken(req.cookies.session),
|
|
||||||
);
|
);
|
||||||
if (session.session) {
|
|
||||||
req.session = session;
|
req.session = session;
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
req.session = EMPTY_SESSION;
|
req.session = EMPTY_SESSION;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ export const authRouter = createTRPCRouter({
|
|||||||
deleteSessionTokenCookie(ctx.setCookie);
|
deleteSessionTokenCookie(ctx.setCookie);
|
||||||
if (ctx.session?.session?.id) {
|
if (ctx.session?.session?.id) {
|
||||||
await invalidateSession(ctx.session.session.id);
|
await invalidateSession(ctx.session.session.id);
|
||||||
await deleteCache(`validateSession:${ctx.session.session.id}`);
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
signInOAuth: publicProcedure
|
signInOAuth: publicProcedure
|
||||||
@@ -226,9 +225,7 @@ export const authRouter = createTRPCRouter({
|
|||||||
|
|
||||||
const token = generateSessionToken();
|
const token = generateSessionToken();
|
||||||
const session = await createSession(token, user.id);
|
const session = await createSession(token, user.id);
|
||||||
console.log('session', session);
|
|
||||||
setSessionTokenCookie(ctx.setCookie, token, session.expiresAt);
|
setSessionTokenCookie(ctx.setCookie, token, session.expiresAt);
|
||||||
console.log('ctx.setCookie', ctx.setCookie);
|
|
||||||
return {
|
return {
|
||||||
type: 'email',
|
type: 'email',
|
||||||
};
|
};
|
||||||
@@ -335,7 +332,6 @@ export const authRouter = createTRPCRouter({
|
|||||||
const session = await validateSessionToken(token);
|
const session = await validateSessionToken(token);
|
||||||
|
|
||||||
if (session.session) {
|
if (session.session) {
|
||||||
await deleteCache(`validateSession:${session.session.id}`);
|
|
||||||
// Re-set the cookie with updated expiration
|
// Re-set the cookie with updated expiration
|
||||||
setSessionTokenCookie(ctx.setCookie, token, session.session.expiresAt);
|
setSessionTokenCookie(ctx.setCookie, token, session.session.expiresAt);
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -95,7 +95,8 @@ const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {
|
|||||||
|
|
||||||
// Only used on protected routes
|
// Only used on protected routes
|
||||||
const enforceAccess = t.middleware(async ({ ctx, next, type, getRawInput }) => {
|
const enforceAccess = t.middleware(async ({ ctx, next, type, getRawInput }) => {
|
||||||
return runWithAlsSession(ctx.session.session?.id, async () => {
|
const sessionId = ctx.session?.session?.id ?? null;
|
||||||
|
return runWithAlsSession(sessionId, async () => {
|
||||||
const rawInput = await getRawInput();
|
const rawInput = await getRawInput();
|
||||||
if (type === 'mutation' && process.env.DEMO_USER_ID) {
|
if (type === 'mutation' && process.env.DEMO_USER_ID) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
@@ -153,7 +154,7 @@ const loggerMiddleware = t.middleware(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const sessionScopeMiddleware = t.middleware(async ({ ctx, next }) => {
|
const sessionScopeMiddleware = t.middleware(async ({ ctx, next }) => {
|
||||||
const sessionId = ctx.session.session?.id ?? null;
|
const sessionId = ctx.session?.session?.id ?? null;
|
||||||
return runWithAlsSession(sessionId, async () => {
|
return runWithAlsSession(sessionId, async () => {
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user