-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #401 from AndreSoftwareDeveloper/master
feat: Changing password
- Loading branch information
Showing
6 changed files
with
104 additions
and
5 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
18 changes: 18 additions & 0 deletions
18
packages/backend/src/core/members/reset_password/change_password/change_password.resolver.ts
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,18 @@ | ||
import { Args, Mutation, Resolver } from '@nestjs/graphql'; | ||
|
||
import { ChangePasswordCoreMembersArgs } from './dto/change_password.args'; | ||
import { ChangePasswordCoreMembersService } from './change_password.service'; | ||
|
||
import { User } from '@/decorators/user.decorator'; | ||
|
||
@Resolver() | ||
export class ChangePasswordCoreMembersResolver { | ||
constructor(private readonly service: ChangePasswordCoreMembersService) {} | ||
|
||
@Mutation(() => User) | ||
async core_members__change_password( | ||
@Args() args: ChangePasswordCoreMembersArgs, | ||
): Promise<User> { | ||
return this.service.change_password(args); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/backend/src/core/members/reset_password/change_password/change_password.service.ts
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,48 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { eq } from 'drizzle-orm'; | ||
|
||
import { ChangePasswordCoreMembersArgs } from './dto/change_password.args'; | ||
|
||
import { DatabaseService } from '@/database'; | ||
import { User } from '@/decorators'; | ||
import { encryptPassword } from '@/core/sessions/encrypt_password'; | ||
import { | ||
core_users, | ||
core_users_pass_reset, | ||
} from '@/templates/core/admin/database/schema/users'; | ||
|
||
@Injectable() | ||
export class ChangePasswordCoreMembersService { | ||
constructor( | ||
private readonly databaseService: DatabaseService, | ||
private readonly configService: ConfigService, | ||
) {} | ||
|
||
async change_password({ | ||
hashKey, | ||
password, | ||
}: ChangePasswordCoreMembersArgs): Promise<User> { | ||
const keyData = | ||
await this.databaseService.db.query.core_users_pass_reset.findFirst({ | ||
where: eq(core_users_pass_reset.key, hashKey), | ||
}); | ||
|
||
const id = keyData.user_id; | ||
const hashPassword = await encryptPassword(this.configService, password); | ||
|
||
const update = await this.databaseService.db | ||
.update(core_users) | ||
.set({ | ||
password: hashPassword, | ||
}) | ||
.where(eq(core_users.id, id)) | ||
.returning(); | ||
|
||
return { | ||
...update[0], | ||
avatar: null, | ||
group: null, | ||
}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/backend/src/core/members/reset_password/change_password/dto/change_password.args.ts
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,18 @@ | ||
import { IsStrongPassword } from 'class-validator'; | ||
import { ArgsType, Field } from '@nestjs/graphql'; | ||
|
||
@ArgsType() | ||
export class ChangePasswordCoreMembersArgs { | ||
@Field(() => String) | ||
hashKey: string; | ||
|
||
@IsStrongPassword({ | ||
minLength: 8, | ||
minLowercase: 1, | ||
minUppercase: 1, | ||
minNumbers: 1, | ||
minSymbols: 1, | ||
}) | ||
@Field(() => String) | ||
password: string; | ||
} |
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,14 @@ | ||
import { ConfigService } from '@nestjs/config'; | ||
import { genSalt, hash } from 'bcrypt'; | ||
|
||
async function encryptPassword( | ||
configService: ConfigService, | ||
password: string, | ||
): Promise<string> { | ||
const passwordSalt = await genSalt(configService.getOrThrow('password_salt')); | ||
const hashPassword = await hash(password, passwordSalt); | ||
|
||
return hashPassword; | ||
} | ||
|
||
export { encryptPassword }; |
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