feat: Implement E2E tests for authentication, student account, discovery, and classroom features, alongside new browse pages and a useAuth composable.
All checks were successful
Build and Deploy Frontend Learner / Build Frontend Learner Docker Image (push) Successful in 48s
Build and Deploy Frontend Learner / Deploy E-learning Frontend Learner to Dev Server (push) Successful in 4s
Build and Deploy Frontend Learner / Notify Deployment Status (push) Successful in 1s
All checks were successful
Build and Deploy Frontend Learner / Build Frontend Learner Docker Image (push) Successful in 48s
Build and Deploy Frontend Learner / Deploy E-learning Frontend Learner to Dev Server (push) Successful in 4s
Build and Deploy Frontend Learner / Notify Deployment Status (push) Successful in 1s
This commit is contained in:
parent
0205aab461
commit
b0b665f588
35 changed files with 546 additions and 862 deletions
|
|
@ -40,7 +40,7 @@ export const useAuth = () => {
|
|||
// ฟังก์ชันเข้าสู่ระบบ (Login)
|
||||
const login = async (credentials: { email: string; password: string }) => {
|
||||
try {
|
||||
// API returns { code: 200, message: "...", data: { token, user, ... } }
|
||||
// API returns { code: 200, message: "...", data: { token, refreshToken } }
|
||||
const response = await $fetch<any>(`${API_BASE_URL}/auth/login`, {
|
||||
method: 'POST',
|
||||
body: credentials
|
||||
|
|
@ -49,16 +49,35 @@ export const useAuth = () => {
|
|||
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 ไม่ถูกต้อง' }
|
||||
}
|
||||
|
||||
// บันทึก Token ก่อน เพื่อใช้เรียก /user/me
|
||||
token.value = data.token
|
||||
refreshToken.value = data.refreshToken // บันทึก Refresh Token
|
||||
|
||||
// API ส่งข้อมูล profile มาใน user object
|
||||
user.value = data.user
|
||||
refreshToken.value = data.refreshToken
|
||||
|
||||
// ดึงข้อมูลผู้ใช้จาก /user/me (เพราะ API login ไม่ส่ง user กลับมาแล้ว)
|
||||
try {
|
||||
const userData = await $fetch<any>(`${API_BASE_URL}/user/me`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${data.token}`
|
||||
}
|
||||
})
|
||||
|
||||
// Validation: ตรวจสอบ Role ต้องเป็น STUDENT เท่านั้น
|
||||
if (!userData || !userData.role || userData.role.code !== 'STUDENT') {
|
||||
// ถ้า Role ไม่ใช่ STUDENT ให้ล้าง Token ออก
|
||||
token.value = null
|
||||
refreshToken.value = null
|
||||
return { success: false, error: 'Email ไม่ถูกต้อง' }
|
||||
}
|
||||
|
||||
// เก็บข้อมูล User ลง Cookie
|
||||
user.value = userData
|
||||
} catch (profileErr) {
|
||||
// ดึงข้อมูลผู้ใช้ไม่สำเร็จ ให้ล้าง Token ออก
|
||||
console.error('Failed to fetch user profile after login:', profileErr)
|
||||
token.value = null
|
||||
refreshToken.value = null
|
||||
return { success: false, error: 'ไม่สามารถดึงข้อมูลผู้ใช้ได้' }
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue