Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat(eng-4621): add recent login awareness to login page
Tre Dubrava6bbf3c50a57e
diff --git a/frontend/src/hooks/index.ts b/frontend/src/hooks/index.tsindex b224c1b6d3..764c3b2b9d 100644--- a/frontend/src/hooks/index.ts+++ b/frontend/src/hooks/index.ts@@ -1,5 +1,6 @@export { useDebounce } from "./useDebounce";export * from "./useGetProjectTypeFromRoute";+export { useLastLogin } from "./useLastLogin";export { useLocalStorageState } from "./useLocalStorageState";export { usePagination } from "./usePagination";export { usePersistentState } from "./usePersistentState";diff --git a/frontend/src/hooks/useLastLogin.ts b/frontend/src/hooks/useLastLogin.tsnew file mode 100644index 0000000000..e15586cae9--- /dev/null+++ b/frontend/src/hooks/useLastLogin.ts@@ -0,0 +1,30 @@+import { useCallback } from "react";++import { LoginMethod } from "@app/hooks/api/admin/types";++import { useLocalStorageState } from "./useLocalStorageState";++const LAST_LOGIN_KEY = "infisical.lastLogin";++export type LastLogin = {+ method: LoginMethod;+ orgSlug?: string;+ timestamp: number;+};++export const useLastLogin = () => {+ const [lastLogin, setLastLogin] = useLocalStorageState<LastLogin | null>(LAST_LOGIN_KEY, null);++ const saveLastLogin = useCallback(+ (entry: Omit<LastLogin, "timestamp">) => {+ setLastLogin({ ...entry, timestamp: Date.now() });+ },+ [setLastLogin]+ );++ const clearLastLogin = useCallback(() => {+ setLastLogin(null);+ }, [setLastLogin]);++ return { lastLogin, saveLastLogin, clearLastLogin };+};diff --git a/frontend/src/pages/auth/LoginLdapPage/LoginLDAPPage.tsx b/frontend/src/pages/auth/LoginLdapPage/LoginLDAPPage.tsxindex 6800f52413..db36fe8623 100644--- a/frontend/src/pages/auth/LoginLdapPage/LoginLDAPPage.tsx+++ b/frontend/src/pages/auth/LoginLdapPage/LoginLDAPPage.tsx@@ -6,18 +6,24 @@ import { Link, useNavigate } from "@tanstack/react-router";import { createNotification } from "@app/components/notifications";import { Button, Input } from "@app/components/v2";import { useServerConfig } from "@app/context";+import { LoginMethod } from "@app/hooks/api/admin/types";import { loginLDAPRedirect } from "@app/hooks/api/auth/queries";+import { useLastLogin } from "@app/hooks/useLastLogin";export const LoginLdapPage = () => {const { t } = useTranslation();const navigate = useNavigate();const { config } = useServerConfig();+ const { lastLogin, saveLastLogin } = useLastLogin();const queryParams = new URLSearchParams(window.location.search);const passedOrgSlug = queryParams.get("organizationSlug");const passedUsername = queryParams.get("username");+ const lastLoginSlug =+ lastLogin?.method === LoginMethod.LDAP && lastLogin.orgSlug ? lastLogin.orgSlug : "";+const [organizationSlug, setOrganizationSlug] = useState(- config.defaultAuthOrgSlug || passedOrgSlug || ""+ config.defaultAuthOrgSlug || passedOrgSlug || lastLoginSlug);const [username, setUsername] = useState(passedUsername || "");const [password, setPassword] = useState("");@@ -40,6 +46,8 @@ export const LoginLdapPage = () => {return;}+ saveLastLogin({ method: LoginMethod.LDAP, orgSlug: organizationSlug });+createNotification({text: "Successfully logged in",type: "success"diff --git a/frontend/src/pages/auth/LoginPage/components/InitialStep/InitialStep.tsx b/frontend/src/pages/auth/LoginPage/components/InitialStep/InitialStep.tsxindex 8041e15273..d82f650c31 100644--- a/frontend/src/pages/auth/LoginPage/components/InitialStep/InitialStep.tsx+++ b/frontend/src/pages/auth/LoginPage/components/InitialStep/InitialStep.tsx@@ -1,7 +1,7 @@import { FormEvent, useEffect, useRef, useState } from "react";import { useTranslation } from "react-i18next";import { faGithub, faGitlab, faGoogle } from "@fortawesome/free-brands-svg-icons";-import { faEye, faEyeSlash, faLock } from "@fortawesome/free-solid-svg-icons";+import { faEye, faEyeSlash } from "@fortawesome/free-solid-svg-icons";import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";import HCaptcha from "@hcaptcha/react-hcaptcha";import { Link, useNavigate } from "@tanstack/react-router";@@ -11,14 +11,17 @@ import { RegionSelect } from "@app/components/navigation/RegionSelect";import { createNotification } from "@app/components/notifications";import attemptCliLogin from "@app/components/utilities/attemptCliLogin";import attemptLogin from "@app/components/utilities/attemptLogin";-import { Button, IconButton, Input, Tooltip } from "@app/components/v2";+import { Button, Input } from "@app/components/v2";import { envConfig } from "@app/config/env";import { useServerConfig } from "@app/context";import { useFetchServerStatus } from "@app/hooks/api";import { LoginMethod } from "@app/hooks/api/admin/types";import { AuthMethod } from "@app/hooks/api/users/types";+import { useLastLogin } from "@app/hooks/useLastLogin";import { useNavigateToSelectOrganization } from "../../Login.utils";+import { OrgLoginButton } from "../OrgLoginButton";+import { SocialLoginButton } from "../SocialLoginButton";type Props = {setStep: (step: number) => void;@@ -51,22 +54,21 @@ export const InitialStep = ({const { data: serverDetails } = useFetchServerStatus();const { navigateToSelectOrganization } = useNavigateToSelectOrganization();+ const { lastLogin, saveLastLogin } = useLastLogin();++ const callbackPort = queryParams.get("callback_port");const redirectToSaml = (orgSlug: string) => {- const callbackPort = queryParams.get("callback_port");const redirectUrl = `/api/v1/sso/redirect/saml2/organizations/${orgSlug}${callbackPort ? `?callback_port=${callbackPort}` : ""}`;-window.location.assign(redirectUrl);};const redirectToOidc = (orgSlug: string) => {- const callbackPort = queryParams.get("callback_port");const redirectUrl = `/api/v1/sso/oidc/login?orgSlug=${orgSlug}${callbackPort ? `&callbackPort=${callbackPort}` : ""}`;-window.location.assign(redirectUrl);};@@ -92,6 +94,24 @@ export const InitialStep = ({}};+ const handleSocialLogin = (method: LoginMethod) => {+ const searchParams = new URLSearchParams();+ if (callbackPort) {+ searchParams.append("callback_port", callbackPort);+ }+ if (isAdmin) {+ searchParams.append("is_admin_login", "true");+ }+ const qs = searchParams.toString();+ const popup = window.open(`/api/v1/sso/redirect/${method}${qs ? `?${qs}` : ""}`);+ if (popup) {+ saveLastLogin({ method });+ }+ window.close();+ };++ const isLastUsedMethod = (method: LoginMethod) => lastLogin?.method === method;+const shouldDisplayLoginMethod = (method: LoginMethod) =>isAdmin || !config.enabledLoginMethods || config.enabledLoginMethods.includes(method);@@ -103,10 +123,7 @@ export const InitialStep = ({}setIsLoading(true);- if (queryParams && queryParams.get("callback_port")) {- const callbackPort = queryParams.get("callback_port");-- // attemptCliLogin+ if (callbackPort) {const isCliLoginSuccessful = await attemptCliLogin({email: email.toLowerCase(),password,@@ -114,7 +131,7 @@ export const InitialStep = ({});if (isCliLoginSuccessful && isCliLoginSuccessful.success) {- navigateToSelectOrganization(callbackPort!);+ navigateToSelectOrganization(callbackPort);} else {setLoginError(true);createNotification({@@ -130,7 +147,7 @@ export const InitialStep = ({});if (isLoginSuccessful && isLoginSuccessful.success) {- // case: login was successful+ saveLastLogin({ method: LoginMethod.EMAIL });navigateToSelectOrganization(undefined, isAdmin);createNotification({text: "Successfully logged in",@@ -182,30 +199,10 @@ export const InitialStep = ({</h1><RegionSelect />{config.defaultAuthOrgAuthMethod === AuthMethod.SAML && (- <div className="w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6">- <Button- colorSchema="primary"- variant="outline_bg"- onClick={handleSaml}- leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />}- className="mx-0 h-10 w-full"- >- Continue with SAML- </Button>- </div>+ <OrgLoginButton label="Continue with SAML" onClick={handleSaml} />)}{config.defaultAuthOrgAuthMethod === AuthMethod.OIDC && (- <div className="mt-2 w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6">- <Button- colorSchema="primary"- variant="outline_bg"- onClick={handleOidc}- leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />}- className="mx-0 h-10 w-full"- >- Continue with OIDC- </Button>- </div>+ <OrgLoginButton label="Continue with OIDC" onClick={handleOidc} className="mt-2" />)}</form>);@@ -221,133 +218,52 @@ export const InitialStep = ({</h1><RegionSelect />{shouldDisplayLoginMethod(LoginMethod.SAML) && (- <div className="w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6">- <Button- colorSchema="primary"- variant="outline_bg"- onClick={handleSaml}- leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />}- className="mx-0 h-10 w-full"- >- Continue with SAML- </Button>- </div>+ <OrgLoginButton+ label="Continue with SAML"+ onClick={handleSaml}+ showLastUsed={isLastUsedMethod(LoginMethod.SAML)}+ />)}{shouldDisplayLoginMethod(LoginMethod.OIDC) && (- <div className="mt-2 w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6">- <Button- colorSchema="primary"- variant="outline_bg"- onClick={handleOidc}- leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />}- className="mx-0 h-10 w-full"- >- Continue with OIDC- </Button>- </div>+ <OrgLoginButton+ label="Continue with OIDC"+ onClick={handleOidc}+ className="mt-2"+ showLastUsed={isLastUsedMethod(LoginMethod.OIDC)}+ />)}{shouldDisplayLoginMethod(LoginMethod.LDAP) && (- <div className="mt-2 w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6">- <Button- colorSchema="primary"- variant="outline_bg"- onClick={() => {- navigate({ to: "/login/ldap" });- }}- leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />}- className="mx-0 h-10 w-full"- >- Continue with LDAP- </Button>- </div>+ <OrgLoginButton+ label="Continue with LDAP"+ onClick={() => navigate({ to: "/login/ldap" })}+ className="mt-2"+ showLastUsed={isLastUsedMethod(LoginMethod.LDAP)}+ />)}<div className="mt-2 flex w-1/4 min-w-[21.2rem] gap-2 md:min-w-[20.1rem] lg:w-1/6">{shouldDisplayLoginMethod(LoginMethod.GOOGLE) && (- <Tooltip position="bottom" content={t("login.continue-with-google")}>- <IconButton- ariaLabel={t("login.continue-with-google")}- colorSchema="primary"- variant="outline_bg"- onClick={() => {- const callbackPort = queryParams.get("callback_port");- const searchParams = new URLSearchParams();-- if (callbackPort) {- searchParams.append("callback_port", callbackPort);- }-- if (isAdmin) {- searchParams.append("is_admin_login", "true");- }-- const queryString = searchParams.toString();-- window.open(`/api/v1/sso/redirect/google${queryString ? `?${queryString}` : ""}`);- window.close();- }}- className="h-10 w-full bg-mineshaft-600"- >- <FontAwesomeIcon icon={faGoogle} />- </IconButton>- </Tooltip>+ <SocialLoginButton+ icon={faGoogle}+ label={t("login.continue-with-google")}+ onClick={() => handleSocialLogin(LoginMethod.GOOGLE)}+ showLastUsed={isLastUsedMethod(LoginMethod.GOOGLE)}+ />)}{shouldDisplayLoginMethod(LoginMethod.GITHUB) && (- <Tooltip position="bottom" content="Continue with GitHub">- <IconButton- ariaLabel="Login continue with GitHub"- colorSchema="primary"- variant="outline_bg"- onClick={() => {- const callbackPort = queryParams.get("callback_port");- const searchParams = new URLSearchParams();-- if (callbackPort) {- searchParams.append("callback_port", callbackPort);- }-- if (isAdmin) {- searchParams.append("is_admin_login", "true");- }-- const queryString = searchParams.toString();-- window.open(`/api/v1/sso/redirect/github${queryString ? `?${queryString}` : ""}`);- window.close();- }}- className="h-10 w-full bg-mineshaft-600"- >- <FontAwesomeIcon icon={faGithub} />- </IconButton>- </Tooltip>+ <SocialLoginButton+ icon={faGithub}+ label="Continue with GitHub"+ onClick={() => handleSocialLogin(LoginMethod.GITHUB)}+ showLastUsed={isLastUsedMethod(LoginMethod.GITHUB)}+ />)}{shouldDisplayLoginMethod(LoginMethod.GITLAB) && (- <Tooltip position="bottom" content="Continue with GitLab">- <IconButton- ariaLabel="Login continue with GitLab"- colorSchema="primary"- variant="outline_bg"- onClick={() => {- const callbackPort = queryParams.get("callback_port");- const searchParams = new URLSearchParams();-- if (callbackPort) {- searchParams.append("callback_port", callbackPort);- }-- if (isAdmin) {- searchParams.append("is_admin_login", "true");- }-- const queryString = searchParams.toString();-- window.open(`/api/v1/sso/redirect/gitlab${queryString ? `?${queryString}` : ""}`);- window.close();- }}- className="h-10 w-full bg-mineshaft-600"- >- <FontAwesomeIcon icon={faGitlab} />- </IconButton>- </Tooltip>+ <SocialLoginButton+ icon={faGitlab}+ label="Continue with GitLab"+ onClick={() => handleSocialLogin(LoginMethod.GITLAB)}+ showLastUsed={isLastUsedMethod(LoginMethod.GITLAB)}+ />)}</div>{(!config.enabledLoginMethods ||@@ -403,7 +319,7 @@ export const InitialStep = ({/></div>)}- <div className="mt-4 w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6">+ <div className="relative mt-4 w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6"><Buttondisabled={shouldShowCaptcha && captchaToken === ""}type="submit"@@ -414,9 +330,13 @@ export const InitialStep = ({variant="solid"isLoading={isLoading}>- {" "}- Continue with Email{" "}+ Continue with Email</Button>+ {isLastUsedMethod(LoginMethod.EMAIL) && (+ <span className="absolute -top-2 -right-2 rounded-full bg-primary/20 px-1.5 py-0.5 text-[10px] text-primary">+ Last used+ </span>+ )}</div></>)}diff --git a/frontend/src/pages/auth/LoginPage/components/OrgLoginButton.tsx b/frontend/src/pages/auth/LoginPage/components/OrgLoginButton.tsxnew file mode 100644index 0000000000..6b347178e9--- /dev/null+++ b/frontend/src/pages/auth/LoginPage/components/OrgLoginButton.tsx@@ -0,0 +1,36 @@+import { faLock } from "@fortawesome/free-solid-svg-icons";+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";+import { twMerge } from "tailwind-merge";++import { Button } from "@app/components/v2";++type Props = {+ label: string;+ onClick: () => void;+ className?: string;+ showLastUsed?: boolean;+};++export const OrgLoginButton = ({ label, onClick, className, showLastUsed }: Props) => (+ <div+ className={twMerge(+ "relative w-1/4 min-w-[21.2rem] rounded-md text-center md:min-w-[20.1rem] lg:w-1/6",+ className+ )}+ >+ <Button+ colorSchema="primary"+ variant="outline_bg"+ onClick={onClick}+ leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />}+ className="mx-0 h-10 w-full"+ >+ {label}+ </Button>+ {showLastUsed && (+ <span className="absolute -top-2 -right-2 rounded-full bg-primary/20 px-1.5 py-0.5 text-[10px] text-primary">+ Last used+ </span>+ )}+ </div>+);diff --git a/frontend/src/pages/auth/LoginPage/components/SSOStep/SSOStep.tsx b/frontend/src/pages/auth/LoginPage/components/SSOStep/SSOStep.tsxindex 82852edc99..bc4c1b9afb 100644--- a/frontend/src/pages/auth/LoginPage/components/SSOStep/SSOStep.tsx+++ b/frontend/src/pages/auth/LoginPage/components/SSOStep/SSOStep.tsx@@ -2,6 +2,8 @@ import { useState } from "react";import { useTranslation } from "react-i18next";import { Button, Input } from "@app/components/v2";+import { LoginMethod } from "@app/hooks/api/admin/types";+import { useLastLogin } from "@app/hooks/useLastLogin";type Props = {setStep: (step: number) => void;@@ -9,21 +11,29 @@ type Props = {};export const SSOStep = ({ setStep, type }: Props) => {- const [ssoIdentifier, setSSOIdentifier] = useState("");+ const { lastLogin, saveLastLogin } = useLastLogin();const { t } = useTranslation();+ const matchingMethod = type === "SAML" ? LoginMethod.SAML : LoginMethod.OIDC;+ const initialSlug =+ lastLogin?.method === matchingMethod && lastLogin.orgSlug ? lastLogin.orgSlug : "";++ const [ssoIdentifier, setSSOIdentifier] = useState(initialSlug);+const queryParams = new URLSearchParams(window.location.search);const handleSubmission = (e: React.FormEvent) => {e.preventDefault();const callbackPort = queryParams.get("callback_port");if (type === "SAML") {+ saveLastLogin({ method: LoginMethod.SAML, orgSlug: ssoIdentifier });window.open(`/api/v1/sso/redirect/saml2/organizations/${ssoIdentifier}${callbackPort ? `?callback_port=${callbackPort}` : ""}`);} else {+ saveLastLogin({ method: LoginMethod.OIDC, orgSlug: ssoIdentifier });window.open(`/api/v1/sso/oidc/login?orgSlug=${ssoIdentifier}${callbackPort ? `&callbackPort=${callbackPort}` : ""diff --git a/frontend/src/pages/auth/LoginPage/components/SocialLoginButton.tsx b/frontend/src/pages/auth/LoginPage/components/SocialLoginButton.tsxnew file mode 100644index 0000000000..c057e8ab9e--- /dev/null+++ b/frontend/src/pages/auth/LoginPage/components/SocialLoginButton.tsx@@ -0,0 +1,32 @@+import { IconDefinition } from "@fortawesome/free-brands-svg-icons";+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";++import { IconButton, Tooltip } from "@app/components/v2";++type Props = {+ icon: IconDefinition;+ label: string;+ onClick: () => void;+ showLastUsed?: boolean;+};++export const SocialLoginButton = ({ icon, label, onClick, showLastUsed }: Props) => (+ <Tooltip position="bottom" content={label}>+ <div className="relative w-full">+ <IconButton+ ariaLabel={label}+ colorSchema="primary"+ variant="outline_bg"+ onClick={onClick}+ className="h-10 w-full bg-mineshaft-600"+ >+ <FontAwesomeIcon icon={icon} />+ </IconButton>+ {showLastUsed && (+ <span className="absolute -top-2 -right-2 rounded-full bg-primary/20 px-1.5 py-0.5 text-[10px] text-primary">+ Last used+ </span>+ )}+ </div>+ </Tooltip>+);