From d3d08f9892475f3fca32d53296642914b11615b1 Mon Sep 17 00:00:00 2001 From: ebube ojinta Date: Fri, 14 Apr 2023 13:52:47 -0700 Subject: [PATCH] fleet login implementation --- src/app/modules/auth/components/Login.tsx | 8 ++++++-- src/app/modules/auth/core/Auth.tsx | 6 +++--- src/app/modules/auth/core/AuthHelpers.ts | 4 ++-- src/app/modules/auth/core/_models.ts | 2 +- src/app/modules/auth/core/_requests.ts | 8 ++++---- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/app/modules/auth/components/Login.tsx b/src/app/modules/auth/components/Login.tsx index a4b8b56..8716df2 100644 --- a/src/app/modules/auth/components/Login.tsx +++ b/src/app/modules/auth/components/Login.tsx @@ -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) diff --git a/src/app/modules/auth/core/Auth.tsx b/src/app/modules/auth/core/Auth.tsx index b72f3c0..1c2f052 100644 --- a/src/app/modules/auth/core/Auth.tsx +++ b/src/app/modules/auth/core/Auth.tsx @@ -77,7 +77,7 @@ const AuthInit: FC = ({children}) => { } catch (error) { console.error(error) if (!didRequest.current) { - logout() + // logout() } } finally { setShowSplashScreen(false) @@ -86,8 +86,8 @@ const AuthInit: FC = ({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) diff --git a/src/app/modules/auth/core/AuthHelpers.ts b/src/app/modules/auth/core/AuthHelpers.ts index f2e96da..c2559c0 100644 --- a/src/app/modules/auth/core/AuthHelpers.ts +++ b/src/app/modules/auth/core/AuthHelpers.ts @@ -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 diff --git a/src/app/modules/auth/core/_models.ts b/src/app/modules/auth/core/_models.ts index 81576cc..b1815c2 100644 --- a/src/app/modules/auth/core/_models.ts +++ b/src/app/modules/auth/core/_models.ts @@ -1,5 +1,5 @@ export interface AuthModel { - api_token: string + session_token: string refreshToken?: string } diff --git a/src/app/modules/auth/core/_requests.ts b/src/app/modules/auth/core/_requests.ts index aaef532..8e72581 100644 --- a/src/app/modules/auth/core/_requests.ts +++ b/src/app/modules/auth/core/_requests.ts @@ -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(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(GET_USER_BY_ACCESSTOKEN_URL, { - api_token: token, + return axios.get(GET_USER_BY_ACCESSTOKEN_URL, { + session_token: token, }) }