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
73855b03
Commit
73855b03
authored
Aug 29, 2026
by
ThinhNC
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: auth service logic and validation schemas for user management
parent
5d8c61c1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
34 deletions
+37
-34
auth.service.ts
src/modules/auth/auth.service.ts
+34
-30
auth.validation.ts
src/modules/auth/auth.validation.ts
+3
-4
No files found.
src/modules/auth/auth.service.ts
View file @
73855b03
...
...
@@ -480,56 +480,60 @@ export class AuthService {
console
.
warn
(
'[ZaloAuth] fetchZaloProfile caught error:'
,
err
);
}
// 2. Lấy và chuẩn hóa số điện thoại (từ phoneToken hoặc phoneNumber)
// 2. Lấy và chuẩn hóa số điện thoại (từ phoneToken hoặc phoneNumber
nếu có thể giải mã
)
let
resolvedPhone
=
dto
.
phoneNumber
;
if
(
dto
.
phoneToken
)
{
const
phoneResponse
=
await
this
.
fetchZaloPhoneNumber
(
accessToken
,
dto
.
phoneToken
,
appSecret
);
if
(
!
phoneResponse
||
(
phoneResponse
.
error
!==
undefined
&&
phoneResponse
.
error
!==
0
)
||
!
phoneResponse
.
data
?.
number
)
{
console
.
error
(
'[ZaloAuth] fetchZaloPhoneNumber failed:'
,
phoneResponse
);
throw
new
AppError
(
phoneResponse
?.
message
?
`Zalo Phone Error:
${
phoneResponse
.
message
}
`
:
'Failed to decode phone number from Zalo token'
,
401
,
ERROR_CODE
.
INVALID_CREDENTIALS
,
);
try
{
const
phoneResponse
=
await
this
.
fetchZaloPhoneNumber
(
accessToken
,
dto
.
phoneToken
,
appSecret
);
if
(
phoneResponse
&&
phoneResponse
.
data
?.
number
)
{
resolvedPhone
=
phoneResponse
.
data
.
number
;
}
else
if
(
phoneResponse
?.
error
===
-
501
)
{
console
.
warn
(
'[ZaloAuth] Zalo Phone API limited by IP location (-501). Authenticating via Zalo ID.'
);
}
else
if
(
phoneResponse
&&
phoneResponse
.
error
!==
undefined
&&
phoneResponse
.
error
!==
0
)
{
console
.
warn
(
'[ZaloAuth] fetchZaloPhoneNumber returned error:'
,
phoneResponse
);
}
}
catch
(
err
)
{
console
.
warn
(
'[ZaloAuth] fetchZaloPhoneNumber caught error:'
,
err
);
}
resolvedPhone
=
phoneResponse
.
data
.
number
;
}
if
(
!
resolvedPhone
)
{
throw
new
AppError
(
'Phone number is required'
,
400
,
ERROR_CODE
.
VALIDATION_ERROR
);
if
(
resolvedPhone
)
{
// Chuẩn hóa số điện thoại: +84... hoặc 84... -> 0...
resolvedPhone
=
resolvedPhone
.
replace
(
/^
\+
84/
,
'0'
).
replace
(
/^84/
,
'0'
);
}
// Chuẩn hóa số điện thoại: +84... hoặc 84... -> 0...
resolvedPhone
=
resolvedPhone
.
replace
(
/^
\+
84/
,
'0'
).
replace
(
/^84/
,
'0'
);
// Đảm bảo có Zalo ID làm mã định danh
if
(
!
zaloId
)
{
zaloId
=
`zalo_
${
resolvedPhone
}
`
;
if
(
resolvedPhone
)
{
zaloId
=
`zalo_
${
resolvedPhone
}
`
;
}
else
{
throw
new
AppError
(
'Unable to identify Zalo user. Please grant basic permissions.'
,
400
,
ERROR_CODE
.
VALIDATION_ERROR
);
}
}
// 3. Tìm hoặc tạo user theo SĐT
let
user
=
await
this
.
repository
.
findByPhone
(
resolvedPhone
);
// 3. Tìm hoặc tạo user
// A. Tìm theo liên kết mạng xã hội Zalo ID trước
let
user
:
any
=
await
this
.
repository
.
findBySocial
(
'zalo'
,
zaloId
);
if
(
user
)
{
// User đã tồn tại — liên kết Zalo ID nếu chưa có
const
existing
=
await
this
.
repository
.
findBySocial
(
'zalo'
,
zaloId
);
if
(
!
existing
)
{
// B. Nếu chưa tìm thấy theo Zalo ID và có SĐT, tìm theo SĐT
if
(
!
user
&&
resolvedPhone
)
{
user
=
await
this
.
repository
.
findByPhone
(
resolvedPhone
);
if
(
user
)
{
await
this
.
repository
.
linkSocialAccount
(
user
.
id
,
'zalo'
,
zaloId
);
}
}
else
{
// User chưa tồn tại — tạo mới từ Zalo profile
}
// C. Nếu user chưa tồn tại, tạo mới
if
(
!
user
)
{
const
role
=
await
this
.
repository
.
findRoleByName
(
SYSTEM_ROLES
.
USER
);
if
(
!
role
)
{
throw
new
AppError
(
'Default role not found'
,
500
,
ERROR_CODE
.
INTERNAL_SERVER_ERROR
);
}
user
=
await
this
.
repository
.
createSocialUser
({
fullName
:
zaloName
||
undefined
,
fullName
:
zaloName
||
'Người dùng Zalo'
,
avatarUrl
:
zaloAvatarUrl
||
undefined
,
phoneNumber
:
resolvedPhone
,
phoneNumber
:
resolvedPhone
||
undefined
,
roleId
:
role
.
id
,
provider
:
'zalo'
,
providerUserId
:
zaloId
,
...
...
src/modules/auth/auth.validation.ts
View file @
73855b03
...
...
@@ -22,9 +22,9 @@ export const zaloLoginSchema = z
name
:
z
.
string
().
optional
(),
avatar
:
z
.
string
().
optional
(),
})
.
refine
((
data
)
=>
data
.
phoneToken
||
data
.
phoneNumber
,
{
message
:
'Either phoneToken
or phoneNumber
must be provided'
,
path
:
[
'
phone
Token'
],
.
refine
((
data
)
=>
data
.
phoneToken
||
data
.
phoneNumber
||
data
.
zaloId
,
{
message
:
'Either phoneToken
, phoneNumber, or zaloId
must be provided'
,
path
:
[
'
access
Token'
],
});
export
const
refreshSchema
=
z
.
object
({
...
...
@@ -135,4 +135,3 @@ export const sessionQuerySchema = z.object({
export
const
revokeOtherSessionsSchema
=
z
.
object
({
refreshToken
:
z
.
string
().
optional
(),
});
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