Skip to content

Commit

Permalink
Fix: User Status
Browse files Browse the repository at this point in the history
  • Loading branch information
ialiaslani committed Dec 28, 2022
1 parent 08712ab commit 8c8a4a3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
34 changes: 22 additions & 12 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,41 @@ export class AuthController {
@HasPermission("public")
@Post("login")
async login(@Body() { email, password }: LoginDto) {
const user: User = await this.userService.findOne({ email })
let user: User = await this.userService.findOne({ email })

const blocked = await this.authService.getCache("user_" + user.id)

if (blocked.name) {
throw new BadRequestException(`Sorry ${blocked.name} Please Try Again After ${blocked.ttl} Seconds`)
if (!user) {
throw new NotFoundException("User Not Fount")
}

const blocked = await this.authService.getCache("user_" + user.id)

if (!user) {
throw new NotFoundException("User Not Fount")
if (blocked.value) {
throw new BadRequestException(`Sorry ${blocked.value} Please Try Again After ${blocked.ttl} Seconds`)
}


if (! await bcrypt.compare(password, user.password)) {
await this.authService.setCache("user_" + user.id, user.name)
const numberOfTries = +(await this.authService.getCache("number_of_tries_" + user.id)).value || 0

if (numberOfTries >= 3) {
await this.authService.deleteCache("number_of_tries_" + user.id)
await this.authService.setCache("user_" + user.id, user.name)
await this.userService.update(user.id, { status: "BLOCKED" })
}

await this.authService.setCache("number_of_tries_" + user.id, `${numberOfTries + 1}`)

throw new BadRequestException("Invalid Password!")
}


const token = await this.jwtService.signAsync({ id: user.id }, { secret: "secretKey" })
const token = await this.jwtService.signAsync({ id: user.id }, { secret: process.env.TOKEN_SECRET })


console.log('====================================');
console.log(await this.authService.getAllData());
console.log('====================================');
if (user.status === "BLOCKED") {
await this.userService.update(user.id, { status: "ACTIVE" })
user = await this.userService.findOne({ email })
}

return { user, token }

Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AuthGuard implements CanActivate {
const request: Request = context.switchToHttp().getRequest()

const token = request.headers.authorization.replace("Bearer ", "")
return this.jwtService.verify(token, { secret: "secretKey" });
return this.jwtService.verify(token, { secret: process.env.TOKEN_SECRET });
} catch (error) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CacheModule } from '../cache/cache.module';
CacheModule,
UserModule,
JwtModule.register({
secret: "secretKey",
secret: process.env.TOKEN_SECRET,
signOptions: { expiresIn: '1d' },
}),
],
Expand Down
10 changes: 5 additions & 5 deletions src/cache/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export class CacheService {
@Inject(CACHE_MANAGER) private readonly cache: Cache,
) { }

async setCache(key: string, name: string): Promise<void> {
await this.cache.set(key, name);
async setCache(key: string, value: string): Promise<void> {
await this.cache.set(key, value);
}

async deleteCache(key: string): Promise<void> {
Expand All @@ -20,12 +20,12 @@ export class CacheService {
}

async getCache(key: string): Promise<{
key: string, name: string, ttl: number
key: string, value: string, ttl: number
}> {
const name = await this.cache.get(key) as string;
const value = await this.cache.get(key) as string;
const ttl = await this.cache.store.ttl(key)
return {
key, name, ttl
key, value, ttl
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/user/models/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class User extends CommonEntity {
@Column()
email: string;

@Column({ default: "ACTIVE" })
status: "ACTIVE" | "DEACTIVE" | "BLOCKED";

@Column({ nullable: true })
image: string;

Expand Down
7 changes: 5 additions & 2 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export class UserService extends CommonService {
}

async update(params, data): Promise<any> {
const hash = await bcrypt.hash(data.password, 12)
return await this.repository.update(params, { ...data, password: hash })
let hash: undefined | string = undefined
if (data.password) {
hash = await bcrypt.hash(data.password, 12)
}
return await this.repository.update(params, { ...data, ...(hash && { password: hash }) })
}

async saveAvatar(id, image = ""): Promise<any> {
Expand Down

0 comments on commit 8c8a4a3

Please sign in to comment.