Skip to content

armgit / meet

Public Imported from GitHub
0

Open source video conferencing app powered by LiveKit. Built with Django and React.

✨(frontend) try silent login every one hour

lebaudantoine234116163a9c
diff --git a/src/frontend/src/features/auth/api/fetchUser.ts b/src/frontend/src/features/auth/api/fetchUser.ts
index 512b3d02..aa7031cc 100644
--- a/src/frontend/src/features/auth/api/fetchUser.ts
+++ b/src/frontend/src/features/auth/api/fetchUser.ts
@@ -1,6 +1,7 @@
import { ApiError } from '@/api/ApiError'
import { fetchApi } from '@/api/fetchApi'
import { type ApiUser } from './ApiUser'
+import { attemptSilentLogin } from "@/features/auth";
/**
* fetch the logged-in user from the api.
@@ -16,6 +17,7 @@ export const fetchUser = (): Promise<ApiUser | false> => {
.catch((error) => {
// we assume that a 401 means the user is not logged in
if (error instanceof ApiError && error.statusCode === 401) {
+ attemptSilentLogin(3600)
resolve(false)
} else {
reject(error)
diff --git a/src/frontend/src/features/auth/index.ts b/src/frontend/src/features/auth/index.ts
index 0b533fea..0a597d74 100644
--- a/src/frontend/src/features/auth/index.ts
+++ b/src/frontend/src/features/auth/index.ts
@@ -2,3 +2,4 @@ export { useUser } from './api/useUser'
export { authUrl } from './utils/authUrl'
export { logoutUrl } from './utils/logoutUrl'
export { RenderIfUserFetched } from './components/RenderIfUserFetched'
+export { attemptSilentLogin } from './utils/silentLogin'
diff --git a/src/frontend/src/features/auth/utils/silentLogin.ts b/src/frontend/src/features/auth/utils/silentLogin.ts
new file mode 100644
index 00000000..c301480b
--- /dev/null
+++ b/src/frontend/src/features/auth/utils/silentLogin.ts
@@ -0,0 +1,30 @@
+import { authUrl } from "@/features/auth";
+
+const SILENT_LOGIN_RETRY_KEY = 'silent-login-retry'
+
+const isRetryAllowed = () => {
+ const lastRetryDate = localStorage.getItem(SILENT_LOGIN_RETRY_KEY);
+ if (!lastRetryDate) {
+ return true;
+ }
+ const now = new Date();
+ return now.getTime() > Number(lastRetryDate)
+}
+
+const setNextRetryTime = (retryIntervalInSeconds: number) => {
+ const now = new Date()
+ const nextRetryTime = now.getTime() + (retryIntervalInSeconds * 1000);
+ localStorage.setItem(SILENT_LOGIN_RETRY_KEY, String(nextRetryTime));
+}
+
+const initiateSilentLogin = () => {
+ window.location.href = authUrl(true)
+}
+
+export const attemptSilentLogin = (retryIntervalInSeconds: number) => {
+ if (!isRetryAllowed()) {
+ return
+ }
+ setNextRetryTime(retryIntervalInSeconds)
+ initiateSilentLogin()
+}