From f62fc0e58418aa27b64c9538c6bf8404c6989b55 Mon Sep 17 00:00:00 2001 From: SergioLangaritaBenitez Date: Mon, 21 Oct 2024 10:04:58 +0200 Subject: [PATCH] update login --- public/callback.html | 1 + src/api/info/getInfoApi.ts | 29 ++++++++++++++++++----------- src/contexts/AuthContext.tsx | 8 +++----- src/pages/Login/index.tsx | 2 +- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/public/callback.html b/public/callback.html index d0e0288..488473c 100644 --- a/public/callback.html +++ b/public/callback.html @@ -110,6 +110,7 @@ "user":{ 'access_token': tokens['access_token'], info:users } , "token": tokens['access_token'], "endpoint": localStorage.getItem('api')}) + localStorage.setItem("authData",authData); var url_redirect = window.location.origin + "/#/ui/services" window.location.href = url_redirect diff --git a/src/api/info/getInfoApi.ts b/src/api/info/getInfoApi.ts index edbd60a..2332395 100644 --- a/src/api/info/getInfoApi.ts +++ b/src/api/info/getInfoApi.ts @@ -4,24 +4,20 @@ type GetInfoProps = { endpoint: string; username: string; password: string; - token: string; + token: string | undefined; }; export async function getInfoApi({ endpoint, username, - password, token, + password, }: GetInfoProps) { - let config - if (token!=""){ - config = { - baseURL: endpoint, - headers: { - 'Authorization': 'Bearer ' + token - } - } - }else{ + console.log(username) + console.log(password) + console.log(token) + let config={} + if ( token !== undefined) { config = { baseURL: endpoint, auth: { @@ -29,8 +25,19 @@ export async function getInfoApi({ password, }, } + }else{ + config = { + baseURL: endpoint, + headers: { Authorization: "Bearer "+token}, + auth: undefined, + } } + console.log(config) + const response = await axios.get("/system/info", config); + console.log(response) return response.data; } + + diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index db03601..1072ea9 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -14,7 +14,7 @@ export type AuthData = { user: string; password: string; endpoint: string; - token: string; + token: string | undefined; authenticated?: boolean; }; @@ -23,7 +23,6 @@ export const AuthContext = createContext({ user: "", password: "", endpoint: "", - token: "", authenticated: false, } as AuthData, // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -39,7 +38,6 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => { user: "", password: "", endpoint: "", - token: "", authenticated: false, } as AuthData; } @@ -84,14 +82,14 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => { endpoint: authData.endpoint, username: authData.user, password: authData.password, - token: authData.token, + token: (authData?.token) ? (authData.token) : (undefined), }); } catch (error) { setAuthData({ user: "", password: "", endpoint: "", - token: "", + token:"", authenticated: false, }); } diff --git a/src/pages/Login/index.tsx b/src/pages/Login/index.tsx index 6ec4cae..9b217d6 100644 --- a/src/pages/Login/index.tsx +++ b/src/pages/Login/index.tsx @@ -39,7 +39,7 @@ function Login() { authenticated: true, user: username, password, - token, + token: undefined, endpoint, }); } catch (error) {