Skip to content

Commit

Permalink
bug: 优化op登录(太湖登录情况下,无cookie正常登录) TencentBlueKing#2441
Browse files Browse the repository at this point in the history
* bug: 优化op登录(太湖登录情况下,无cookie正常登录) TencentBlueKing#2441

* bug: 优化op登录(太湖登录情况下,无cookie正常登录) TencentBlueKing#2441
  • Loading branch information
lannoy0523 authored Aug 8, 2024
1 parent 13b41da commit 4b52aab
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/frontend/devops-op/src/layout/components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
},
data() {
return {
userName: localStorage.getItem('userName') || getBkUid()
userName: localStorage.getItem('userName')
}
},
computed: {
Expand Down
18 changes: 15 additions & 3 deletions src/frontend/devops-op/src/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getBkUid, getToken } from '@/utils/auth' // get token from cookie
import { toLoginPage } from '@/utils/login'
import { getBkUid, getTempUid, getToken, setTempUid } from '@/utils/auth' // get token from cookie
import { MODE_CONFIG, MODE_CONFIG_STAND_ALONE, toLoginPage } from '@/utils/login'
import { ROLE_ADMIN } from '@/store/modules/user'

NProgress.configure({ showSpinner: false }) // NProgress Configuration
Expand All @@ -21,9 +21,10 @@ router.beforeEach(async(to, from, next) => {
// determine whether the user has logged in
// const bkTicket = getBkTicket()
const bkUid = getBkUid()
const tempUid = getTempUid()
const token = getToken()
// const hasToken = token || bkTicket || bkUid
const hasToken = token || bkUid
const hasToken = token || bkUid || tempUid
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
Expand Down Expand Up @@ -71,6 +72,17 @@ router.beforeEach(async(to, from, next) => {
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
if (MODE_CONFIG !== MODE_CONFIG_STAND_ALONE) {
try {
const response = await fetch('/web/auth/api/user/info')
if (response.ok) {
const resJson = await response.json()
setTempUid(resJson.data.userId)
}
} catch (error) {
console.error(error)
}
}
toLoginPage(to.path)
NProgress.done()
}
Expand Down
5 changes: 3 additions & 2 deletions src/frontend/devops-op/src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { login, userInfo, userInfoById } from '@/api/user'
import { getToken, setToken, removeToken, getBkUid } from '@/utils/auth'
import { getToken, setToken, removeToken, getBkUid, getTempUid } from '@/utils/auth'
import { resetRouter } from '@/router'
import { MODE_CONFIG, MODE_CONFIG_STAND_ALONE } from '@/utils/login'

Expand Down Expand Up @@ -73,7 +73,7 @@ const actions = {

// get user info
async getInfo({ commit }) {
const uid = getBkUid()
const uid = getBkUid() || getTempUid()
const user = (uid && MODE_CONFIG !== MODE_CONFIG_STAND_ALONE) ? uid : (await getUser()).user
return new Promise((resolve, reject) => {
userInfoById(user).then(response => {
Expand All @@ -86,6 +86,7 @@ const actions = {
}

const avatar = ''
localStorage.setItem('userName', name)
commit('SET_USER_ID', userId)
commit('SET_NAME', name)
commit('SET_ADMIN', admin)
Expand Down
10 changes: 10 additions & 0 deletions src/frontend/devops-op/src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const TOKEN_KEY = 'bkrepo_ticket'
export const BK_TICKET = 'bk_ticket'
export const BK_UID = 'bk_uid'

const BK_TEMP_UID = 'bk_temp_uid'

export function getBkTicket() {
return Cookies.get(BK_TICKET)
}
Expand All @@ -20,6 +22,14 @@ export function setToken(token) {
return Cookies.set(TOKEN_KEY, token)
}

export function setTempUid(uid) {
return Cookies.set(BK_TEMP_UID, uid)
}

export function getTempUid() {
return Cookies.get(BK_TEMP_UID)
}

export function removeToken() {
return Cookies.remove(TOKEN_KEY)
}

0 comments on commit 4b52aab

Please sign in to comment.