Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#1008] Add check oauth configs from backend in web (#1009)

CHEYNEca1e486e7b60
diff --git a/web/app/login/page.js b/web/app/login/page.js
index e285605874..e86321b278 100644
--- a/web/app/login/page.js
+++ b/web/app/login/page.js
@@ -17,8 +17,7 @@ import { yupResolver } from '@hookform/resolvers/yup'
import { useAuth } from '@/lib/provider/session'
import { useLocalStorage } from 'react-use'
-import { useAppDispatch, useAppSelector } from '@/lib/hooks/useStore'
-import { getAuthConfigs } from '@/lib/store/auth'
+import { useAppSelector } from '@/lib/hooks/useStore'
const defaultValues = {
grant_type: 'client_credentials',
@@ -41,7 +40,6 @@ const LoginPage = () => {
const [version] = useLocalStorage('version')
- const dispatch = useAppDispatch()
const authStore = useAppSelector(state => state.auth)
const {
@@ -59,11 +57,13 @@ const LoginPage = () => {
}
useEffect(() => {
- dispatch(getAuthConfigs())
-
- const check = version && pathname === '/login'
- if (check) {
+ if (authStore.authType === 'simple') {
router.push('/')
+ } else {
+ const check = version && pathname === '/login'
+ if (check) {
+ router.push('/')
+ }
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
diff --git a/web/lib/provider/session.js b/web/lib/provider/session.js
index d909e98189..d71ab87c76 100644
--- a/web/lib/provider/session.js
+++ b/web/lib/provider/session.js
@@ -10,14 +10,15 @@ import { createContext, useEffect, useState, useContext } from 'react'
import { useRouter } from 'next/navigation'
import { useAppDispatch, useAppSelector } from '@/lib/hooks/useStore'
-import { setVersion as setStoreVersion } from '@/lib/store/sys'
+import { initialVersion, setVersion as setStoreVersion } from '@/lib/store/sys'
import { useLocalStorage } from 'react-use'
import { getVersionApi } from '@/lib/api/version'
import { loginApi } from '@/lib/api/auth'
-import { isProdEnv } from '../utils'
+import { isProdEnv, to } from '../utils'
+import { getAuthConfigs } from '../store/auth'
const devOauthUrl = process.env.NEXT_PUBLIC_OAUTH_PATH
@@ -42,11 +43,7 @@ const AuthProvider = ({ children }) => {
const dispatch = useAppDispatch()
const handleLogin = async params => {
- let oauthUrl = ''
-
- if (typeof window !== 'undefined') {
- oauthUrl = window.localStorage.getItem('oauthUrl')
- }
+ let oauthUrl = authStore.oauthUrl
try {
const response = await loginApi(isProdEnv ? oauthUrl : devOauthUrl, params)
@@ -84,31 +81,37 @@ const AuthProvider = ({ children }) => {
useEffect(() => {
const initAuth = async () => {
- if (token) {
- setLoading(true)
-
- getVersionApi()
- .then(res => {
- setLoading(false)
- const { version } = res.data
- console.log(
- `Gravitino Version: %c${version.version}`,
- `color: white; background-color: #6062E0; padding: 2px; border-radius: 4px;`
- )
- setVersion(version)
- dispatch(setStoreVersion(version.version))
- })
- .catch(() => {
- localStorage.removeItem('version')
- localStorage.removeItem('accessToken')
- setLoading(false)
-
- // setVersion(null)
- router.replace('/login')
- })
+ const [authConfigsErr, resAuthConfigs] = await to(dispatch(getAuthConfigs()))
+ const { authType, oauthUrl } = resAuthConfigs.payload
+
+ if (authType !== 'simple') {
+ if (token) {
+ setLoading(true)
+
+ getVersionApi()
+ .then(res => {
+ setLoading(false)
+ const { version } = res.data
+ console.log(
+ `Gravitino Version: %c${version.version}`,
+ `color: white; background-color: #6062E0; padding: 2px; border-radius: 4px;`
+ )
+ setVersion(version)
+ dispatch(setStoreVersion(version.version))
+ })
+ .catch(() => {
+ localStorage.removeItem('version')
+ localStorage.removeItem('accessToken')
+ setLoading(false)
+
+ router.replace('/login')
+ })
+ } else {
+ setLoading(false)
+ router.replace('/login')
+ }
} else {
- setLoading(false)
- router.replace('/login')
+ dispatch(initialVersion())
}
}
diff --git a/web/lib/store/auth/index.js b/web/lib/store/auth/index.js
index 1d84d2f9be..132128fdbf 100644
--- a/web/lib/store/auth/index.js
+++ b/web/lib/store/auth/index.js
@@ -11,25 +11,30 @@ import { getAuthConfigsApi } from '@/lib/api/auth'
export const getAuthConfigs = createAsyncThunk('sys/getAuthConfigs', async () => {
let oauthUrl = null
+ let authType = null
const [err, res] = await to(getAuthConfigsApi())
if (!err && res) {
oauthUrl = `${res.data['gravitino.authenticator.oauth.serverUri']}${res.data['gravitino.authenticator.oauth.tokenPath']}`
- if (typeof window !== 'undefined') {
- window.localStorage.setItem('oauthUrl', oauthUrl)
- }
+ authType = res.data['gravitino.authenticator']
}
- return oauthUrl
+ return { oauthUrl, authType }
})
export const authSlice = createSlice({
name: 'auth',
initialState: {
- oauthUrl: null
+ oauthUrl: null,
+ authType: null
},
reducers: {},
- extraReducers: builder => {}
+ extraReducers: builder => {
+ builder.addCase(getAuthConfigs.fulfilled, (state, action) => {
+ state.oauthUrl = action.payload.oauthUrl
+ state.authType = action.payload.authType
+ })
+ }
})
export default authSlice.reducer
diff --git a/web/lib/store/sys/index.js b/web/lib/store/sys/index.js
index 798ce069d1..87fc88556a 100644
--- a/web/lib/store/sys/index.js
+++ b/web/lib/store/sys/index.js
@@ -15,7 +15,7 @@ export const initialVersion = createAsyncThunk('sys/fetchVersion', async (params
if (!err && res) {
version = res.data.version
- sessionStorage.setItem('version', version.version)
+ typeof window !== 'undefined' && window.localStorage.setItem('version', JSON.stringify(version))
console.log(
`Gravitino Version: %c${version.version}`,