Skip to content

Commit

Permalink
fix(auth): update password
Browse files Browse the repository at this point in the history
  • Loading branch information
Shchepotin committed Jul 4, 2023
1 parent c230e52 commit d2793ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { AllConfigType } from 'src/config/config.type';
import { SessionService } from 'src/session/session.service';
import { JwtRefreshPayloadType } from './strategies/types/jwt-refresh-payload.type';
import { Session } from 'src/session/entities/session.entity';
import { JwtPayloadType } from './strategies/types/jwt-payload.type';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -308,20 +309,20 @@ export class AuthService {
await this.forgotService.softDelete(forgot.id);
}

async me(user: User): Promise<NullableType<User>> {
async me(userJwtPayload: JwtPayloadType): Promise<NullableType<User>> {
return this.usersService.findOne({
id: user.id,
id: userJwtPayload.id,
});
}

async update(
user: User,
userJwtPayload: JwtPayloadType,
userDto: AuthUpdateDto,
): Promise<NullableType<User>> {
if (userDto.password) {
if (userDto.oldPassword) {
const currentUser = await this.usersService.findOne({
id: user.id,
id: userJwtPayload.id,
});

if (!currentUser) {
Expand Down Expand Up @@ -356,6 +357,7 @@ export class AuthService {
user: {
id: currentUser.id,
},
excludeId: userJwtPayload.sessionId,
});
}
} else {
Expand All @@ -371,10 +373,10 @@ export class AuthService {
}
}

await this.usersService.update(user.id, userDto);
await this.usersService.update(userJwtPayload.id, userDto);

return this.usersService.findOne({
id: user.id,
id: userJwtPayload.id,
});
}

Expand Down
13 changes: 10 additions & 3 deletions src/session/session.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { FindOptions } from 'src/utils/types/find-options.type';
import { DeepPartial, Repository } from 'typeorm';
import { DeepPartial, Not, Repository } from 'typeorm';
import { Session } from './entities/session.entity';
import { NullableType } from '../utils/types/nullable.type';
import { User } from 'src/users/entities/user.entity';
Expand Down Expand Up @@ -29,10 +29,17 @@ export class SessionService {
return this.sessionRepository.save(this.sessionRepository.create(data));
}

async softDelete(criteria: {
async softDelete({
excludeId,
...criteria
}: {
id?: Session['id'];
user?: Pick<User, 'id'>;
excludeId?: Session['id'];
}): Promise<void> {
await this.sessionRepository.softDelete(criteria);
await this.sessionRepository.softDelete({
...criteria,
id: criteria.id ? criteria.id : excludeId ? Not(excludeId) : undefined,
});
}
}

0 comments on commit d2793ca

Please sign in to comment.