Skip to content

Commit

Permalink
remove origin header constraint, return updated user
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 14, 2024
1 parent be28804 commit 7888930
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 17 deletions.
4 changes: 2 additions & 2 deletions api/src/modules/auth/authentication.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export class AuthenticationController {
return tsRestHandler(
authContract.confirmEmail,
async ({ body: { newEmail } }) => {
await this.authService.confirmEmail(user, newEmail);
const updatedUser = await this.authService.confirmEmail(user, newEmail);
return {
body: null,
body: { data: updatedUser },
status: HttpStatus.OK,
};
},
Expand Down
4 changes: 2 additions & 2 deletions api/src/modules/auth/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ export class AuthenticationService {
);
}

async confirmEmail(user: User, newEmail: string): Promise<void> {
async confirmEmail(user: User, newEmail: string): Promise<User> {
const existingUser = await this.usersService.findByEmail(newEmail);
if (existingUser) {
throw new ConflictException(`Email already in use`);
}
user.email = newEmail;
await this.usersService.saveUser(user);
return this.usersService.saveUser(user);
}
}
2 changes: 1 addition & 1 deletion api/test/e2e/features/validate-token.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Feature: Validate Token

Scenario: Validating a token without providing the Authorization header
When the user attempts to validate a token without providing the Authorization header
Then the user should receive a 400 status code
Then the user should receive a 401 status code



10 changes: 3 additions & 7 deletions api/test/integration/users/users-email-update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ROLES } from '@shared/entities/users/roles.enum';
import { MockEmailService } from '../../utils/mocks/mock-email.service';
import { IEmailServiceToken } from '@api/modules/notifications/email/email-service.interface';
import { JwtManager } from '@api/modules/auth/services/jwt.manager';
import { User } from '@shared/entities/users/user.entity';
import { authContract } from '@shared/contracts/auth.contract';

describe('Users ME (e2e)', () => {
Expand Down Expand Up @@ -71,7 +70,7 @@ describe('Users ME (e2e)', () => {
});
});
describe('Confirm email update', () => {
it('should update the email', async () => {
it('should update the email and return the updated user', async () => {
const user = await testManager
.mocks()
.createUser({ email: '[email protected]', role: ROLES.PARTNER });
Expand All @@ -85,11 +84,8 @@ describe('Users ME (e2e)', () => {
.set('Authorization', `Bearer ${emailUpdateToken}`);

expect(response.status).toBe(200);
const userWithUpdatedEmail = await testManager
.getDataSource()
.getRepository(User)
.findOneBy({ email: newEmail });
expect(userWithUpdatedEmail.id).toEqual(user.id);
expect(response.body.data.email).toBe(newEmail);
expect(response.body.data.id).toBe(user.id);
});
it('should fail if the new email is already in use', async () => {
const user = await createUser(testManager.getDataSource(), {
Expand Down
7 changes: 3 additions & 4 deletions shared/contracts/auth.contract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { initContract } from "@ts-rest/core";
import { LogInSchema } from "@shared/schemas/auth/login.schema";
import { UserWithAccessToken } from "@shared/dtos/users/user.dto";
import { UserDto, UserWithAccessToken } from "@shared/dtos/users/user.dto";
import { TokenTypeSchema } from "@shared/schemas/auth/token-type.schema";
import { z } from "zod";
import { BearerTokenSchema } from "@shared/schemas/auth/bearer-token.schema";
import { SignUpSchema } from "@shared/schemas/auth/sign-up.schema";
import { EmailConfirmation } from "@api/modules/auth/strategies/email-update.strategy";
import { RequestEmailUpdateSchema } from "@shared/schemas/users/request-email-update.schema";
import { ApiResponse } from "@shared/dtos/global/api-response.dto";

// TODO: This is a scaffold. We need to define types for responses, zod schemas for body and query param validation etc.

Expand Down Expand Up @@ -48,7 +48,6 @@ export const authContract = contract.router({
requestPasswordRecovery: {
method: "POST",
path: "/authentication/recover-password",
headers: z.object({ origin: z.string().url() }),
responses: {
201: null,
},
Expand All @@ -59,7 +58,7 @@ export const authContract = contract.router({
method: "PATCH",
path: "/authentication/confirm-email",
responses: {
200: null,
200: contract.type<ApiResponse<UserDto>>(),
},
body: RequestEmailUpdateSchema,
},
Expand Down
1 change: 0 additions & 1 deletion shared/contracts/users.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export const usersContract = contract.router({
requestEmailUpdate: {
method: "PATCH",
path: "/users/me/email",
headers: z.object({ origin: z.string().url() }),
responses: {
200: contract.type<null>(),
},
Expand Down

0 comments on commit 7888930

Please sign in to comment.