Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
finwise-miniapp-be
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ThinhNC
finwise-miniapp-be
Commits
ac6f8aa3
Commit
ac6f8aa3
authored
Sep 10, 2026
by
ThinhNC
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: implement authentication service and repository for user management and session handling
parent
97a31c0d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
0 deletions
+55
-0
auth.repository.ts
src/modules/auth/auth.repository.ts
+1
-0
auth.service.ts
src/modules/auth/auth.service.ts
+29
-0
zalo-auth.test.ts
tests/zalo-auth.test.ts
+25
-0
No files found.
src/modules/auth/auth.repository.ts
View file @
ac6f8aa3
...
@@ -168,6 +168,7 @@ export class AuthRepository {
...
@@ -168,6 +168,7 @@ export class AuthRepository {
async
updateProfile
(
userId
:
string
,
data
:
{
async
updateProfile
(
userId
:
string
,
data
:
{
fullName
?:
string
;
fullName
?:
string
;
phoneNumber
?:
string
|
null
;
phoneNumber
?:
string
|
null
;
avatarUrl
?:
string
|
null
;
})
{
})
{
return
prisma
.
user
.
update
({
return
prisma
.
user
.
update
({
where
:
{
id
:
userId
},
where
:
{
id
:
userId
},
...
...
src/modules/auth/auth.service.ts
View file @
ac6f8aa3
...
@@ -553,6 +553,35 @@ export class AuthService {
...
@@ -553,6 +553,35 @@ export class AuthService {
provider
:
'zalo'
,
provider
:
'zalo'
,
providerUserId
:
zaloId
,
providerUserId
:
zaloId
,
});
});
}
else
{
// User đã tồn tại: Tự động cập nhật SĐT nếu có resolvedPhone mà user chưa có hoặc khác SĐT cũ
const
updateData
:
{
phoneNumber
?:
string
;
fullName
?:
string
;
avatarUrl
?:
string
}
=
{};
if
(
resolvedPhone
&&
user
.
phoneNumber
!==
resolvedPhone
)
{
// Kiểm tra xem số điện thoại này có đang thuộc về tài khoản khác không để tránh lỗi Unique constraint
const
existingPhoneUser
=
await
this
.
repository
.
findByPhone
(
resolvedPhone
);
if
(
!
existingPhoneUser
||
existingPhoneUser
.
id
===
user
.
id
)
{
updateData
.
phoneNumber
=
resolvedPhone
;
}
else
{
console
.
warn
(
`[ZaloAuth] Phone number
${
resolvedPhone
}
is already linked to another user
${
existingPhoneUser
.
id
}
`
);
}
}
// Cập nhật thêm tên hoặc avatar nếu tài khoản hiện tại chưa có
if
(
!
user
.
fullName
&&
zaloName
)
{
updateData
.
fullName
=
zaloName
;
}
if
(
!
user
.
avatarUrl
&&
zaloAvatarUrl
)
{
updateData
.
avatarUrl
=
zaloAvatarUrl
;
}
if
(
Object
.
keys
(
updateData
).
length
>
0
)
{
try
{
user
=
await
this
.
repository
.
updateProfile
(
user
.
id
,
updateData
);
}
catch
(
updateErr
)
{
console
.
warn
(
'[ZaloAuth] Failed to update user profile with resolved Zalo info:'
,
updateErr
);
}
}
}
}
// Load role relation nếu chưa có (createSocialUser đã include)
// Load role relation nếu chưa có (createSocialUser đã include)
...
...
tests/zalo-auth.test.ts
View file @
ac6f8aa3
...
@@ -158,5 +158,30 @@ describe('Zalo Auth Integration Tests', () => {
...
@@ -158,5 +158,30 @@ describe('Zalo Auth Integration Tests', () => {
});
});
expect
(
count
).
toBe
(
1
);
expect
(
count
).
toBe
(
1
);
});
});
it
(
'should update phoneNumber for existing user whose phoneNumber was previously null'
,
async
()
=>
{
// Giả lập user đã tạo trước đó nhưng phoneNumber là null
await
prisma
.
user
.
updateMany
({
where
:
{
phoneNumber
:
testPhone
},
data
:
{
phoneNumber
:
null
},
});
const
res
=
await
request
(
app
)
.
post
(
'/api/v1/auth/zalo-login'
)
.
send
({
accessToken
:
'valid_mock_token_123'
,
phoneToken
:
'valid_phone_token_abc'
,
});
expect
(
res
.
status
).
toBe
(
200
);
expect
(
res
.
body
.
success
).
toBe
(
true
);
expect
(
res
.
body
.
data
.
user
.
phoneNumber
).
toBe
(
testPhone
);
const
dbUser
=
await
prisma
.
user
.
findFirst
({
where
:
{
phoneNumber
:
testPhone
},
});
expect
(
dbUser
).
not
.
toBeNull
();
expect
(
dbUser
?.
phoneNumber
).
toBe
(
testPhone
);
});
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment