Skip to content

Commit 81a51cd

Browse files
committed
fixing authentication login /logout
1 parent d96eca4 commit 81a51cd

File tree

6 files changed

+73
-57
lines changed

6 files changed

+73
-57
lines changed

src/apps/auth/auth.controller.spec.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/apps/auth/auth.controller.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AuthController {
2121
@Req() req: Request,
2222
@Body() data: LoginDTO
2323
) {
24-
const loginResponse = await this.authService.login(data);
24+
const loginResponse = await this.authService.login(data, req);
2525

2626
return Response.success({
2727
message: i18n.t('response.login.success'),
@@ -32,13 +32,14 @@ export class AuthController {
3232
@Post('/register')
3333
@HttpCode(HttpStatus.CREATED)
3434
async register(
35+
@I18n() i18n: I18nContext,
3536
@Req() req: Request,
3637
@Body() data: RegisterDTO,
3738
) {
3839
const registerResponse = await this.authService.register(data, req);
3940

4041
return Response.success({
41-
message: "Your account has been created.",
42+
message: i18n.t('response.register.success'),
4243
data: registerResponse,
4344
});
4445
}
@@ -47,14 +48,17 @@ export class AuthController {
4748
@HttpCode(HttpStatus.OK)
4849
@UseJwtGuard()
4950
async logout(
51+
@I18n() i18n: I18nContext,
5052
@Req() req: Request,
51-
@Body() data: RegisterDTO,
5253
) {
53-
// const registerResponse = await this.authService.logout();
54-
console.log(req.user)
55-
// return Response.success({
56-
// message: "Your account has been created.",
57-
// data: registerResponse,
58-
// });
54+
try {
55+
await this.authService.logout(req.user);
56+
57+
return Response.success({
58+
message: i18n.t('response.logout.success'),
59+
});
60+
} catch (error) {
61+
62+
}
5963
}
6064
}

src/apps/auth/auth.service.spec.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/apps/auth/auth.service.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,26 @@ export class AuthService {
2424
) {}
2525

2626
async logout(userAuth: UserInAuth) {
27-
//
27+
28+
try {
29+
this.userDevicesRepository.update({
30+
id: userAuth.id
31+
}, {
32+
access_token: null,
33+
refresh_token: null,
34+
})
35+
36+
this.redisService.del(`user-data:${userAuth.id}`);
37+
38+
return true;
39+
} catch (error) {
40+
return false;
41+
} finally {
42+
43+
}
2844
}
2945

30-
async login(data: LoginDTO) {
46+
async login(data: LoginDTO, req: Request) {
3147
const user = await this.getUser([
3248
{username: data.identity,},
3349
{email: data.identity,},
@@ -40,6 +56,32 @@ export class AuthService {
4056

4157
const tokens = await this.getTokens(user, data.device);
4258

59+
const userDevice = this.userDevicesRepository.create({
60+
user_id: user.id, // Associate with the newly created user
61+
unique_id: data.device.id,
62+
name: data.device.name,
63+
brand: data.device.brand,
64+
os: data.device.os,
65+
platform: req.header('x-app-platform') ?? 'unknown',
66+
access_token: await this.makeHash(tokens.access_token),
67+
refresh_token: await this.makeHash(tokens.refresh_token),
68+
status: 'allowed',
69+
last_login: {
70+
ip: req.ip,
71+
app_version: req.header('x-app-version'),
72+
timestamp: new Date().getTime(),
73+
coordinate: data.coordinate,
74+
},
75+
});
76+
try {
77+
await this.userDevicesRepository.save(userDevice);
78+
} catch (error) {
79+
await this.userDevicesRepository.update({
80+
user_id: userDevice.user_id,
81+
unique_id: userDevice.unique_id,
82+
}, userDevice);
83+
}
84+
4385
delete user.password;
4486

4587
return {

src/entities/user-device.entity.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import { CurrentLoginType } from "src/apps/auth/types";
22
import { strRandom } from "src/utils";
3-
import { Column, Entity, PrimaryColumn } from "typeorm";
3+
import { Column, CreateDateColumn, Entity, PrimaryColumn, Unique, UpdateDateColumn } from "typeorm";
44

55
@Entity({
66
name: 'user_devices'
77
})
8+
@Unique(['user_id', 'unique_id']) // Add a unique constraint on user_id and unique_id columns
89
export class UserDevice {
910
@PrimaryColumn()
10-
id: string;
11+
id: number;
1112

1213
@Column()
1314
user_id: number;
14-
// userId: string;
1515

1616
@Column({name: 'unique_id'})
1717
unique_id:string;
18-
// uniqueId: string;
1918

2019
@Column()
2120
name: string;
@@ -31,23 +30,18 @@ export class UserDevice {
3130

3231
@Column('json', {name: 'last_login'})
3332
last_login:CurrentLoginType;
34-
// lastLogin: CurrentLoginType;
3533

3634
@Column('json', {name: 'current_login'})
3735
current_login:CurrentLoginType;
38-
// currentLogin: CurrentLoginType;
3936

4037
@Column({name: 'access_token'})
4138
access_token:string;
42-
// accessToken: string;
4339

4440
@Column({name: 'refresh_token'})
4541
refresh_token:string;
46-
// refreshToken: string;
4742

4843
@Column({name: 'fcm_token'})
4944
fcm_token:string;
50-
// fcmToken: string;
5145

5246
@Column()
5347
preference: string;
@@ -57,6 +51,11 @@ export class UserDevice {
5751

5852
@Column({name: 'status_reason'})
5953
status_reason:string;
60-
// statusReason: string;
54+
55+
@UpdateDateColumn()
56+
updated_at:Date;
57+
58+
@CreateDateColumn()
59+
created_at: Date;
6160

6261
}

src/i18n/en/response.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"login": {
33
"success": "Your identity accepted, login successful."
4+
},
5+
"register": {
6+
"success": "Your account has been created."
7+
},
8+
"logout": {
9+
"success": "Logout successful."
410
}
11+
512
}

0 commit comments

Comments
 (0)