Skip to content

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

[#999] feat(UI): Add fetch OAuth configurations from API in the web UI (#1000)

CHEYNE1e4a5bb2c003
diff --git a/web/.env b/web/.env
index f488de43c5..3a837392a8 100644
--- a/web/.env
+++ b/web/.env
@@ -4,5 +4,7 @@
#
OUTPUT_MODE=standalone
-NEXT_PUBLIC_API_URL=http://localhost:8090/api
-NEXT_PUBLIC_OAUTH_URL=http://localhost:9010/oauth2/token
+# Development ENV
+NEXT_PUBLIC_API_URL=http://localhost:8090
+NEXT_PUBLIC_OAUTH_URI=http://localhost:9010
+NEXT_PUBLIC_OAUTH_PATH=/oauth2/token
diff --git a/web/.env.static b/web/.env.static
index 7bd62d79ae..d15fb523d6 100644
--- a/web/.env.static
+++ b/web/.env.static
@@ -4,5 +4,7 @@
#
OUTPUT_MODE=export
-NEXT_PUBLIC_API_URL=http://localhost:8090/api
-NEXT_PUBLIC_OAUTH_URL=http://localhost:9010/oauth2/token
+# Development ENV
+NEXT_PUBLIC_API_URL=http://localhost:8090
+NEXT_PUBLIC_OAUTH_URI=http://localhost:9010
+NEXT_PUBLIC_OAUTH_PATH=/oauth2/token
diff --git a/web/app/login/page.js b/web/app/login/page.js
index f9ebae2a15..e285605874 100644
--- a/web/app/login/page.js
+++ b/web/app/login/page.js
@@ -17,6 +17,9 @@ 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'
+
const defaultValues = {
grant_type: 'client_credentials',
client_id: '',
@@ -38,6 +41,9 @@ const LoginPage = () => {
const [version] = useLocalStorage('version')
+ const dispatch = useAppDispatch()
+ const authStore = useAppSelector(state => state.auth)
+
const {
control,
handleSubmit,
@@ -53,6 +59,8 @@ const LoginPage = () => {
}
useEffect(() => {
+ dispatch(getAuthConfigs())
+
const check = version && pathname === '/login'
if (check) {
router.push('/')
diff --git a/web/lib/api/auth/index.js b/web/lib/api/auth/index.js
index 9299fbe1c5..1d8c1daa1d 100644
--- a/web/lib/api/auth/index.js
+++ b/web/lib/api/auth/index.js
@@ -5,9 +5,16 @@
import axios from 'axios'
-export const loginApi = params => {
+export const getAuthConfigsApi = () => {
return axios({
- url: '/oauth2/token',
+ url: `/configs`,
+ method: 'get'
+ })
+}
+
+export const loginApi = (url, params) => {
+ return axios({
+ url,
method: 'post',
params
})
diff --git a/web/lib/layout/VersionView.js b/web/lib/layout/VersionView.js
index e0c36ffc00..08e48fb2b3 100644
--- a/web/lib/layout/VersionView.js
+++ b/web/lib/layout/VersionView.js
@@ -9,7 +9,7 @@ import { Typography } from '@mui/material'
import { useAppSelector } from '@/lib/hooks/useStore'
const VersionView = () => {
- const store = useAppSelector(state => state.version)
+ const store = useAppSelector(state => state.sys)
return <Typography variant='subtitle2'>{store.version}</Typography>
}
diff --git a/web/lib/provider/session.js b/web/lib/provider/session.js
index d62b95caa0..d909e98189 100644
--- a/web/lib/provider/session.js
+++ b/web/lib/provider/session.js
@@ -9,14 +9,18 @@ import { createContext, useEffect, useState, useContext } from 'react'
import { useRouter } from 'next/navigation'
-import { useAppDispatch } from '@/lib/hooks/useStore'
-import { setVersion as setStoreVersion } from '@/lib/store/version'
+import { useAppDispatch, useAppSelector } from '@/lib/hooks/useStore'
+import { 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'
+
+const devOauthUrl = process.env.NEXT_PUBLIC_OAUTH_PATH
+
const authProvider = {
version: null,
loading: true,
@@ -34,11 +38,18 @@ const AuthProvider = ({ children }) => {
const [loading, setLoading] = useState(authProvider.loading)
const [token, setToken] = useLocalStorage('accessToken', null, { raw: true })
const [version, setVersion] = useLocalStorage('version', authProvider.version, { raw: false })
+ const authStore = useAppSelector(state => state.auth)
const dispatch = useAppDispatch()
const handleLogin = async params => {
+ let oauthUrl = ''
+
+ if (typeof window !== 'undefined') {
+ oauthUrl = window.localStorage.getItem('oauthUrl')
+ }
+
try {
- const response = await loginApi(params)
+ const response = await loginApi(isProdEnv ? oauthUrl : devOauthUrl, params)
const { access_token } = response.data
diff --git a/web/lib/store/auth/index.js b/web/lib/store/auth/index.js
new file mode 100644
index 0000000000..1d84d2f9be
--- /dev/null
+++ b/web/lib/store/auth/index.js
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+
+import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
+
+import { to } from '@/lib/utils'
+
+import { getAuthConfigsApi } from '@/lib/api/auth'
+
+export const getAuthConfigs = createAsyncThunk('sys/getAuthConfigs', async () => {
+ let oauthUrl = 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)
+ }
+ }
+
+ return oauthUrl
+})
+
+export const authSlice = createSlice({
+ name: 'auth',
+ initialState: {
+ oauthUrl: null
+ },
+ reducers: {},
+ extraReducers: builder => {}
+})
+
+export default authSlice.reducer
diff --git a/web/lib/store/index.js b/web/lib/store/index.js
index aafbd47af7..c57623db1b 100644
--- a/web/lib/store/index.js
+++ b/web/lib/store/index.js
@@ -9,12 +9,14 @@ import { configureStore } from '@reduxjs/toolkit'
import { isDevEnv } from '@/lib/utils'
-import version from './version'
+import sys from './sys'
+import auth from './auth'
import metalakes from './metalakes'
export const store = configureStore({
reducer: {
- version,
+ sys,
+ auth,
metalakes
},
devTools: true,
diff --git a/web/lib/store/version/index.js b/web/lib/store/sys/index.js
similarity index 67%
rename from web/lib/store/version/index.js
rename to web/lib/store/sys/index.js
index cd82e757b8..798ce069d1 100644
--- a/web/lib/store/version/index.js
+++ b/web/lib/store/sys/index.js
@@ -5,19 +5,23 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
+import { to } from '@/lib/utils'
+
import { getVersionApi } from '@/lib/api/version'
export const initialVersion = createAsyncThunk('sys/fetchVersion', async (params, { getState }) => {
- const response = await getVersionApi()
-
- const { version } = response.data
+ let version = null
+ const [err, res] = await to(getVersionApi())
- sessionStorage.setItem('version', version.version)
+ if (!err && res) {
+ version = res.data.version
+ sessionStorage.setItem('version', version.version)
- console.log(
- `Gravitino Version: %c${version.version}`,
- `color: white; background-color: #6062E0; padding: 2px; border-radius: 4px;`
- )
+ console.log(
+ `Gravitino Version: %c${version.version}`,
+ `color: white; background-color: #6062E0; padding: 2px; border-radius: 4px;`
+ )
+ }
return version
})
diff --git a/web/lib/utils/index.js b/web/lib/utils/index.js
index a2b716dd8e..e2544f30c6 100644
--- a/web/lib/utils/index.js
+++ b/web/lib/utils/index.js
@@ -7,6 +7,20 @@ export const isDevEnv = process.env.NODE_ENV === 'development'
export const isProdEnv = process.env.NODE_ENV === 'production'
+export const to = (promise, errExt) => {
+ return promise
+ .then(data => [null, data])
+ .catch(err => {
+ if (errExt) {
+ const error = Object.assign({}, err, errExt)
+
+ return [error, undefined]
+ }
+
+ return [err, undefined]
+ })
+}
+
export const genUpdates = (originalData, newData) => {
const updates = []
diff --git a/web/next.config.js b/web/next.config.js
index 82abdaef70..e89286de77 100644
--- a/web/next.config.js
+++ b/web/next.config.js
@@ -6,23 +6,29 @@
const isProdEnv = process.env.NODE_ENV === 'production'
const apiUrl = process.env.NEXT_PUBLIC_API_URL
-const oauthUrl = process.env.NEXT_PUBLIC_OAUTH_URL
+const oauthUri = process.env.NEXT_PUBLIC_OAUTH_URI
+const oauthPath = process.env.NEXT_PUBLIC_OAUTH_PATH
/** @type {import('next').NextConfig} */
const nextConfig = {
...(isProdEnv
? {}
: {
+ // ** Just for development
async rewrites() {
return {
fallback: [
{
source: '/api/:path*',
- destination: `${apiUrl}/:path*`
+ destination: `${apiUrl}/api/:path*`
},
{
- source: '/oauth2/token',
- destination: `${oauthUrl}`
+ source: '/configs',
+ destination: `${apiUrl}/configs`
+ },
+ {
+ source: `${oauthPath}`,
+ destination: `${oauthUri}${oauthPath}`
}
]
}