Commit 236dc091 authored by ThinhNC's avatar ThinhNC

feat: implement AuthService to handle user authentication, session management,...

feat: implement AuthService to handle user authentication, session management, and profile operations
parent e66d662c
...@@ -465,6 +465,14 @@ export class AuthService { ...@@ -465,6 +465,14 @@ export class AuthService {
let zaloName = dto.name || 'Người dùng Zalo'; let zaloName = dto.name || 'Người dùng Zalo';
let zaloAvatarUrl: string | null = dto.avatar || null; let zaloAvatarUrl: string | null = dto.avatar || null;
console.log('[ZaloAuth] Incoming login request:', {
clientZaloId: dto.zaloId,
clientName: dto.name,
clientAvatar: dto.avatar,
hasPhoneToken: Boolean(dto.phoneToken),
hasPhoneNumber: Boolean(dto.phoneNumber),
});
try { try {
const appsecretProof = crypto const appsecretProof = crypto
.createHmac('sha256', appSecret) .createHmac('sha256', appSecret)
...@@ -472,6 +480,7 @@ export class AuthService { ...@@ -472,6 +480,7 @@ export class AuthService {
.digest('hex'); .digest('hex');
const zaloProfile = await this.fetchZaloProfile(accessToken, appsecretProof); const zaloProfile = await this.fetchZaloProfile(accessToken, appsecretProof);
console.log('[ZaloAuth] fetchZaloProfile response:', zaloProfile);
if (zaloProfile && zaloProfile.id) { if (zaloProfile && zaloProfile.id) {
zaloId = zaloProfile.id; zaloId = zaloProfile.id;
if (zaloProfile.name) zaloName = zaloProfile.name; if (zaloProfile.name) zaloName = zaloProfile.name;
...@@ -497,6 +506,7 @@ export class AuthService { ...@@ -497,6 +506,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);
console.log('[ZaloAuth] fetchZaloPhoneNumber response:', phoneResponse);
if (phoneResponse && phoneResponse.data?.number) { if (phoneResponse && phoneResponse.data?.number) {
resolvedPhone = phoneResponse.data.number; resolvedPhone = phoneResponse.data.number;
} else if (phoneResponse?.error === -501) { } else if (phoneResponse?.error === -501) {
...@@ -553,6 +563,7 @@ export class AuthService { ...@@ -553,6 +563,7 @@ export class AuthService {
provider: 'zalo', provider: 'zalo',
providerUserId: zaloId, providerUserId: zaloId,
}); });
console.log('[ZaloAuth] Created new user:', user.id);
} else { } 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ũ // 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 } = {}; const updateData: { phoneNumber?: string; fullName?: string; avatarUrl?: string } = {};
...@@ -576,11 +587,15 @@ export class AuthService { ...@@ -576,11 +587,15 @@ export class AuthService {
} }
if (Object.keys(updateData).length > 0) { if (Object.keys(updateData).length > 0) {
console.log(`[ZaloAuth] Updating existing user ${user.id} with:`, updateData);
try { try {
user = await this.repository.updateProfile(user.id, updateData); user = await this.repository.updateProfile(user.id, updateData);
console.log(`[ZaloAuth] Successfully updated user ${user.id}`);
} catch (updateErr) { } catch (updateErr) {
console.warn('[ZaloAuth] Failed to update user profile with resolved Zalo info:', updateErr); console.warn('[ZaloAuth] Failed to update user profile with resolved Zalo info:', updateErr);
} }
} else {
console.log(`[ZaloAuth] No new data to update for user ${user.id}`);
} }
} }
......
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