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 1 commit
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
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
6 changes: 1 addition & 5 deletions packages/web-ui/api/requests/users/get-all-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { UserResponseDto } from '@/contract/auth/user.response.dto.d';

export const GET_ALL_USERS_REQ_KEY = 'all-users';

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

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/users/get-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ export const GET_USER_REQ_KEY = 'user';

export const getUser = async (
userId: string | undefined,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<UserResponseDto>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.users.getUser(userId ?? ''),
options: { method: 'GET' },
sessionId,
jwtToken,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/web-ui/api/requests/users/get-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ export const GET_USERS_REQ_KEY = 'users';

export const getUsers = async (
userIds: string[] | undefined,
sessionId: string | null,
jwtToken: string | null
) => {
return new Promise<UserResponseDto[]>(async (resolve, reject) => {
const res = await apiClient({
url: Endpoints.users.getUsers(userIds ?? []),
options: { method: 'GET' },
sessionId,
jwtToken,
});

Expand Down
8 changes: 4 additions & 4 deletions packages/web-ui/app/rooms/[room]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ export default async function RoomLayout({
children,
params,
}: RootLayoutProps) {
const { sessionId, getToken } = auth();
const { getToken } = auth();
const token = await getToken();

const queryClient = getQueryClient();
const room = await queryClient.fetchQuery([GET_ROOM_REQ_KEY], () =>
getRoom(params.room, sessionId, token)
getRoom(params.room, token)
);
const chat = await queryClient.fetchQuery([GET_CHAT_REQ_KEY], () =>
getChat(params.room, sessionId, token)
getChat(params.room, token)
);
const aiModel = await queryClient.fetchQuery([GET_AI_MODEL_REQ_KEY], () =>
getAiModel(chat.aiModelId ?? '', sessionId, token)
getAiModel(chat.aiModelId ?? '', token)
);
const dehydratedState = dehydrate(queryClient);

Expand Down
8 changes: 4 additions & 4 deletions packages/web-ui/app/rooms/[room]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import { ChatTools } from '@/components/chat-tools';
import getQueryClient from '@/app/get-query-client';

export default async function Room({ params }: { params: { room: string } }) {
const { sessionId, getToken, userId } = auth();
const { getToken, userId } = auth();
const token = await getToken();
const queryClient = getQueryClient();
const room = await queryClient.fetchQuery([GET_ROOM_REQ_KEY], () =>
getRoom(params.room, sessionId, token)
getRoom(params.room, token)
);
const chat = await queryClient.fetchQuery([GET_CHAT_REQ_KEY], () =>
getChat(params.room, sessionId, token)
getChat(params.room, token)
);
const owner = await queryClient.fetchQuery([GET_USER_REQ_KEY], () =>
getUser(room.ownerId, sessionId, token)
getUser(room.ownerId, token)
);
const dehydratedState = dehydrate(queryClient);
const isOwner = owner.id === userId;
Expand Down
Loading