|
|
|
@ -4,6 +4,7 @@ import { SESSIONS_SERVICE } from 'src/core/consts'
@@ -4,6 +4,7 @@ import { SESSIONS_SERVICE } from 'src/core/consts'
|
|
|
|
|
import { InvalidCredentialsException } from 'src/shared' |
|
|
|
|
import { USERS_REPOSITORY } from '../consts' |
|
|
|
|
import { IUsersRepository } from '../interfaces' |
|
|
|
|
import { UsersAuthAttemptionsService } from './users-auth-attemptions.service' |
|
|
|
|
import { UsersConfirmationCodesService } from './users-confirmation-codes.service' |
|
|
|
|
import { UsersPasswordsService } from './users-passwords.service' |
|
|
|
|
|
|
|
|
@ -14,6 +15,7 @@ export class UsersAuthService implements Users.IUsersAuthService {
@@ -14,6 +15,7 @@ export class UsersAuthService implements Users.IUsersAuthService {
|
|
|
|
|
@Inject(SESSIONS_SERVICE) private readonly sessionsService: Sessions.ISessionsService, |
|
|
|
|
private readonly confirmationCodesService: UsersConfirmationCodesService, |
|
|
|
|
private readonly passwordsService: UsersPasswordsService, |
|
|
|
|
private readonly authAttemptionsService: UsersAuthAttemptionsService, |
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
public async sendConfirmationCode(phoneNumber: string) { |
|
|
|
@ -21,7 +23,11 @@ export class UsersAuthService implements Users.IUsersAuthService {
@@ -21,7 +23,11 @@ export class UsersAuthService implements Users.IUsersAuthService {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async confirmLoginCode(payload: Users.IConfirmCodePayload) { |
|
|
|
|
await this.confirmationCodesService.confirmCode(payload.phoneNumber, payload.code) |
|
|
|
|
await this.confirmationCodesService.confirmCode( |
|
|
|
|
payload.phoneNumber, |
|
|
|
|
payload.code, |
|
|
|
|
payload.ip, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const user = await this.usersRepository.findOne({ phoneNumber: payload.phoneNumber }) |
|
|
|
|
|
|
|
|
@ -33,8 +39,12 @@ export class UsersAuthService implements Users.IUsersAuthService {
@@ -33,8 +39,12 @@ export class UsersAuthService implements Users.IUsersAuthService {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async checkCode(phoneNumber: string, code: string) { |
|
|
|
|
return await this.confirmationCodesService.compareCode(phoneNumber, code) |
|
|
|
|
public async checkCode(phoneNumber: string, code: string, ip?: string) { |
|
|
|
|
const isCorrect = await this.confirmationCodesService.compareCode(phoneNumber, code) |
|
|
|
|
|
|
|
|
|
if (!isCorrect) await this.authAttemptionsService.add(ip) |
|
|
|
|
|
|
|
|
|
return isCorrect |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async loginWithPassword(payload: Users.ILoginWithPasswordPayload) { |
|
|
|
@ -48,7 +58,10 @@ export class UsersAuthService implements Users.IUsersAuthService {
@@ -48,7 +58,10 @@ export class UsersAuthService implements Users.IUsersAuthService {
|
|
|
|
|
payload.password, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (!isCorrect) throw new InvalidCredentialsException() |
|
|
|
|
if (!isCorrect) { |
|
|
|
|
await this.authAttemptionsService.add(payload.ip) |
|
|
|
|
throw new InvalidCredentialsException() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return await this.sessionsService.start({ |
|
|
|
|
userId: user.id, |
|
|
|
|