Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: verify token optionally when getAll defaultModels #100

Merged
1 commit merged into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/hooks/verifyToken.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ import { INVALID_TOKEN, MUST_LOGIN_FIRST } from '@constants';
import { FastifyReply, FastifyRequest } from 'fastify';
import jwt from 'jsonwebtoken';

export async function verifyTokenOrPass(req: FastifyRequest) {
const token = req.cookies.token;

if (!token) return;

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const decodedPayload: any = jwt.verify(token, envs.JWT_SECRET);
req.userId = decodedPayload['userId'];
return;
} catch (err) {
req.log.info(err);
return;
}
}

export async function verifyToken(req: FastifyRequest, res: FastifyReply) {
const token = req.cookies.token;

Expand Down
4 changes: 2 additions & 2 deletions src/routes/apis/defaultModel.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UploadDefaultModelInputDto, UpdateDefaultModelInputDto, DefaultModelQueryStringDto } from '@dtos/in';
import { DefaultModelListResultDto, DefaultModelResultDto, ToggleLikeResultDto } from '@dtos/out';
import { defaultModelHandler } from '@handlers';
import { verifyToken, verifyUserRole } from '@hooks';
import { verifyToken, verifyTokenOrPass, verifyUserRole } from '@hooks';
import { UserRole } from '@prisma/client';
import { Type } from '@sinclair/typebox';
import { createRoutes } from '@utils';
Expand All @@ -10,7 +10,7 @@ export const defaultModelPlugin = createRoutes('Default Model', [
{
method: 'GET',
url: '',
onRequest: [],
onRequest: [verifyTokenOrPass],
schema: {
summary: 'Get all default models',
querystring: DefaultModelQueryStringDto,
Expand Down