Skip to content

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

[#2074] fix(web): fix login input typing error (#2088)

CHEYNE559046aabb8c
diff --git a/web/app/ui/login/page.js b/web/app/ui/login/page.js
index 6c8defc0d9..fcf9c74776 100644
--- a/web/app/ui/login/page.js
+++ b/web/app/ui/login/page.js
@@ -5,6 +5,7 @@
'use client'
+import { useRouter } from 'next/navigation'
import Image from 'next/image'
import { Box, Card, Grid, Button, CardContent, Typography, TextField, FormControl, FormHelperText } from '@mui/material'
@@ -12,7 +13,9 @@ import { Box, Card, Grid, Button, CardContent, Typography, TextField, FormContro
import * as yup from 'yup'
import { useForm, Controller } from 'react-hook-form'
import { yupResolver } from '@hookform/resolvers/yup'
-import { useAuth } from '@/lib/provider/session'
+
+import { useAppDispatch } from '@/lib/hooks/useStore'
+import { loginAction } from '@/lib/store/auth'
const defaultValues = {
grant_type: 'client_credentials',
@@ -29,20 +32,27 @@ const schema = yup.object().shape({
})
const LoginPage = () => {
- const auth = useAuth()
+ const router = useRouter()
+ const dispatch = useAppDispatch()
const {
control,
handleSubmit,
+ reset,
formState: { errors }
} = useForm({
- defaultValues,
+ defaultValues: Object.assign({}, defaultValues),
mode: 'onChange',
resolver: yupResolver(schema)
})
- const onSubmit = data => {
- auth.login(data)
+ const onSubmit = async data => {
+ dispatch(loginAction({ params: data, router }))
+ reset()
+ }
+
+ const onError = errors => {
+ console.error('fields error', errors)
}
return (
@@ -57,7 +67,7 @@ const LoginPage = () => {
</Typography>
</Box>
- <form autoComplete='off' onSubmit={handleSubmit(onSubmit)}>
+ <form autoComplete='off' onSubmit={handleSubmit(onSubmit, onError)}>
<Grid item xs={12} sx={{ mt: 4 }}>
<FormControl fullWidth>
<Controller
diff --git a/web/lib/store/auth/index.js b/web/lib/store/auth/index.js
index 03c6d4d75a..34e6cbbcdf 100644
--- a/web/lib/store/auth/index.js
+++ b/web/lib/store/auth/index.js
@@ -4,13 +4,13 @@
*/
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
+import toast from 'react-hot-toast'
-import { to, isProdEnv, loggerVersion } from '@/lib/utils'
+import { to, isProdEnv } from '@/lib/utils'
import { getAuthConfigsApi, loginApi } from '@/lib/api/auth'
-import { getVersionApi } from '@/lib/api/version'
-import { setVersion as setStoreVersion } from '@/lib/store/sys'
+import { initialVersion } from '@/lib/store/sys'
const devOauthUrl = process.env.NEXT_PUBLIC_OAUTH_PATH
@@ -46,42 +46,30 @@ export const refreshToken = createAsyncThunk('auth/refreshToken', async (data, {
})
export const loginAction = createAsyncThunk('auth/loginAction', async ({ params, router }, { getState, dispatch }) => {
- const preLogin = new Promise(async resolve => {
- dispatch(setAuthParams(params))
- localStorage.setItem('authParams', JSON.stringify(params))
+ dispatch(setAuthParams(params))
+ localStorage.setItem('authParams', JSON.stringify(params))
- const url = getState().auth.oauthUrl
+ const url = getState().auth.oauthUrl
- const [err, res] = await to(loginApi(url, params))
-
- if (err || !res) {
- throw new Error(err)
- }
-
- const { access_token, expires_in } = res.data
+ const [err, res] = await to(loginApi(url, params))
- localStorage.setItem('accessToken', access_token)
- localStorage.setItem('expiredIn', expires_in)
- dispatch(setAuthToken(access_token))
- dispatch(setExpiredIn(expires_in))
+ if (err || !res) {
+ toast.error(err.response?.data?.err || err.message, { id: `global_error_message_status_${err.response?.status}` })
+ throw new Error(err)
+ }
- resolve(access_token)
- })
+ const { access_token, expires_in } = res?.data
- preLogin.then(async token => {
- if (!token) {
- throw new Error('Token not found')
- }
+ localStorage.setItem('accessToken', access_token)
+ localStorage.setItem('expiredIn', expires_in)
+ dispatch(setAuthToken(access_token))
+ dispatch(setExpiredIn(expires_in))
- const [verErr, resVer] = await to(getVersionApi())
- const { version } = resVer
+ await dispatch(initialVersion())
- loggerVersion(version.version)
- localStorage.setItem('version', JSON.stringify(version))
- dispatch(setStoreVersion(version.version))
+ router.push('/')
- router.replace('/')
- })
+ return { token: access_token, expired: expires_in }
})
export const setIntervalId = createAsyncThunk('auth/setIntervalId', async (expiredIn, { dispatch }) => {