feat: Implement core authentication and course management logic with new discovery and profile pages.
This commit is contained in:
parent
1aa3190ca4
commit
2ffcc36fe4
12 changed files with 397 additions and 89 deletions
|
|
@ -8,6 +8,8 @@ interface User {
|
|||
id: number
|
||||
username: string
|
||||
email: string
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
role: {
|
||||
code: string
|
||||
name: { th: string; en: string }
|
||||
|
|
@ -167,6 +169,36 @@ export const useAuth = () => {
|
|||
}
|
||||
}
|
||||
|
||||
// Update User Profile
|
||||
const updateUserProfile = async (payload: {
|
||||
first_name: string
|
||||
last_name: string
|
||||
phone: string
|
||||
prefix: { th: string; en: string }
|
||||
}) => {
|
||||
if (!token.value) return
|
||||
|
||||
try {
|
||||
const { error } = await useFetch(`${API_BASE_URL}/user/me`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.value}`
|
||||
},
|
||||
body: payload
|
||||
})
|
||||
|
||||
if (error.value) throw error.value
|
||||
|
||||
// If successful, refresh the local user data
|
||||
await fetchUserProfile()
|
||||
|
||||
return { success: true }
|
||||
} catch (err: any) {
|
||||
console.error('Failed to update profile:', err)
|
||||
return { success: false, error: err.data?.message || err.message || 'บันทึกข้อมูลไม่สำเร็จ' }
|
||||
}
|
||||
}
|
||||
|
||||
// Request Password Reset
|
||||
const requestPasswordReset = async (email: string) => {
|
||||
try {
|
||||
|
|
@ -197,6 +229,26 @@ export const useAuth = () => {
|
|||
}
|
||||
}
|
||||
|
||||
// Change Password
|
||||
const changePassword = async (payload: { oldPassword: string, newPassword: string }) => {
|
||||
if (!token.value) return { success: false, error: 'ไม่พบ Token การใช้งาน' }
|
||||
|
||||
try {
|
||||
const { data, error } = await useFetch(`${API_BASE_URL}/user/change-password`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.value}`
|
||||
},
|
||||
body: payload
|
||||
})
|
||||
|
||||
if (error.value) throw error.value
|
||||
return { success: true }
|
||||
} catch (err: any) {
|
||||
return { success: false, error: err.data?.message || 'เปลี่ยนรหัสผ่านไม่สำเร็จ' }
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh Access Token
|
||||
const refreshAccessToken = async () => {
|
||||
if (!refreshToken.value) return false
|
||||
|
|
@ -243,20 +295,23 @@ export const useAuth = () => {
|
|||
const lastName = user.value.profile?.last_name || ''
|
||||
|
||||
return {
|
||||
prefix,
|
||||
prefix: user.value.profile?.prefix || { th: '', en: '' },
|
||||
firstName,
|
||||
lastName,
|
||||
email: user.value.email,
|
||||
phone: user.value.profile?.phone || '',
|
||||
photoURL: user.value.profile?.avatar_url || '',
|
||||
role: user.value.role
|
||||
role: user.value.role,
|
||||
createdAt: user.value.created_at || new Date().toISOString()
|
||||
}
|
||||
}),
|
||||
login,
|
||||
register,
|
||||
fetchUserProfile,
|
||||
updateUserProfile,
|
||||
requestPasswordReset,
|
||||
confirmResetPassword,
|
||||
changePassword,
|
||||
refreshAccessToken,
|
||||
logout
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue