Skip to content

Commit 3dcadfb

Browse files
authored
Merge pull request #5583 from PrestigePvP/tre/eng-4621-add-recent-login-awareness
feat(eng-4621): add recent login awareness to login page
2 parents 16314ac + 9fe7902 commit 3dcadfb

File tree

7 files changed

+198
-155
lines changed

7 files changed

+198
-155
lines changed

frontend/src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { useDebounce } from "./useDebounce";
22
export * from "./useGetProjectTypeFromRoute";
3+
export { useLastLogin } from "./useLastLogin";
34
export { useLocalStorageState } from "./useLocalStorageState";
45
export { usePagination } from "./usePagination";
56
export { usePersistentState } from "./usePersistentState";

frontend/src/hooks/useLastLogin.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useCallback } from "react";
2+
3+
import { LoginMethod } from "@app/hooks/api/admin/types";
4+
5+
import { useLocalStorageState } from "./useLocalStorageState";
6+
7+
const LAST_LOGIN_KEY = "infisical.lastLogin";
8+
9+
export type LastLogin = {
10+
method: LoginMethod;
11+
orgSlug?: string;
12+
timestamp: number;
13+
};
14+
15+
export const useLastLogin = () => {
16+
const [lastLogin, setLastLogin] = useLocalStorageState<LastLogin | null>(LAST_LOGIN_KEY, null);
17+
18+
const saveLastLogin = useCallback(
19+
(entry: Omit<LastLogin, "timestamp">) => {
20+
setLastLogin({ ...entry, timestamp: Date.now() });
21+
},
22+
[setLastLogin]
23+
);
24+
25+
const clearLastLogin = useCallback(() => {
26+
setLastLogin(null);
27+
}, [setLastLogin]);
28+
29+
return { lastLogin, saveLastLogin, clearLastLogin };
30+
};

frontend/src/pages/auth/LoginLdapPage/LoginLDAPPage.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ import { Link, useNavigate } from "@tanstack/react-router";
66
import { createNotification } from "@app/components/notifications";
77
import { Button, Input } from "@app/components/v2";
88
import { useServerConfig } from "@app/context";
9+
import { LoginMethod } from "@app/hooks/api/admin/types";
910
import { loginLDAPRedirect } from "@app/hooks/api/auth/queries";
11+
import { useLastLogin } from "@app/hooks/useLastLogin";
1012

1113
export const LoginLdapPage = () => {
1214
const { t } = useTranslation();
1315
const navigate = useNavigate();
1416
const { config } = useServerConfig();
17+
const { lastLogin, saveLastLogin } = useLastLogin();
1518
const queryParams = new URLSearchParams(window.location.search);
1619
const passedOrgSlug = queryParams.get("organizationSlug");
1720
const passedUsername = queryParams.get("username");
1821

22+
const lastLoginSlug =
23+
lastLogin?.method === LoginMethod.LDAP && lastLogin.orgSlug ? lastLogin.orgSlug : "";
24+
1925
const [organizationSlug, setOrganizationSlug] = useState(
20-
config.defaultAuthOrgSlug || passedOrgSlug || ""
26+
config.defaultAuthOrgSlug || passedOrgSlug || lastLoginSlug
2127
);
2228
const [username, setUsername] = useState(passedUsername || "");
2329
const [password, setPassword] = useState("");
@@ -40,6 +46,8 @@ export const LoginLdapPage = () => {
4046
return;
4147
}
4248

49+
saveLastLogin({ method: LoginMethod.LDAP, orgSlug: organizationSlug });
50+
4351
createNotification({
4452
text: "Successfully logged in",
4553
type: "success"

0 commit comments

Comments
 (0)