mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
feat: update frontend to use cookie session
* use SessionMiddleware in socket.io server * add back jwtStrategy for commands
This commit is contained in:
1 parent
be5cd152a9
commit
34c277c886
13 files changed
+90
-116
No files matched your search
@@ -5,14 +5,7 @@ const api = axios.create({
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
withCredentials: true,
|
||||
})
|
||||
|
||||
export const setToken = (token) => {
|
||||
if (token) {
|
||||
api.defaults.headers.common.Authorization = `Bearer ${token}`
|
||||
} else {
|
||||
delete api.defaults.headers.common.Authorization
|
||||
}
|
||||
}
|
||||
|
||||
export default api
|
||||
+5
-37
@@ -1,36 +1,9 @@
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import api, { setToken } from './api'
|
||||
|
||||
const TOKEN_KEY = 'JWT_TOKEN'
|
||||
|
||||
const myStorage = window.localStorage
|
||||
const myToken = ref(myStorage.getItem(TOKEN_KEY))
|
||||
|
||||
if (myToken.value) {
|
||||
setToken(myToken.value)
|
||||
}
|
||||
|
||||
// watch the token and store it in localStorage
|
||||
watch(myToken, (newToken) => {
|
||||
if (newToken) {
|
||||
console.log('update token:', newToken)
|
||||
myStorage.setItem(TOKEN_KEY, newToken)
|
||||
setToken(newToken)
|
||||
} else {
|
||||
myStorage.removeItem(TOKEN_KEY)
|
||||
setToken(null)
|
||||
}
|
||||
})
|
||||
|
||||
export const getToken = () => {
|
||||
return myToken.value
|
||||
}
|
||||
import api from './api'
|
||||
|
||||
export const login = async (username, password) => {
|
||||
try {
|
||||
const res = await api.post('/auth/login', { username, password })
|
||||
const { token } = res.data
|
||||
myToken.value = token
|
||||
console.log(res.data)
|
||||
return true
|
||||
} catch (err) {
|
||||
if (err.response?.status === 401) {
|
||||
@@ -40,19 +13,14 @@ export const login = async (username, password) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const register = async (username, email, password) => {
|
||||
const res = await api.post('/auth/register', {
|
||||
export const register = (username, email, password) => {
|
||||
return api.post('/auth/register', {
|
||||
username,
|
||||
password,
|
||||
email,
|
||||
})
|
||||
const { token } = res.data
|
||||
myToken.value = token
|
||||
return true
|
||||
}
|
||||
|
||||
export const logout = () => {
|
||||
myToken.value = null
|
||||
return api.post('/auth/logout')
|
||||
}
|
||||
|
||||
export const isAuthenticated = computed(() => !!myToken.value)
|
||||
@@ -1,7 +1,5 @@
|
||||
import io from 'socket.io-client'
|
||||
|
||||
import { getToken } from './auth'
|
||||
|
||||
class SocketWrapper {
|
||||
constructor() {
|
||||
this.socket = null
|
||||
@@ -18,9 +16,6 @@ class SocketWrapper {
|
||||
query: {
|
||||
session_id: session._id,
|
||||
},
|
||||
auth: {
|
||||
token: getToken(),
|
||||
},
|
||||
})
|
||||
|
||||
this.socket.on('connect', () => {
|
||||
|
||||
Reference in new issue
Block a user