Skip to content

Commit

Permalink
fix: cookies无法由js修改问题
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed May 10, 2024
1 parent 9d71f2a commit a3b98bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
10 changes: 7 additions & 3 deletions backend/controller/accapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ func (ar *AccountRouter) LoginAccount(c *gin.Context) {
domain := c.Request.Header.Get("Origin")

// set-cookie
// 要求:
// 1. 调试模式时允许跨域
// 2. 设置的域为请求的域
// 3. 允许js修改
if gin.Mode() == gin.DebugMode {
http.SetCookie(c.Writer, &http.Cookie{
Name: "access-token",
Value: token,
Path: "/",
Domain: domain,
Secure: true,
SameSite: http.SameSiteNoneMode,
Secure: false,
SameSite: http.SameSiteLaxMode,
HttpOnly: false,
MaxAge: 3600,
})
Expand All @@ -99,7 +103,7 @@ func (ar *AccountRouter) LoginAccount(c *gin.Context) {
Value: token,
Path: "/",
Domain: domain,
Secure: true,
Secure: false,
SameSite: http.SameSiteStrictMode,
HttpOnly: false,
MaxAge: 3600,
Expand Down
3 changes: 0 additions & 3 deletions frontend/config.json

This file was deleted.

25 changes: 15 additions & 10 deletions frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ app.use(store)
app.use(VueCookies)

// let config=require("../config.json");
fetch('/config.json').then(response => response.json()).then(config => {
console.log(config)
store.commit('setBaseURL', config.base_url)
}).then(() => {
const axiosInstance = axios.create({
withCredentials: true,
baseURL: store.state.base_url,
})
app.config.globalProperties.$axios = { ...axiosInstance }
// fetch('/config.json').then(response => response.json()).then(config => {
// console.log(config)
// store.commit('setBaseURL', config.base_url)
// }).then(() => {
// const axiosInstance = axios.create({
// withCredentials: true,
// baseURL: store.state.base_url,
// })
// app.config.globalProperties.$axios = { ...axiosInstance }
// })

const axiosInstance = axios.create({
withCredentials: true,
baseURL: store.state.base_url,
})

app.config.globalProperties.$axios = { ...axiosInstance }

registerPlugins(app)

Expand Down

0 comments on commit a3b98bf

Please sign in to comment.