Commit 93ab2d01 authored by ThinhNC's avatar ThinhNC

feat: implement authentication service with JWT handling, device tracking, and email verification

parent 236dc091
...@@ -459,6 +459,9 @@ export class AuthService { ...@@ -459,6 +459,9 @@ export class AuthService {
): Promise<LoginResponseDto> { ): Promise<LoginResponseDto> {
const { accessToken } = dto; const { accessToken } = dto;
const appSecret = process.env.ZALO_APP_SECRET || ''; const appSecret = process.env.ZALO_APP_SECRET || '';
const appsecretProof = appSecret
? crypto.createHmac('sha256', appSecret).update(accessToken).digest('hex')
: '';
// 1. Xác thực access_token và lấy thông tin Zalo profile (có fallback khi IP server ở nước ngoài) // 1. Xác thực access_token và lấy thông tin Zalo profile (có fallback khi IP server ở nước ngoài)
let zaloId = dto.zaloId || ''; let zaloId = dto.zaloId || '';
...@@ -474,11 +477,6 @@ export class AuthService { ...@@ -474,11 +477,6 @@ export class AuthService {
}); });
try { try {
const appsecretProof = crypto
.createHmac('sha256', appSecret)
.update(accessToken)
.digest('hex');
const zaloProfile = await this.fetchZaloProfile(accessToken, appsecretProof); const zaloProfile = await this.fetchZaloProfile(accessToken, appsecretProof);
console.log('[ZaloAuth] fetchZaloProfile response:', zaloProfile); console.log('[ZaloAuth] fetchZaloProfile response:', zaloProfile);
if (zaloProfile && zaloProfile.id) { if (zaloProfile && zaloProfile.id) {
...@@ -505,7 +503,7 @@ export class AuthService { ...@@ -505,7 +503,7 @@ export class AuthService {
if (dto.phoneToken) { if (dto.phoneToken) {
try { try {
const phoneResponse = await this.fetchZaloPhoneNumber(accessToken, dto.phoneToken, appSecret); const phoneResponse = await this.fetchZaloPhoneNumber(accessToken, dto.phoneToken, appSecret, appsecretProof);
console.log('[ZaloAuth] fetchZaloPhoneNumber response:', phoneResponse); console.log('[ZaloAuth] fetchZaloPhoneNumber response:', phoneResponse);
if (phoneResponse && phoneResponse.data?.number) { if (phoneResponse && phoneResponse.data?.number) {
resolvedPhone = phoneResponse.data.number; resolvedPhone = phoneResponse.data.number;
...@@ -694,17 +692,28 @@ export class AuthService { ...@@ -694,17 +692,28 @@ export class AuthService {
accessToken: string, accessToken: string,
phoneToken: string, phoneToken: string,
appSecret: string, appSecret: string,
appsecretProof?: string,
): Promise<ZaloPhoneResponse> { ): Promise<ZaloPhoneResponse> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const proof =
appsecretProof ||
(appSecret ? crypto.createHmac('sha256', appSecret).update(accessToken).digest('hex') : '');
const headers: Record<string, string> = {
access_token: accessToken,
code: phoneToken,
secret_key: appSecret,
};
if (proof) {
headers.appsecret_proof = proof;
}
const options = { const options = {
hostname: 'graph.zalo.me', hostname: 'graph.zalo.me',
path: '/v2.0/me/info', path: '/v2.0/me/info',
method: 'GET', method: 'GET',
headers: { headers,
access_token: accessToken,
code: phoneToken,
secret_key: appSecret,
},
}; };
const req = https.request(options, (res) => { const req = https.request(options, (res) => {
let data = ''; let data = '';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment