Implement login page with email/password authentication #69
@@ -0,0 +1,9 @@
|
||||
### Get All Students
|
||||
POST http://localhost:8000/api/login
|
||||
content-Type: application/json
|
||||
Accept: application/json
|
||||
|
||||
{
|
||||
"email": "[email protected]",
|
||||
"password": "123"
|
||||
}
|
||||
@@ -64,16 +64,16 @@ const formData = ref({
|
||||
|
||||
|
||||
const handleSubmit = async () => {
|
||||
console.log(formData, authStore)
|
||||
toast.promise(authStore.login(formData.value), {
|
||||
const promise = authStore.login(formData.value)
|
||||
toast.promise(promise, {
|
||||
loading: 'Calling API',
|
||||
success: (data) => {
|
||||
return `Login Sucessfull - ${data?.name}`
|
||||
success: (user) => {
|
||||
router.push('/')
|
||||
return `Login Successful - ${user.name}`
|
||||
},
|
||||
error: (data) => `[API] Error - ${data?.response?.data?.message}`
|
||||
error: (error) =>
|
||||
error.response?.data?.message
|
||||
})
|
||||
|
||||
|
||||
router.push('/')
|
||||
}
|
||||
</script>
|
||||
@@ -16,11 +16,15 @@ export const useAPIStore = defineStore('api', () => {
|
||||
},
|
||||
})
|
||||
|
||||
// AUTH
|
||||
const postLogin = async (credentials) => {
|
||||
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`)
|
||||
|
||||
@@ -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
|
||||
|
||||
const loginResp = await apiStore.postLogin(credentials)
|
||||
const userResp = await apiStore.getAuthUser()
|
||||
const jwtToken = loginResp.data.token
|
||||
setToken(jwtToken)
|
||||
currentUser.value = response.data
|
||||
return response.data
|
||||
currentUser.value = userResp.data
|
||||
return userResp.data
|
||||
}
|
||||
|
||||
const logout = async () => {
|
||||
|
||||
@@ -2,18 +2,24 @@ import { defineStore } from 'pinia'
|
||||
import { inject } from 'vue'
|
||||
|
||||
export const useSocketStore = defineStore('socket', () => {
|
||||
try {
|
||||
const socket = inject('socket')
|
||||
|
||||
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,
|
||||
}
|
||||
} catch (error) {
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user