-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b32ef0
commit 527f7ff
Showing
9 changed files
with
83 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { ChangeCredentialsModel } from "../../../entities/auth"; | ||
export interface ResetPasswordUseCase { | ||
execute(credentials: ChangeCredentialsModel): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { UserRepository } from "../../interfaces/repositories/user-repository"; | ||
import { ResetPasswordUseCase } from "../../interfaces/use-cases/auth/reset-password"; | ||
import { ChangeCredentialsModel, DecodedToken } from "../../entities/auth"; | ||
import { UserResponseModel } from "../../entities/user"; | ||
|
||
export class ResetPassword implements ResetPasswordUseCase { | ||
userRepository: UserRepository | ||
|
||
constructor(userRepository: UserRepository) { | ||
this.userRepository = userRepository | ||
} | ||
async execute(credentials: ChangeCredentialsModel): Promise<void> { | ||
let nb_of_updated_user: number = 0 | ||
let decoded_token: DecodedToken | null = null | ||
|
||
//is the user reset_password_token valid ? | ||
if (credentials.reset_password_token) { | ||
decoded_token = this.userRepository.verifyResetPasswordToken(credentials.reset_password_token) | ||
if (!decoded_token) throw new Error("Token is not valid"); | ||
} else throw new Error("Token is not valid"); | ||
|
||
// does the user bind to reset_password_code exist ? | ||
const preexistant_user: UserResponseModel | null = await this.userRepository.getUser( | ||
{ | ||
user_id: decoded_token.user_id, | ||
reset_password_code: decoded_token.reset_password_code | ||
} | ||
) | ||
// if the user does not exist or the reset_password_code is not valid | ||
if (!preexistant_user) throw new Error("User does not exist or token is not valid"); | ||
|
||
// is the user validated ? | ||
if (!preexistant_user.valid_email) throw new Error("User email is not validated"); | ||
|
||
// change the password | ||
nb_of_updated_user = await this.userRepository.changePassword({ ...preexistant_user, ...credentials }) | ||
if (nb_of_updated_user == 0) throw new Error("Can't change password"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters