More changes at the login #6

Merged
ameye merged 5 commits from login-error-msg into master 2023-04-16 21:02:07 +00:00
6 changed files with 22 additions and 15 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ PUBLIC_URL=http://localhost:3000/
PORT=3000
PRIMARY_APP_API_URL=https://preview.keenthemes.com/metronic8/laravel/api
REACT_APP_BASE_LAYOUT_CONFIG_KEY='metronic-react-demo1-8150'
REACT_APP_API_URL=https://float-gat.dev.chiefsoft.net/en/userweb/api/v1
REACT_APP_API_URL=https://float-gat.dev.chiefsoft.net/en/fleetweb/api/v1/
REACT_APP_VERSION=v8.1.5
REACT_APP_THEME_NAME=WrenchBoard
REACT_APP_THEME_DEMO=dashboard
+6 -2
View File
@@ -8,6 +8,7 @@ import {getUserByToken, login} from '../core/_requests'
import {toAbsoluteUrl} from '../../../../_metronic/helpers'
import {useAuth} from '../core/Auth'
// import Logo from '../../../../../public/media/logos/favicon.ico'
import {useNavigate} from 'react-router-dom'
const loginSchema = Yup.object().shape({
email: Yup.string()
@@ -33,6 +34,7 @@ const initialValues = {
*/
export function Login() {
const navigate = useNavigate()
const [loading, setLoading] = useState(false)
const {saveAuth, setCurrentUser} = useAuth()
@@ -44,8 +46,10 @@ export function Login() {
try {
const {data: auth} = await login(values.email, values.password)
saveAuth(auth)
const {data: user} = await getUserByToken(auth.api_token)
setCurrentUser(user)
// const {data: user} = await getUserByToken(auth.api_token)
// setCurrentUser(user)
setCurrentUser(auth.profile)
navigate('/dashboard')
} catch (error) {
console.error(error)
saveAuth(undefined)
+8 -5
View File
@@ -69,9 +69,12 @@ const AuthInit: FC<WithChildren> = ({children}) => {
const requestUser = async (apiToken: string) => {
try {
if (!didRequest.current) {
const {data} = await getUserByToken(apiToken)
if (data) {
setCurrentUser(data)
// const {data} = await getUserByToken(apiToken)
// if (data) {
// setCurrentUser(data)
// }
if(localStorage.getItem('kt-auth-react-v')){
setCurrentUser(auth?.profile)
}
}
} catch (error) {
@@ -86,8 +89,8 @@ const AuthInit: FC<WithChildren> = ({children}) => {
return () => (didRequest.current = true)
}
if (auth && auth.api_token) {
requestUser(auth.api_token)
if (auth && auth.session_token) {
requestUser(auth.session_token)
} else {
logout()
setShowSplashScreen(false)
+2 -2
View File
@@ -52,8 +52,8 @@ export function setupAxios(axios: any) {
axios.interceptors.request.use(
(config: {headers: {Authorization: string}}) => {
const auth = getAuth()
if (auth && auth.api_token) {
config.headers.Authorization = `Bearer ${auth.api_token}`
if (auth && auth.session_token) {
config.headers.Authorization = `Bearer ${auth.session_token}`
}
return config
+1 -1
View File
@@ -1,5 +1,5 @@
export interface AuthModel {
api_token: string
session_token: string
refreshToken?: string
}
+4 -4
View File
@@ -11,8 +11,8 @@ export const REQUEST_PASSWORD_URL = `${API_URL}/forgot_password`
// Server should return AuthModel
export function login(email: string, password: string) {
return axios.post<AuthModel>(LOGIN_URL, {
email,
password,
username: email,
password: password,
})
}
@@ -41,7 +41,7 @@ export function requestPassword(email: string) {
}
export function getUserByToken(token: string) {
return axios.post<UserModel>(GET_USER_BY_ACCESSTOKEN_URL, {
api_token: token,
return axios.get<UserModel>(GET_USER_BY_ACCESSTOKEN_URL, {
session_token: token,
})
}