From a5663879987946c299078c5d676595c9c2a7bc37 Mon Sep 17 00:00:00 2001 From: Edd Date: Sat, 20 Dec 2025 21:59:33 +0000 Subject: [PATCH] feature implement login part2 --- api/sample-requests.http | 9 +++++++++ frontend/src/pages/login/LoginPage.vue | 18 ++++++++--------- frontend/src/stores/api.js | 14 ++++++++----- frontend/src/stores/auth.js | 14 ++++++------- frontend/src/stores/socket.js | 28 ++++++++++++++++---------- 5 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 api/sample-requests.http diff --git a/api/sample-requests.http b/api/sample-requests.http new file mode 100644 index 0000000..889ba57 --- /dev/null +++ b/api/sample-requests.http @@ -0,0 +1,9 @@ +### Get All Students +POST http://localhost:8000/api/login +content-Type: application/json +Accept: application/json + +{ + "email": "pa@mail.pt", + "password": "123" +} \ No newline at end of file diff --git a/frontend/src/pages/login/LoginPage.vue b/frontend/src/pages/login/LoginPage.vue index 7ddc2e5..0098fa7 100644 --- a/frontend/src/pages/login/LoginPage.vue +++ b/frontend/src/pages/login/LoginPage.vue @@ -64,16 +64,16 @@ const formData = ref({ const handleSubmit = async () => { - console.log(formData, authStore) - toast.promise(authStore.login(formData.value), { - loading: 'Calling API', - success: (data) => { - return `Login Sucessfull - ${data?.name}` - }, - error: (data) => `[API] Error - ${data?.response?.data?.message}` + const promise = authStore.login(formData.value) + toast.promise(promise, { + loading: 'Calling API', + success: (user) => { + router.push('/') + return `Login Successful - ${user.name}` + }, + error: (error) => + error.response?.data?.message }) - - router.push('/') } \ No newline at end of file diff --git a/frontend/src/stores/api.js b/frontend/src/stores/api.js index 3920e25..8774bd2 100644 --- a/frontend/src/stores/api.js +++ b/frontend/src/stores/api.js @@ -16,12 +16,16 @@ export const useAPIStore = defineStore('api', () => { }, }) - // AUTH const postLogin = async (credentials) => { - const response = await axios.post(`${API_BASE_URL}/login`, credentials) - token.value = response.data.token - axios.defaults.headers.common['Authorization'] = `Bearer ${token.value}` - } + const response = await axios.post(`${API_BASE_URL}/login`, credentials) + + if (!response.data.token) throw response + + token.value = response.data.token + axios.defaults.headers.common['Authorization'] = `Bearer ${token.value}` + + return response +} const postLogout = async () => { await axios.post(`${API_BASE_URL}/logout`) token.value = undefined diff --git a/frontend/src/stores/auth.js b/frontend/src/stores/auth.js index c5bef31..43c8434 100644 --- a/frontend/src/stores/auth.js +++ b/frontend/src/stores/auth.js @@ -30,14 +30,12 @@ export const useAuthStore = defineStore('auth', () => { } const login = async (credentials) => { - await apiStore.postLogin(credentials) - const response = await apiStore.getAuthUser() - const jwtToken = response - const user = response.data - - setToken(jwtToken) - currentUser.value = response.data - return response.data + const loginResp = await apiStore.postLogin(credentials) + const userResp = await apiStore.getAuthUser() + const jwtToken = loginResp.data.token + setToken(jwtToken) + currentUser.value = userResp.data + return userResp.data } const logout = async () => { diff --git a/frontend/src/stores/socket.js b/frontend/src/stores/socket.js index 0af311c..f26b116 100644 --- a/frontend/src/stores/socket.js +++ b/frontend/src/stores/socket.js @@ -2,18 +2,24 @@ import { defineStore } from 'pinia' import { inject } from 'vue' export const useSocketStore = defineStore('socket', () => { - const socket = inject('socket') + try { + const socket = inject('socket') - const handleConnection = () => { - socket.on('connect', () => { - console.log(`[Socket] Connected -- ${socket.id}`) - }) - socket.on('disconnect', () => { - console.log(`[Socket] Disconnected -- ${socket.id}`) - }) - } + const handleConnection = () => { + try { + socket.on('connect', () => { + console.log(`[Socket] Connected -- ${socket.id}`) + }) + socket.on('disconnect', () => { + console.log(`[Socket] Disconnected -- ${socket.id}`) + }) + } catch (error) {} + } - return { - handleConnection, + return { + handleConnection, + } + } catch (error) { } + })