Skip to content

Commit

Permalink
fix: timeout env variable parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
SeDemal committed Apr 24, 2024
1 parent fed1faf commit ff68f98
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
6 changes: 1 addition & 5 deletions src/components/layout/header/AvatarMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ export const AvatarMenu = () => {
</UnstyledButton>
</Menu.Target>
<Menu.Dropdown>
<Menu.Item
closeMenuOnClick={false}
icon={<Icon size="1rem" />}
onClick={toggleColorScheme}
>
<Menu.Item closeMenuOnClick={false} icon={<Icon size="1rem" />} onClick={toggleColorScheme}>
{t('actions.avatar.switchTheme')}
</Menu.Item>
{sessionData?.user && (
Expand Down
14 changes: 10 additions & 4 deletions src/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { z } = require('zod');
const { createEnv } = require('@t3-oss/env-nextjs');
const { secondsFromTimeString } = require('./tools/client/parseDuration');

const trueStrings = ['1', 't', 'T', 'TRUE', 'true', 'True'];
const falseStrings = ['0', 'f', 'F', 'FALSE', 'false', 'False'];
Expand All @@ -16,7 +17,7 @@ const numberSchema = z
.string()
.regex(/\d*/)
.transform((value) => (value === undefined ? undefined : Number(value)))
.optional()
.optional();

const portSchema = z
.string()
Expand Down Expand Up @@ -49,7 +50,12 @@ const env = createEnv({
DEMO_MODE: z.string().optional(),
HOSTNAME: z.string().optional(),

AUTH_SESSION_EXPIRY_TIME: numberSchema,
//regex allows number with extra letter as time multiplier, applied with secondsFromTimeString
AUTH_SESSION_EXPIRY_TIME: z
.string()
.regex(/^\d+[smhd]?$/)
.transform(secondsFromTimeString)
.optional(),

// Authentication
AUTH_PROVIDER: z
Expand Down Expand Up @@ -98,7 +104,7 @@ const env = createEnv({
AUTH_OIDC_OWNER_GROUP: z.string().default('admin'),
AUTH_OIDC_AUTO_LOGIN: zodParsedBoolean(),
AUTH_OIDC_SCOPE_OVERWRITE: z.string().default('openid email profile groups'),
AUTH_OIDC_TIMEOUT: numberSchema.default(3500)
AUTH_OIDC_TIMEOUT: numberSchema.default(3500),
}
: {}),
},
Expand All @@ -120,7 +126,7 @@ const env = createEnv({
.optional()
.default('light'),
NEXT_PUBLIC_DOCKER_HOST: z.string().optional(),
NEXT_PUBLIC_LOGOUT_REDIRECT_URL: z.string().optional()
NEXT_PUBLIC_LOGOUT_REDIRECT_URL: z.string().optional(),
},
/**
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
Expand Down
5 changes: 1 addition & 4 deletions src/hooks/custom-session-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { Session } from 'next-auth';
import { SessionProvider, signIn } from 'next-auth/react';
import { useEffect } from 'react';

dayjs.extend(relativeTime);

interface CustomSessionProviderProps {
session: Session;
children: React.ReactNode;
Expand All @@ -15,7 +12,7 @@ export const CustomSessionProvider = ({ session, children }: CustomSessionProvid
//Automatically redirect to the login page after a session expires
useEffect(() => {
if (!session) return () => {};
const timeout = setTimeout(signIn, dayjs(session?.expires).diff(new Date()));
const timeout = setTimeout(signIn, dayjs(session?.expires).diff());
return () => clearTimeout(timeout);
}, [session]);

Expand Down
2 changes: 1 addition & 1 deletion src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { type GetServerSidePropsContext, type NextApiRequest, type NextApiRespon
import { type NextAuthOptions, getServerSession } from 'next-auth';
import { Adapter } from 'next-auth/adapters';
import { decode, encode } from 'next-auth/jwt';
import { env } from '~/env';
import { adapter, getProviders, onCreateUser } from '~/utils/auth';
import { createRedirectUri } from '~/utils/auth/oidc';
import EmptyNextAuthProvider from '~/utils/empty-provider';
import { fromDate, generateSessionToken } from '~/utils/session';
import { colorSchemeParser } from '~/validations/user';

import { env } from '~/env';
import { db } from './db';
import { users } from './db/schema';

Expand Down
19 changes: 19 additions & 0 deletions src/tools/client/parseDuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,22 @@ export const parseDuration = (time: number, t: TFunction): string => {

return eta;
};

export const secondsFromTimeString = (time: string | undefined): number | undefined => {
if (!time) return undefined;
const lastChar = time[time.length - 1];
if (!isNaN(+lastChar)) return Number(time);
const numTime = +time.substring(0, time.length - 1);
switch (lastChar.toLowerCase()) {
case 's':
return numTime;
case 'm':
return numTime * 60;
case 'h':
return numTime * 60 * 60;
case 'd':
return numTime * 24 * 60 * 60;
default:
return undefined;
}
};

0 comments on commit ff68f98

Please sign in to comment.