mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-24 11:01:17 -08:00
feat(tauri): use deep link for sso authentication
This commit is contained in:
3012 files changed
+57543
-104
No files matched your search
+110
-100
@@ -75,6 +75,7 @@ import { HomeCreateRoom } from './client/home/CreateRoom';
|
||||
import { Create } from './client/create';
|
||||
import { ToRoomEvent } from './client/ToRoomEvent';
|
||||
import { CallStatusRenderer } from './CallStatusRenderer';
|
||||
import { TauriDeepLinkBridge } from './TauriDeepLinkBridge';
|
||||
|
||||
/**
|
||||
* Returns true if there is at least one stored session.
|
||||
@@ -100,50 +101,58 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
const routes = createRoutesFromElements(
|
||||
<Route>
|
||||
<Route
|
||||
index
|
||||
loader={() => {
|
||||
if (hasStoredSession()) return redirect(getHomePath());
|
||||
const afterLoginPath = getAppPathFromHref(getOriginBaseUrl(), window.location.href);
|
||||
if (afterLoginPath) setAfterLoginRedirectPath(afterLoginPath);
|
||||
return redirect(getLoginPath());
|
||||
}}
|
||||
/>
|
||||
<Route
|
||||
loader={({ request }) => {
|
||||
// Allow reaching the login page with ?addAccount=1 even when already logged in
|
||||
const url = new URL(request.url);
|
||||
if (url.searchParams.get('addAccount') === '1') return null;
|
||||
if (hasStoredSession()) return redirect(getHomePath());
|
||||
return null;
|
||||
}}
|
||||
element={
|
||||
<>
|
||||
<AuthLayout />
|
||||
<UnAuthRouteThemeManager />
|
||||
<TauriDeepLinkBridge />
|
||||
<Outlet />
|
||||
</>
|
||||
}
|
||||
>
|
||||
<Route path={LOGIN_PATH} element={<Login />} />
|
||||
<Route path={REGISTER_PATH} element={<Register />} />
|
||||
<Route path={RESET_PASSWORD_PATH} element={<ResetPassword />} />
|
||||
</Route>
|
||||
|
||||
<Route
|
||||
loader={() => {
|
||||
const session = getFirstSession();
|
||||
if (!session) {
|
||||
const afterLoginPath = getAppPathFromHref(
|
||||
getOriginBaseUrl(hashRouter),
|
||||
window.location.href
|
||||
);
|
||||
<Route
|
||||
index
|
||||
loader={() => {
|
||||
if (hasStoredSession()) return redirect(getHomePath());
|
||||
const afterLoginPath = getAppPathFromHref(getOriginBaseUrl(), window.location.href);
|
||||
if (afterLoginPath) setAfterLoginRedirectPath(afterLoginPath);
|
||||
return redirect(getLoginPath());
|
||||
}}
|
||||
/>
|
||||
<Route
|
||||
loader={({ request }) => {
|
||||
// Allow reaching the login page with ?addAccount=1 even when already logged in
|
||||
const url = new URL(request.url);
|
||||
if (url.searchParams.get('addAccount') === '1') return null;
|
||||
if (hasStoredSession()) return redirect(getHomePath());
|
||||
return null;
|
||||
}}
|
||||
element={
|
||||
<>
|
||||
<AuthLayout />
|
||||
<UnAuthRouteThemeManager />
|
||||
</>
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
element={
|
||||
<AuthRouteThemeManager>
|
||||
{/* HandleNotificationClick must live outside ClientRoot's loading gate so
|
||||
>
|
||||
<Route path={LOGIN_PATH} element={<Login />} />
|
||||
<Route path={REGISTER_PATH} element={<Register />} />
|
||||
<Route path={RESET_PASSWORD_PATH} element={<ResetPassword />} />
|
||||
</Route>
|
||||
|
||||
<Route
|
||||
loader={() => {
|
||||
const session = getFirstSession();
|
||||
if (!session) {
|
||||
const afterLoginPath = getAppPathFromHref(
|
||||
getOriginBaseUrl(hashRouter),
|
||||
window.location.href
|
||||
);
|
||||
if (afterLoginPath) setAfterLoginRedirectPath(afterLoginPath);
|
||||
return redirect(getLoginPath());
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
element={
|
||||
<AuthRouteThemeManager>
|
||||
{/* HandleNotificationClick must live outside ClientRoot's loading gate so
|
||||
SW notification-click postMessages are never dropped during client
|
||||
reloads (e.g., account switches). It only needs navigate + Jotai atoms. */}
|
||||
<HandleNotificationClick />
|
||||
@@ -274,75 +283,76 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
|
||||
const decodedSpaceIdOrAlias =
|
||||
encodedSpaceIdOrAlias && decodeURIComponent(encodedSpaceIdOrAlias);
|
||||
|
||||
if (decodedSpaceIdOrAlias) {
|
||||
return redirect(getSpaceLobbyPath(decodedSpaceIdOrAlias));
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
element={<WelcomePage />}
|
||||
if (decodedSpaceIdOrAlias) {
|
||||
return redirect(getSpaceLobbyPath(decodedSpaceIdOrAlias));
|
||||
}
|
||||
return null;
|
||||
}}
|
||||
element={<WelcomePage />}
|
||||
/>
|
||||
)}
|
||||
<Route path={LOBBY_PATH_SEGMENT} element={<Lobby />} />
|
||||
<Route path={SEARCH_PATH_SEGMENT} element={<SpaceSearch />} />
|
||||
<Route
|
||||
path={ROOM_PATH_SEGMENT}
|
||||
element={
|
||||
<SpaceRouteRoomProvider>
|
||||
<Room />
|
||||
</SpaceRouteRoomProvider>
|
||||
}
|
||||
/>
|
||||
)}
|
||||
<Route path={LOBBY_PATH_SEGMENT} element={<Lobby />} />
|
||||
<Route path={SEARCH_PATH_SEGMENT} element={<SpaceSearch />} />
|
||||
</Route>
|
||||
<Route
|
||||
path={ROOM_PATH_SEGMENT}
|
||||
path={EXPLORE_PATH}
|
||||
element={
|
||||
<SpaceRouteRoomProvider>
|
||||
<Room />
|
||||
</SpaceRouteRoomProvider>
|
||||
<PageRoot
|
||||
nav={
|
||||
<MobileFriendlyPageNav path={EXPLORE_PATH}>
|
||||
<Explore />
|
||||
</MobileFriendlyPageNav>
|
||||
}
|
||||
>
|
||||
<Outlet />
|
||||
</PageRoot>
|
||||
}
|
||||
/>
|
||||
>
|
||||
{mobile ? null : (
|
||||
<Route
|
||||
index
|
||||
loader={() => redirect(getExploreFeaturedPath())}
|
||||
element={<WelcomePage />}
|
||||
/>
|
||||
)}
|
||||
<Route path={FEATURED_PATH_SEGMENT} element={<FeaturedRooms />} />
|
||||
<Route path={SERVER_PATH_SEGMENT} element={<PublicRooms />} />
|
||||
</Route>
|
||||
<Route path={CREATE_PATH} element={<Create />} />
|
||||
<Route
|
||||
path={INBOX_PATH}
|
||||
element={
|
||||
<PageRoot
|
||||
nav={
|
||||
<MobileFriendlyPageNav path={INBOX_PATH}>
|
||||
<Inbox />
|
||||
</MobileFriendlyPageNav>
|
||||
}
|
||||
>
|
||||
<Outlet />
|
||||
</PageRoot>
|
||||
}
|
||||
>
|
||||
{mobile ? null : (
|
||||
<Route
|
||||
index
|
||||
loader={() => redirect(getInboxNotificationsPath())}
|
||||
element={<WelcomePage />}
|
||||
/>
|
||||
)}
|
||||
<Route path={NOTIFICATIONS_PATH_SEGMENT} element={<Notifications />} />
|
||||
<Route path={INVITES_PATH_SEGMENT} element={<Invites />} />
|
||||
</Route>
|
||||
<Route path={TO_ROOM_EVENT_PATH} element={<ToRoomEvent />} />
|
||||
</Route>
|
||||
<Route
|
||||
path={EXPLORE_PATH}
|
||||
element={
|
||||
<PageRoot
|
||||
nav={
|
||||
<MobileFriendlyPageNav path={EXPLORE_PATH}>
|
||||
<Explore />
|
||||
</MobileFriendlyPageNav>
|
||||
}
|
||||
>
|
||||
<Outlet />
|
||||
</PageRoot>
|
||||
}
|
||||
>
|
||||
{mobile ? null : (
|
||||
<Route
|
||||
index
|
||||
loader={() => redirect(getExploreFeaturedPath())}
|
||||
element={<WelcomePage />}
|
||||
/>
|
||||
)}
|
||||
<Route path={FEATURED_PATH_SEGMENT} element={<FeaturedRooms />} />
|
||||
<Route path={SERVER_PATH_SEGMENT} element={<PublicRooms />} />
|
||||
</Route>
|
||||
<Route path={CREATE_PATH} element={<Create />} />
|
||||
<Route
|
||||
path={INBOX_PATH}
|
||||
element={
|
||||
<PageRoot
|
||||
nav={
|
||||
<MobileFriendlyPageNav path={INBOX_PATH}>
|
||||
<Inbox />
|
||||
</MobileFriendlyPageNav>
|
||||
}
|
||||
>
|
||||
<Outlet />
|
||||
</PageRoot>
|
||||
}
|
||||
>
|
||||
{mobile ? null : (
|
||||
<Route
|
||||
index
|
||||
loader={() => redirect(getInboxNotificationsPath())}
|
||||
element={<WelcomePage />}
|
||||
/>
|
||||
)}
|
||||
<Route path={NOTIFICATIONS_PATH_SEGMENT} element={<Notifications />} />
|
||||
<Route path={INVITES_PATH_SEGMENT} element={<Invites />} />
|
||||
</Route>
|
||||
<Route path={TO_ROOM_EVENT_PATH} element={<ToRoomEvent />} />
|
||||
</Route>
|
||||
<Route path="/*" element={<p>Page not found</p>} />
|
||||
</Route>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { createLogger } from '$utils/debug';
|
||||
import { parseTauriSsoCallback } from '$pages/auth/tauriSso';
|
||||
import { getLoginPath, withSearchParam } from './pathUtils';
|
||||
|
||||
const log = createLogger('TauriDeepLinkBridge');
|
||||
|
||||
const mapDeepLinkToLoginPath = (rawUrl: string): string | undefined => {
|
||||
const callback = parseTauriSsoCallback(rawUrl);
|
||||
if (!callback) return undefined;
|
||||
|
||||
return withSearchParam(getLoginPath(callback.server), { loginToken: callback.loginToken });
|
||||
};
|
||||
|
||||
export function TauriDeepLinkBridge() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isTauri()) return undefined;
|
||||
|
||||
let mounted = true;
|
||||
let unlisten: (() => void) | undefined;
|
||||
|
||||
const applyUrls = (urls: string[]) => {
|
||||
const loginPath = urls.map(mapDeepLinkToLoginPath).find((path): path is string => !!path);
|
||||
if (loginPath) {
|
||||
navigate(loginPath, { replace: true });
|
||||
}
|
||||
};
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const { getCurrent, onOpenUrl } = await import('@tauri-apps/plugin-deep-link');
|
||||
|
||||
const current = await getCurrent();
|
||||
applyUrls(current ?? []);
|
||||
|
||||
const removeListener = await onOpenUrl((urls) => {
|
||||
applyUrls(urls);
|
||||
});
|
||||
|
||||
if (mounted) {
|
||||
unlisten = removeListener;
|
||||
} else {
|
||||
removeListener();
|
||||
}
|
||||
} catch (error) {
|
||||
log.warn('Failed to initialize deep link bridge:', error);
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
mounted = false;
|
||||
unlisten?.();
|
||||
};
|
||||
}, [navigate]);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Avatar, AvatarImage, Box, Button, Text } from 'folds';
|
||||
import { IIdentityProvider, SSOAction, createClient } from '$types/matrix-sdk';
|
||||
import { useMemo } from 'react';
|
||||
import { MouseEvent, useMemo } from 'react';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { openUrl } from '@tauri-apps/plugin-opener';
|
||||
import { useAutoDiscoveryInfo } from '$hooks/useAutoDiscoveryInfo';
|
||||
|
||||
type SSOLoginProps = {
|
||||
@@ -25,6 +27,12 @@ export function SSOLogin({ providers, redirectUrl, action, saveScreenSpace }: SS
|
||||
|
||||
const renderAsIcons = withoutIcon ? false : saveScreenSpace && providers && providers.length > 2;
|
||||
|
||||
const openSso = async (event: MouseEvent, url: string) => {
|
||||
if (!isTauri()) return;
|
||||
event.preventDefault();
|
||||
await openUrl(url);
|
||||
};
|
||||
|
||||
return (
|
||||
<Box justifyContent="Center" gap="600" wrap="Wrap">
|
||||
{providers ? (
|
||||
@@ -41,6 +49,7 @@ export function SSOLogin({ providers, redirectUrl, action, saveScreenSpace }: SS
|
||||
key={id}
|
||||
as="a"
|
||||
href={getSSOIdUrl(id)}
|
||||
onClick={(event) => openSso(event, getSSOIdUrl(id))}
|
||||
aria-label={buttonTitle}
|
||||
size="300"
|
||||
radii="300"
|
||||
@@ -56,6 +65,7 @@ export function SSOLogin({ providers, redirectUrl, action, saveScreenSpace }: SS
|
||||
key={id}
|
||||
as="a"
|
||||
href={getSSOIdUrl(id)}
|
||||
onClick={(event) => openSso(event, getSSOIdUrl(id))}
|
||||
size="500"
|
||||
variant="Secondary"
|
||||
fill="Soft"
|
||||
@@ -79,6 +89,7 @@ export function SSOLogin({ providers, redirectUrl, action, saveScreenSpace }: SS
|
||||
style={{ width: '100%' }}
|
||||
as="a"
|
||||
href={getSSOIdUrl()}
|
||||
onClick={(event) => openSso(event, getSSOIdUrl())}
|
||||
size="500"
|
||||
variant="Secondary"
|
||||
fill="Soft"
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useMemo } from 'react';
|
||||
import { Box, Text, color } from 'folds';
|
||||
import { Link, useSearchParams } from 'react-router-dom';
|
||||
import { SSOAction } from '$types/matrix-sdk';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { useAuthFlows } from '$hooks/useAuthFlows';
|
||||
import { useAuthServer } from '$hooks/useAuthServer';
|
||||
import { useParsedLoginFlows } from '$hooks/useParsedLoginFlows';
|
||||
@@ -11,6 +12,7 @@ import { LoginPathSearchParams } from '$pages/paths';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import { SSOLogin } from '$pages/auth/SSOLogin';
|
||||
import { OrDivider } from '$pages/auth/OrDivider';
|
||||
import { buildTauriSsoRedirectUrl } from '$pages/auth/tauriSso';
|
||||
import { PasswordLoginForm } from './PasswordLoginForm';
|
||||
import { TokenLogin } from './TokenLogin';
|
||||
|
||||
@@ -41,7 +43,8 @@ export function Login() {
|
||||
const { loginFlows } = useAuthFlows();
|
||||
const [searchParams] = useSearchParams();
|
||||
const loginSearchParams = useLoginSearchParams(searchParams);
|
||||
const ssoRedirectUrl = usePathWithOrigin(getLoginPath(server));
|
||||
const webSsoRedirectUrl = usePathWithOrigin(getLoginPath(server));
|
||||
const ssoRedirectUrl = isTauri() ? buildTauriSsoRedirectUrl(server) : webSsoRedirectUrl;
|
||||
const loginTokenForHashRouter = getLoginTokenSearchParam();
|
||||
const absoluteLoginPath = usePathWithOrigin(getLoginPath(server));
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useMemo } from 'react';
|
||||
import { Box, Text, color } from 'folds';
|
||||
import { Link, useSearchParams } from 'react-router-dom';
|
||||
import { SSOAction } from '$types/matrix-sdk';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { useAuthServer } from '$hooks/useAuthServer';
|
||||
import { RegisterFlowStatus, useAuthFlows } from '$hooks/useAuthFlows';
|
||||
import { useParsedLoginFlows } from '$hooks/useParsedLoginFlows';
|
||||
@@ -11,6 +12,7 @@ import { usePathWithOrigin } from '$hooks/usePathWithOrigin';
|
||||
import { RegisterPathSearchParams } from '$pages/paths';
|
||||
import { SSOLogin } from '$pages/auth/SSOLogin';
|
||||
import { OrDivider } from '$pages/auth/OrDivider';
|
||||
import { buildTauriSsoRedirectUrl } from '$pages/auth/tauriSso';
|
||||
import { PasswordRegisterForm, SUPPORTED_REGISTER_STAGES } from './PasswordRegisterForm';
|
||||
|
||||
const useRegisterSearchParams = (searchParams: URLSearchParams): RegisterPathSearchParams =>
|
||||
@@ -31,7 +33,8 @@ export function Register() {
|
||||
const { sso } = useParsedLoginFlows(loginFlows.flows);
|
||||
|
||||
// redirect to /login because only that path handle m.login.token
|
||||
const ssoRedirectUrl = usePathWithOrigin(getLoginPath(server));
|
||||
const webSsoRedirectUrl = usePathWithOrigin(getLoginPath(server));
|
||||
const ssoRedirectUrl = isTauri() ? buildTauriSsoRedirectUrl(server) : webSsoRedirectUrl;
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="500">
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
const TAURI_SSO_PROTOCOL = 'sable:';
|
||||
const TAURI_SSO_HOST = 'login';
|
||||
|
||||
type TauriSsoCallback = {
|
||||
loginToken: string;
|
||||
server?: string;
|
||||
};
|
||||
|
||||
export const buildTauriSsoRedirectUrl = (server?: string): string => {
|
||||
const redirectUrl = new URL(`${TAURI_SSO_PROTOCOL}//${TAURI_SSO_HOST}`);
|
||||
|
||||
if (server) {
|
||||
redirectUrl.searchParams.set('server', server);
|
||||
}
|
||||
|
||||
return redirectUrl.toString();
|
||||
};
|
||||
|
||||
export const parseTauriSsoCallback = (rawUrl: string): TauriSsoCallback | undefined => {
|
||||
try {
|
||||
const callbackUrl = new URL(rawUrl);
|
||||
if (callbackUrl.protocol !== TAURI_SSO_PROTOCOL) return undefined;
|
||||
if (callbackUrl.hostname !== TAURI_SSO_HOST) return undefined;
|
||||
|
||||
const loginToken = callbackUrl.searchParams.get('loginToken');
|
||||
if (!loginToken) return undefined;
|
||||
|
||||
return {
|
||||
loginToken,
|
||||
server: callbackUrl.searchParams.get('server') ?? undefined,
|
||||
};
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
PushProcessor,
|
||||
} from '$types/matrix-sdk';
|
||||
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import {
|
||||
sessionsAtom,
|
||||
activeSessionIdAtom,
|
||||
@@ -158,7 +159,7 @@ export function BackgroundNotifications() {
|
||||
// by the SW notificationclick event. This routes through HandleNotificationClick
|
||||
// (postMessage path) which does the account switch + deep link reliably on all
|
||||
// platforms including iOS where window.Notification onclick is not fired.
|
||||
if ('serviceWorker' in navigator) {
|
||||
if ('serviceWorker' in navigator && !isTauri()) {
|
||||
try {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
await reg.showNotification(opts.title, {
|
||||
|
||||
Reference in new issue
Block a user