feat: Implement initial landing pages, authentication flows, and course browsing functionality with i18n.
This commit is contained in:
parent
7de5457170
commit
b60a3853cd
12 changed files with 822 additions and 606 deletions
|
|
@ -78,14 +78,17 @@ export const useAuth = () => {
|
|||
// ฟังก์ชันเข้าสู่ระบบ (Login)
|
||||
const login = async (credentials: { email: string; password: string }) => {
|
||||
try {
|
||||
const data = await $fetch<loginResponse>(`${API_BASE_URL}/auth/login`, {
|
||||
// API returns { code: 200, message: "...", data: { token, user, ... } }
|
||||
const response = await $fetch<any>(`${API_BASE_URL}/auth/login`, {
|
||||
method: 'POST',
|
||||
body: credentials
|
||||
})
|
||||
|
||||
if (data) {
|
||||
// Validation: อนุญาตเฉพาะ Role 'STUDENT' เท่านั้น
|
||||
if (data.user.role.code !== 'STUDENT') {
|
||||
if (response && response.data) {
|
||||
const data = response.data
|
||||
|
||||
// Validation: Ensure user and role exist, then check for Role 'STUDENT'
|
||||
if (!data.user || !data.user.role || data.user.role.code !== 'STUDENT') {
|
||||
return { success: false, error: 'Email ไม่ถูกต้อง' }
|
||||
}
|
||||
|
||||
|
|
@ -360,6 +363,44 @@ export const useAuth = () => {
|
|||
return false
|
||||
}
|
||||
|
||||
// ฟังก์ชันส่งอีเมลยืนยันตัวตน
|
||||
const sendVerifyEmail = async () => {
|
||||
if (!token.value) return { success: false, error: 'Token missing' }
|
||||
|
||||
const doSend = async () => {
|
||||
return await $fetch<{code: number, message: string}>(`${API_BASE_URL}/user/send-verify-email`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.value}`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await doSend()
|
||||
return { success: true, message: res.message }
|
||||
} catch (err: any) {
|
||||
if (err.statusCode === 400) {
|
||||
return { success: false, error: 'Email already verified', code: 400 }
|
||||
}
|
||||
if (err.statusCode === 401) {
|
||||
const refreshed = await refreshAccessToken()
|
||||
if (refreshed) {
|
||||
try {
|
||||
const res = await doSend()
|
||||
return { success: true, message: res.message }
|
||||
} catch (retryErr: any) {
|
||||
return { success: false, error: retryErr.data?.message || 'Failed to send verification email' }
|
||||
}
|
||||
} else {
|
||||
logout()
|
||||
return { success: false, error: 'Session expired' }
|
||||
}
|
||||
}
|
||||
return { success: false, error: err.data?.message || 'Failed to send verification email' }
|
||||
}
|
||||
}
|
||||
|
||||
// ฟังก์ชันออกจากระบบ (Logout)
|
||||
const logout = () => {
|
||||
token.value = null
|
||||
|
|
@ -401,6 +442,7 @@ export const useAuth = () => {
|
|||
changePassword,
|
||||
uploadAvatar,
|
||||
refreshAccessToken,
|
||||
logout
|
||||
logout,
|
||||
sendVerifyEmail
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue