feature implement login part2
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user