Skip to content

Commit

Permalink
🐛 fix: fix dayjs error in en-US language (lobehub#3604)
Browse files Browse the repository at this point in the history
* 🐛 fix: fix dayjs issue

* 🐛 fix: fix user auth and password issue
  • Loading branch information
arvinxx committed Aug 25, 2024
1 parent 7076b95 commit 174f4df
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
12 changes: 11 additions & 1 deletion src/layout/GlobalProvider/Locale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ const Locale = memo<LocaleLayoutProps>(({ children, defaultLang, antdLocale }) =
if (!lang) return;

// load default lang
const dayJSLocale = await import(`dayjs/locale/${lang!.toLowerCase()}.js`);
let dayJSLocale;
try {
// dayjs locale is using `en` instead of `en-US`
// refs: https://github.com/lobehub/lobe-chat/issues/3396
const locale = lang!.toLowerCase() === 'en-us' ? 'en' : lang!.toLowerCase();

dayJSLocale = await import(`dayjs/locale/${locale}.js`);
} catch {
console.warn(`dayjs locale for ${lang} not found, fallback to en`);
dayJSLocale = await import(`dayjs/locale/en.js`);
}

dayjs.locale(dayJSLocale.default);
});
Expand Down
4 changes: 2 additions & 2 deletions src/libs/trpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @link https://trpc.io/docs/v11/procedures
*/
import { trpc } from './init';
import { passwordChecker } from './middleware/password';
import { jwtPayloadChecker } from './middleware/jwtPayload';
import { userAuth } from './middleware/userAuth';

/**
Expand All @@ -27,7 +27,7 @@ export const publicProcedure = trpc.procedure;
export const authedProcedure = trpc.procedure.use(userAuth);

// procedure that asserts that the user add the password
export const passwordProcedure = trpc.procedure.use(passwordChecker);
export const passwordProcedure = trpc.procedure.use(jwtPayloadChecker);

/**
* Merge multiple routers together
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { TRPCError } from '@trpc/server';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import * as utils from '@/app/api/middleware/auth/utils';
import * as appConfig from '@/config/app';
import { createCallerFactory } from '@/libs/trpc';
import { trpc } from '@/libs/trpc/init';
import { AuthContext, createContextInner } from '@/server/context';

import { passwordChecker } from './password';
import { jwtPayloadChecker } from './jwtPayload';

const appRouter = trpc.router({
protectedQuery: trpc.procedure.use(passwordChecker).query(async ({ ctx }) => {
protectedQuery: trpc.procedure.use(jwtPayloadChecker).query(async ({ ctx }) => {
return ctx.jwtPayload;
}),
});
Expand All @@ -38,23 +37,9 @@ describe('passwordChecker middleware', () => {
await expect(router.protectedQuery()).rejects.toThrow(new TRPCError({ code: 'UNAUTHORIZED' }));
});

it('should throw UNAUTHORIZED error if access code is not correct', async () => {
vi.spyOn(appConfig, 'getAppConfig').mockReturnValue({
ACCESS_CODES: ['123'],
} as any);
vi.spyOn(utils, 'getJWTPayload').mockResolvedValue({ accessCode: '456' });

ctx = await createContextInner({ authorizationHeader: 'Bearer token' });
router = createCaller(ctx);

await expect(router.protectedQuery()).rejects.toThrow(new TRPCError({ code: 'UNAUTHORIZED' }));
});

it('should call next with jwtPayload in context if access code is correct', async () => {
const jwtPayload = { accessCode: '123' };
vi.spyOn(appConfig, 'getAppConfig').mockReturnValue({
ACCESS_CODES: ['123'],
} as any);

vi.spyOn(utils, 'getJWTPayload').mockResolvedValue(jwtPayload);

ctx = await createContextInner({ authorizationHeader: 'Bearer token' });
Expand All @@ -67,9 +52,6 @@ describe('passwordChecker middleware', () => {

it('should call next with jwtPayload in context if no access codes are set', async () => {
const jwtPayload = {};
vi.spyOn(appConfig, 'getAppConfig').mockReturnValue({
ACCESS_CODES: [],
} as any);
vi.spyOn(utils, 'getJWTPayload').mockResolvedValue(jwtPayload);

ctx = await createContextInner({ authorizationHeader: 'Bearer token' });
Expand All @@ -81,7 +63,6 @@ describe('passwordChecker middleware', () => {
});
it('should call next with jwtPayload in context if access codes is undefined', async () => {
const jwtPayload = {};
vi.spyOn(appConfig, 'getAppConfig').mockReturnValue({} as any);
vi.spyOn(utils, 'getJWTPayload').mockResolvedValue(jwtPayload);

ctx = await createContextInner({ authorizationHeader: 'Bearer token' });
Expand Down
14 changes: 14 additions & 0 deletions src/libs/trpc/middleware/jwtPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TRPCError } from '@trpc/server';

import { getJWTPayload } from '@/app/api/middleware/auth/utils';
import { trpc } from '@/libs/trpc/init';

export const jwtPayloadChecker = trpc.middleware(async (opts) => {
const { ctx } = opts;

if (!ctx.authorizationHeader) throw new TRPCError({ code: 'UNAUTHORIZED' });

const jwtPayload = await getJWTPayload(ctx.authorizationHeader);

return opts.next({ ctx: { jwtPayload } });
});
26 changes: 0 additions & 26 deletions src/libs/trpc/middleware/password.ts

This file was deleted.

0 comments on commit 174f4df

Please sign in to comment.