Skip to content

Commit

Permalink
fix: Handled project switching and auth-token updating (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik authored Oct 11, 2023
2 parents 2bac0d0 + b37cbd3 commit e906957
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
29 changes: 29 additions & 0 deletions apps/api/src/app/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ export class ProjectController {
return projectWithEnvironment;
}

@Put('/switch/:projectId')
@ApiOperation({
summary: 'Switch project',
})
async switchProject(
@UserSession() user: IJwtPayload,
@Param('projectId', ValidateMongoId) projectId: string,
@Res({ passthrough: true }) res: Response
) {
const projectEnvironment = await this.getEnvironment.execute(projectId);
const token = this.authService.getSignedToken(
{
_id: user._id,
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
profilePicture: user.profilePicture,
accessToken: projectEnvironment.apiKeys[0].key,
},
projectEnvironment._projectId
);
res.cookie(CONSTANTS.AUTH_COOKIE_NAME, token, {
...COOKIE_CONFIG,
domain: process.env.COOKIE_DOMAIN,
});

return;
}

@Put(':projectId')
@ApiOperation({
summary: 'Update project',
Expand Down
1 change: 1 addition & 0 deletions apps/web/config/constants.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const MODAL_TITLES = {
};

export const API_KEYS = {
PROJECT_SWITCH: 'PROJECT_SWITCH',
PROJECTS_LIST: 'PROJECT_LIST',
PROJECT_CREATE: 'PROJECT_CREATE',
PROJECT_ENVIRONMENT: 'PROJECT_ENVIRONMENT',
Expand Down
7 changes: 6 additions & 1 deletion apps/web/hooks/useApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export function useApp() {
replace(ROUTES.SIGNIN);
},
});
const { mutate: switchProject } = useMutation<unknown, IErrorObject, string, string[]>(
[API_KEYS.PROJECT_SWITCH],
(projectId) => commonApi(API_KEYS.PROJECT_SWITCH as any, { parameters: [projectId] })
);
const { mutate: createProject, isLoading: isCreateProjectLoading } = useMutation<
{ project: IProjectPayload; environment: IEnvironmentData },
IErrorObject,
Expand Down Expand Up @@ -74,13 +78,14 @@ export function useApp() {
}
},
});
const onProjectIdChange = (id: string) => {
const onProjectIdChange = async (id: string) => {
const project = projects?.find((projectItem) => projectItem._id === id);
if (project && profileInfo) {
setProfileInfo({
...profileInfo,
_projectId: id,
});
switchProject(id);
if (![ROUTES.SETTINGS, ROUTES.ACTIVITIES, ROUTES.IMPORTS].includes(pathname)) {
replace(ROUTES.IMPORTS);
}
Expand Down
4 changes: 4 additions & 0 deletions apps/web/libs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const routes: Record<string, Route> = {
url: () => '/v1/project',
method: 'POST',
},
[API_KEYS.PROJECT_SWITCH]: {
url: (projectId) => `/v1/project/switch/${projectId}`,
method: 'PUT',
},
[API_KEYS.PROJECT_ENVIRONMENT]: {
url: (projectId) => `/v1/project/${projectId}/environment`,
method: 'GET',
Expand Down

0 comments on commit e906957

Please sign in to comment.