Loading...
|| ( -No teams found.
+ > + ) ||Loading...
|| ( +No teams found.
) } } diff --git a/src/pages/Login/Login.tsx b/src/pages/Login/Login.tsx index 9e6ef25d..7346ecc1 100644 --- a/src/pages/Login/Login.tsx +++ b/src/pages/Login/Login.tsx @@ -1,11 +1,11 @@ -import styles from './login.module.scss'; +import styles from './login.module.scss' import { Title, Input, Link } from "@statisticsnorway/ssb-component-library"; import { useEffect, useState } from "react"; import { verifyKeycloakToken } from "../../api/VerifyKeycloakToken"; import { useLocation, useNavigate } from 'react-router-dom'; - -const jwtRegex = /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/; +import { jwtRegex } from "../../utils/regex"; +import { getUserProfile, getUserProfileFallback } from "../../api/UserApi"; export default function Login() { const [error, setError] = useState(false); @@ -28,31 +28,36 @@ export default function Login() { }, [navigate]); useEffect(() => { - const validateAccessToken = (accessToken: string) => { + const validateToken = async (accessToken: string) => { // Check if the token matches the JWT pattern - if (!jwtRegex.test(accessToken)) { - return Promise.resolve(false); - } + if (!jwtRegex.test(accessToken)) return false; // Check if the token is invalid - return verifyKeycloakToken(accessToken).then(isValid => { - if (!isValid) { - return false; - } - setValue(accessToken); - return true; - }); + const isValid = await verifyKeycloakToken(accessToken); + if (!isValid) return false; + setValue(accessToken); + + try { + const userProfile = await getUserProfile(accessToken); + localStorage.setItem("userProfile", JSON.stringify(userProfile)); + } catch (error) { + console.error("Could not fetch user profile, using fallback", error); + const userProfile = getUserProfileFallback(accessToken); + localStorage.setItem("userProfile", JSON.stringify(userProfile)); + } + + return true; }; if (!value) { setError(false); } else { - validateAccessToken(value).then(isValidToken => { - if (isValidToken) { - localStorage.setItem('access_token', value) + validateToken(value).then(isValidAccessToken => { + if (isValidAccessToken) { + localStorage.setItem("access_token", value); navigate(from); } - setError(!isValidToken); + setError(!isValidAccessToken); }); } }, [value, from]); @@ -69,11 +74,10 @@ export default function Login() { ) diff --git a/src/pages/Users.tsx b/src/pages/Users.tsx index fa0733a7..5c911af2 100644 --- a/src/pages/Users.tsx +++ b/src/pages/Users.tsx @@ -1,10 +1,10 @@ import PageLayout from "../components/PageLayout/PageLayout" -import { getAllTeams, TeamApiResponse } from "../api/teamApi" +import { getAllTeams, Team } from "../api/TeamApi" import { useEffect, useState } from "react" import { Dialog } from "@statisticsnorway/ssb-component-library" export default function Users() { - const [teams, setTeams] = useState{team.uniformName} | {team.displayName} | diff --git a/src/utils/regex.ts b/src/utils/regex.ts new file mode 100644 index 00000000..0be8b190 --- /dev/null +++ b/src/utils/regex.ts @@ -0,0 +1,2 @@ + +export const jwtRegex = /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/; \ No newline at end of file