Cập Nhật Thông Tin Khách Hàng
API cập nhật thông tin cá nhân của khách hàng đã đăng nhập. Sử dụng JWT token từ API login để xác thực người dùng.
API hỗ trợ cập nhật các trường: họ tên, email, số điện thoại, ngày sinh, giới tính, địa chỉ.
POST/PUT https://api.socdo.vn/mini-app/v1/update-profile
Request Headers
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...
Content-Type: application/json
Request Body
{
"name": "Nguyễn Văn A",
"email": "[email protected]",
"mobile": "0123456789",
"birthday": "25/03/1990",
"gender": "Nam",
"address": "123 Đường ABC, Phường XYZ, Quận 1, TP.HCM"
}
Tham số
| Tham số |
Loại |
Bắt buộc |
Mô tả |
Authorization |
header |
✓ |
JWT token từ API login (Bearer token) |
name |
string |
- |
Họ và tên đầy đủ |
email |
string |
- |
Địa chỉ email (phải đúng format email) |
mobile |
string |
- |
Số điện thoại (chỉ chứa số) |
birthday |
string |
- |
Ngày sinh (định dạng DD/MM/YYYY hoặc timestamp) |
gender |
string |
- |
Giới tính (Nam/Nữ, Male/Female) |
address |
string |
- |
Địa chỉ đầy đủ |
Response thành công (200)
{
"success": true,
"message": "Cập nhật thông tin thành công",
"data": {
"user_id": 123,
"username": "zalo_123456789",
"name": "Nguyễn Văn A",
"email": "[email protected]",
"mobile": "0123456789",
"avatar": "/uploads/avatar/user.jpg",
"avatar_url": "https://socdo.vn/uploads/avatar/user.jpg",
"gender": "Nam",
"shop": 23933,
"balance": 50000,
"balance2": 0,
"created_at": 1703123456,
"last_login": 1735123456
}
}
Response lỗi (400 - Dữ liệu không hợp lệ)
{
"success": false,
"message": "Email không hợp lệ"
}
Response lỗi (400 - Không có trường để cập nhật)
{
"success": false,
"message": "Không có trường nào để cập nhật"
}
Response lỗi (401 - Token không hợp lệ)
{
"success": false,
"message": "Token không hợp lệ hoặc đã hết hạn",
"error": "Invalid token"
}
Response lỗi (404 - User không tồn tại)
{
"success": false,
"message": "Không tìm thấy người dùng"
}
Ví dụ JavaScript
// Cập nhật thông tin profile
async function updateProfile(profileData) {
try {
// Lấy token từ localStorage (đã lưu khi login)
const authToken = localStorage.getItem('auth_token');
if (!authToken) {
throw new Error('Chưa đăng nhập');
}
const response = await fetch('https://api.socdo.vn/mini-app/v1/update-profile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
},
body: JSON.stringify(profileData)
});
const result = await response.json();
if (result.success) {
console.log('Cập nhật thông tin thành công:', result.data);
// Cập nhật UI với thông tin mới
updateUserProfileUI(result.data);
return result;
} else {
console.error('Lỗi cập nhật:', result.message);
// Nếu token hết hạn, xóa và chuyển về trang đăng nhập
if (response.status === 401) {
localStorage.removeItem('auth_token');
window.location.href = '/login';
}
return result;
}
} catch (error) {
console.error('Lỗi kết nối:', error);
return { success: false, message: 'Lỗi kết nối mạng' };
}
}
// Hàm cập nhật UI với thông tin mới
function updateUserProfileUI(userData) {
if (userData.name) {
document.getElementById('user-name').textContent = userData.name;
}
if (userData.email) {
document.getElementById('user-email').textContent = userData.email;
}
if (userData.mobile) {
document.getElementById('user-phone').textContent = userData.mobile;
}
if (userData.avatar_url) {
document.getElementById('user-avatar').src = userData.avatar_url;
}
}
// Ví dụ sử dụng với form
function handleProfileUpdate(event) {
event.preventDefault();
const formData = {
name: document.getElementById('name-input').value.trim(),
email: document.getElementById('email-input').value.trim(),
mobile: document.getElementById('mobile-input').value.trim(),
birthday: document.getElementById('birthday-input').value.trim(),
gender: document.getElementById('gender-input').value.trim(),
address: document.getElementById('address-input').value.trim()
};
// Chỉ gửi các trường có giá trị
const profileData = {};
for (const key in formData) {
if (formData[key]) {
profileData[key] = formData[key];
}
}
// Kiểm tra có trường nào để cập nhật không
if (Object.keys(profileData).length === 0) {
alert('Vui lòng điền ít nhất một trường để cập nhật');
return;
}
// Gọi API cập nhật
updateProfile(profileData).then(response => {
if (response.success) {
alert('Cập nhật thông tin thành công!');
// Optionally reload page or update UI
location.reload();
} else {
alert('Lỗi: ' + response.message);
}
});
}
// Gắn event listener cho form submit
document.getElementById('profile-form').addEventListener('submit', handleProfileUpdate);
Lưu ý quan trọng
- User Token Required: API yêu cầu user token từ login (Bearer token trong Authorization header)
- Cập nhật một phần: Bạn chỉ cần gửi các trường muốn cập nhật, không cần gửi tất cả các trường
- Email Validation: Email sẽ được kiểm tra định dạng, phải là email hợp lệ
- Birthday Format: Ngày sinh có thể là định dạng DD/MM/YYYY hoặc timestamp, hệ thống sẽ tự động convert
- Gender Mapping: Giới tính sẽ được chuẩn hóa (Nam/Nữ, Male/Female đều được chấp nhận)
- Mobile Format: Số điện thoại sẽ tự động loại bỏ các ký tự không phải số
- Database Update: Trường
date_update sẽ tự động cập nhật thành thời gian hiện tại
- Response Data: API trả về thông tin user đã được cập nhật để client có thể refresh UI
Mapping trường dữ liệu
| Trường trong Request |
Trường trong Database |
Ghi chú |
name |
name |
Họ và tên |
email |
email |
Email (validate format) |
mobile |
mobile |
Số điện thoại (chỉ số) |
birthday |
ngaysinh |
Convert từ DD/MM/YYYY sang timestamp |
gender |
gioi_tinh |
Chuẩn hóa thành Nam/Nữ |
address |
dia_chi |
Địa chỉ đầy đủ |
Bảo mật
- JWT Verification: Sử dụng Firebase JWT để xác thực token
- User Isolation: Mỗi user chỉ có thể cập nhật thông tin của chính mình (dựa trên user_id trong token)
- Input Validation: Tất cả dữ liệu đầu vào đều được validate (email format, birthday format, etc.)
- Database Security: Sử dụng prepared statement để tránh SQL injection
- CORS Headers: Cấu hình CORS để bảo mật cross-origin requests
- Token Expiry Check: Token được kiểm tra thời hạn và issuer
API Liên Quan