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

refactor: refactor clerk auth #183

Merged
merged 2 commits into from
Sep 19, 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
63 changes: 56 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@anatine/zod-nestjs": "^1.10.0",
"@clerk/clerk-sdk-node": "^4.12.1",
"@clerk/clerk-sdk-node": "^4.12.5",
"@nestjs/bull": "^0.6.3",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.2",
Expand Down
12 changes: 7 additions & 5 deletions packages/api/src/auth/guards/clerk/clerk.auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dotenv/config';
import { UserUnauthorizedException } from '@/auth/exceptions/user-unauthorized.exception';
import { AuthGuard } from '@/auth/guards/auth-guard';
import { sessions } from '@clerk/clerk-sdk-node';
import { clerkClient } from '@clerk/clerk-sdk-node';
import { ExecutionContext, Injectable, Logger } from '@nestjs/common';
import { Request } from 'express';

Expand All @@ -12,13 +12,15 @@ export class ClerkAuthGuard extends AuthGuard {
async canActivate(context: ExecutionContext): Promise<boolean> {
const request: Request = context.switchToHttp().getRequest();
const clerkJwtToken = request.get('x-clerk-jwt-token');
const clerkSessionId = request.get('x-clerk-session-id');

return new Promise<boolean>((resolve, reject) => {
sessions
.verifySession(clerkSessionId, clerkJwtToken)
clerkClient
.authenticateRequest({
loadSession: true,
headerToken: clerkJwtToken,
})
.then((session) => {
request['auth'] = session;
request['auth'] = session.toAuth().session;
resolve(true);
})
.catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ApiHeader } from '@nestjs/swagger';

export function ApiClerkAuthHeaders(): MethodDecorator {
return applyDecorators(
ApiHeader({ name: 'x-clerk-session-id', required: true }),
ApiHeader({ name: 'x-clerk-jwt-token', required: true })
);
}
3 changes: 0 additions & 3 deletions packages/web-ui/api/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export type ClerkFetcherParamsType = {
url: string;
options?: RequestInit;
sessionId: string | null;
jwtToken: string | null;
isApplicationJson?: boolean;
};
Expand All @@ -19,15 +18,13 @@ const myFetch = createFetch(
async function apiClient({
url,
options,
sessionId,
jwtToken,
isApplicationJson = true,
}: ClerkFetcherParamsType) {
return myFetch(url, {
...options,
headers: {
...(isApplicationJson ? { 'Content-Type': 'application/json' } : {}),
'X-Clerk-Session-Id': sessionId ?? '',
'X-Clerk-Jwt-Token': jwtToken ?? '',
...options?.headers,
},
Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/ai-models/get-ai-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ export const GET_AI_MODEL_REQ_KEY = 'ai-model';

export const getAiModel = async (
aiModelId: string,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<AiModelResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.ai.getAiModel(aiModelId),
options: { method: 'GET' },
sessionId,
jwtToken,
});

Expand Down
6 changes: 1 addition & 5 deletions packages/web-ui/api/requests/ai-models/get-ai-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { AiModelResponseDto } from '@/contract/ai/ai-model.response.dto.d';

export const GET_AI_MODELS_REQ_KEY = 'ai-models';

export const getAiModels = async (
sessionId: string | null,
jwtToken: string | null
) => {
export const getAiModels = async (jwtToken: string | null) => {
return new Promise<AiModelResponseDto[]>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.ai.getAiModels(),
options: { method: 'GET' },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/rooms/create-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';

export const createRoom = async (
values: CreateRoomRequestDto,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<RoomResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.createRoom(),
options: { method: 'POST', body: JSON.stringify(values) },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/rooms/delete-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import { RemoveDocumentFromChatRequestDto } from '@/contract/chats/remove-docume
export const deleteDocument = async (
roomId: string,
values: RemoveDocumentFromChatRequestDto,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<ChatResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.chats.deleteDocument(roomId),
options: { method: 'PATCH', body: JSON.stringify(values) },
sessionId,
jwtToken,
});

Expand Down
7 changes: 1 addition & 6 deletions packages/web-ui/api/requests/rooms/delete-room.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { apiClient } from '@/api/apiClient';
import Endpoints from '@/api/endpoints';

export const deleteRoom = async (
roomId: string,
sessionId: string | null,
jwtToken: string | null
) => {
export const deleteRoom = async (roomId: string, jwtToken: string | null) => {
return new Promise<void>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.deleteRoom(roomId),
options: { method: 'DELETE' },
sessionId,
jwtToken,
});

Expand Down
7 changes: 1 addition & 6 deletions packages/web-ui/api/requests/rooms/get-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import { ChatResponseDto } from '@/contract/chats/chat.response.dto.d';

export const GET_CHAT_REQ_KEY = 'chat';

export const getChat = async (
roomId: string,
sessionId: string | null,
jwtToken: string | null
) => {
export const getChat = async (roomId: string, jwtToken: string | null) => {
return new Promise<ChatResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.chats.getChat(roomId),
options: { method: 'GET' },
sessionId,
jwtToken,
});

Expand Down
6 changes: 1 addition & 5 deletions packages/web-ui/api/requests/rooms/get-my-rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';

export const GET_MY_ROOMS_REQ_KEY = 'my-rooms';

export const getMyRooms = async (
sessionId: string | null,
jwtToken: string | null
) => {
export const getMyRooms = async (jwtToken: string | null) => {
return new Promise<RoomResponseDto[]>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.getMyRooms(),
options: { method: 'GET' },
sessionId,
jwtToken,
});

Expand Down
6 changes: 1 addition & 5 deletions packages/web-ui/api/requests/rooms/get-public-rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';

export const GET_PUBLIC_ROOMS_REQ_KEY = 'public-rooms';

export const getPublicRooms = async (
sessionId: string | null,
jwtToken: string | null
) => {
export const getPublicRooms = async (jwtToken: string | null) => {
return new Promise<RoomResponseDto[]>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.getPublicRooms(),
options: { method: 'GET' },
sessionId,
jwtToken,
});

Expand Down
7 changes: 1 addition & 6 deletions packages/web-ui/api/requests/rooms/get-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';

export const GET_ROOM_REQ_KEY = 'room';

export const getRoom = async (
roomId: string,
sessionId: string | null,
jwtToken: string | null
) => {
export const getRoom = async (roomId: string, jwtToken: string | null) => {
return new Promise<RoomResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.getRoomById(roomId),
options: { method: 'GET', cache: 'no-store' },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/rooms/invite-user-to-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';

export const inviteUserToRoom = async (
values: InviteUserToRoomRequestDto,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<RoomResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.inviteToRoom(),
options: { method: 'POST', body: JSON.stringify(values) },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/rooms/join-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';

export const joinRoom = async (
values: JoinRoomRequestDto,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<RoomResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.joinRoom(),
options: { method: 'POST', body: JSON.stringify(values) },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/rooms/leave-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';

export const leaveRoom = async (
values: LeaveRoomRequestDto,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<RoomResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.leaveRoom(),
options: { method: 'POST', body: JSON.stringify(values) },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/rooms/update-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import { UpdateRoomSettingsRequestDto } from '@/contract/rooms/update-room-setti
export const updateRoom = async (
roomId: string,
values: UpdateRoomSettingsRequestDto,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<RoomResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.rooms.updateRoom(roomId),
options: { method: 'PATCH', body: JSON.stringify(values) },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/rooms/upload-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Endpoints from '@/api/endpoints';
export const uploadDocuments = async (
roomId: string,
formData: FormData,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<void>(async (resolve, reject) => {
Expand All @@ -14,7 +13,6 @@ export const uploadDocuments = async (
method: 'POST',
body: formData,
},
sessionId,
jwtToken,
isApplicationJson: false,
});
Expand Down
Loading