@sanity/sdk-react 0.0.0-alpha.10 → 0.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components.d.ts +0 -1
- package/dist/components.js +24 -47
- package/dist/components.js.map +1 -1
- package/package.json +4 -5
- package/src/components/Login/LoginLinks.test.tsx +91 -0
- package/src/components/Login/LoginLinks.tsx +58 -0
- package/src/components/auth/AuthBoundary.test.tsx +1 -1
- package/src/components/auth/Login.test.tsx +1 -1
- package/src/components/auth/Login.tsx +11 -26
- package/src/components/auth/LoginCallback.tsx +4 -7
- package/src/components/auth/LoginError.tsx +12 -8
- package/src/components/auth/LoginFooter.tsx +13 -20
- package/src/components/auth/LoginLayout.tsx +8 -9
- package/src/components/auth/authTestHelpers.tsx +1 -8
package/dist/components.d.ts
CHANGED
package/dist/components.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
2
|
import { AuthStateType, createSanityInstance } from "@sanity/sdk";
|
|
3
|
-
import { Suspense, useEffect, useCallback, useMemo } from "react";
|
|
3
|
+
import { Fragment, Suspense, useEffect, useCallback, useMemo } from "react";
|
|
4
4
|
import { ErrorBoundary } from "react-error-boundary";
|
|
5
5
|
import { useLoginUrls, useHandleCallback, useLogOut, useAuthState } from "./_chunks-es/useLogOut.js";
|
|
6
|
-
import { Box, Flex, Inline, Text, Container, Card, Heading, Spinner, Stack, Button } from "@sanity/ui";
|
|
7
6
|
import { SanityLogo } from "@sanity/logos";
|
|
8
7
|
import { SanityProvider } from "./_chunks-es/context.js";
|
|
9
8
|
class AuthError extends Error {
|
|
@@ -34,18 +33,9 @@ const LINKS = [
|
|
|
34
33
|
}
|
|
35
34
|
];
|
|
36
35
|
function LoginFooter() {
|
|
37
|
-
return /* @__PURE__ */ jsxs(
|
|
38
|
-
/* @__PURE__ */ jsx(
|
|
39
|
-
/* @__PURE__ */ jsx(
|
|
40
|
-
"a",
|
|
41
|
-
{
|
|
42
|
-
href: link.url,
|
|
43
|
-
target: "_blank",
|
|
44
|
-
rel: "noopener noreferrer",
|
|
45
|
-
style: { color: "inherit" },
|
|
46
|
-
children: link.title
|
|
47
|
-
}
|
|
48
|
-
) }, link.url)) }) })
|
|
36
|
+
return /* @__PURE__ */ jsxs("div", { className: "sc-login-footer", children: [
|
|
37
|
+
/* @__PURE__ */ jsx(SanityLogo, { className: "sc-login-footer__logo" }),
|
|
38
|
+
/* @__PURE__ */ jsx("ul", { className: "sc-login-footer__links", children: LINKS.map((link) => /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx("li", { className: "sc-login-footer__link", children: /* @__PURE__ */ jsx("a", { href: link.url, target: "_blank", rel: "noopener noreferrer", children: link.title }) }) }, link.title)) })
|
|
49
39
|
] });
|
|
50
40
|
}
|
|
51
41
|
function LoginLayout({
|
|
@@ -53,38 +43,23 @@ function LoginLayout({
|
|
|
53
43
|
footer = /* @__PURE__ */ jsx(LoginFooter, {}),
|
|
54
44
|
header
|
|
55
45
|
}) {
|
|
56
|
-
return /* @__PURE__ */ jsx(
|
|
57
|
-
|
|
58
|
-
|
|
46
|
+
return /* @__PURE__ */ jsx("div", { className: "sc-login-layout", children: /* @__PURE__ */ jsxs("div", { className: "sc-login-layout__container", children: [
|
|
47
|
+
/* @__PURE__ */ jsxs("div", { className: "sc-login-layout__card", children: [
|
|
48
|
+
header && /* @__PURE__ */ jsx("div", { className: "sc-login-layout__card-header", children: header }),
|
|
49
|
+
children && /* @__PURE__ */ jsx("div", { className: "sc-login-layout__card-body", children })
|
|
50
|
+
] }),
|
|
59
51
|
footer
|
|
60
52
|
] }) });
|
|
61
53
|
}
|
|
62
54
|
function Login({ header, footer }) {
|
|
63
|
-
return /* @__PURE__ */
|
|
64
|
-
/* @__PURE__ */ jsx(
|
|
65
|
-
/* @__PURE__ */ jsx(
|
|
66
|
-
|
|
67
|
-
{
|
|
68
|
-
fallback: /* @__PURE__ */ jsx(Box, { padding: 5, children: /* @__PURE__ */ jsx(Flex, { align: "center", justify: "center", children: /* @__PURE__ */ jsx(Spinner, {}) }) }),
|
|
69
|
-
children: /* @__PURE__ */ jsx(Providers, {})
|
|
70
|
-
}
|
|
71
|
-
)
|
|
72
|
-
] });
|
|
55
|
+
return /* @__PURE__ */ jsx(LoginLayout, { header, footer, children: /* @__PURE__ */ jsxs("div", { className: "sc-login", children: [
|
|
56
|
+
/* @__PURE__ */ jsx("h1", { className: "sc-login__title", children: "Choose login provider" }),
|
|
57
|
+
/* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx("div", { className: "sc-login__loading", children: "Loading\u2026" }), children: /* @__PURE__ */ jsx(Providers, {}) })
|
|
58
|
+
] }) });
|
|
73
59
|
}
|
|
74
60
|
function Providers() {
|
|
75
61
|
const loginUrls = useLoginUrls();
|
|
76
|
-
return /* @__PURE__ */ jsx(
|
|
77
|
-
Button,
|
|
78
|
-
{
|
|
79
|
-
as: "a",
|
|
80
|
-
href: url,
|
|
81
|
-
mode: "ghost",
|
|
82
|
-
text: title,
|
|
83
|
-
textAlign: "center",
|
|
84
|
-
fontSize: 2
|
|
85
|
-
},
|
|
86
|
-
url
|
|
87
|
-
)) });
|
|
62
|
+
return /* @__PURE__ */ jsx("div", { className: "sc-login-providers", children: loginUrls.map(({ title, url }) => /* @__PURE__ */ jsx("a", { href: url, children: title }, url)) });
|
|
88
63
|
}
|
|
89
64
|
function LoginCallback({ header, footer }) {
|
|
90
65
|
const handleCallback = useHandleCallback();
|
|
@@ -93,10 +68,10 @@ function LoginCallback({ header, footer }) {
|
|
|
93
68
|
handleCallback(url.toString()).then((replacementLocation) => {
|
|
94
69
|
replacementLocation && history.replaceState(null, "", replacementLocation);
|
|
95
70
|
});
|
|
96
|
-
}, [handleCallback]), /* @__PURE__ */
|
|
97
|
-
/* @__PURE__ */ jsx(
|
|
98
|
-
/* @__PURE__ */ jsx(
|
|
99
|
-
] });
|
|
71
|
+
}, [handleCallback]), /* @__PURE__ */ jsx(LoginLayout, { header, footer, children: /* @__PURE__ */ jsxs("div", { className: "sc-login-callback", children: [
|
|
72
|
+
/* @__PURE__ */ jsx("h1", { className: "sc-login-callback__title", children: "Logging you in\u2026" }),
|
|
73
|
+
/* @__PURE__ */ jsx("div", { className: "sc-login-callback__loading", children: "Loading\u2026" })
|
|
74
|
+
] }) });
|
|
100
75
|
}
|
|
101
76
|
function LoginError({
|
|
102
77
|
error,
|
|
@@ -108,10 +83,12 @@ function LoginError({
|
|
|
108
83
|
const logout = useLogOut(), handleRetry = useCallback(async () => {
|
|
109
84
|
await logout(), resetErrorBoundary();
|
|
110
85
|
}, [logout, resetErrorBoundary]);
|
|
111
|
-
return /* @__PURE__ */ jsx(LoginLayout, { header, footer, children: /* @__PURE__ */ jsxs(
|
|
112
|
-
/* @__PURE__ */
|
|
113
|
-
|
|
114
|
-
|
|
86
|
+
return /* @__PURE__ */ jsx(LoginLayout, { header, footer, children: /* @__PURE__ */ jsxs("div", { className: "sc-login-error", children: [
|
|
87
|
+
/* @__PURE__ */ jsxs("div", { className: "sc-login-error__content", children: [
|
|
88
|
+
/* @__PURE__ */ jsx("h2", { className: "sc-login-error__title", children: "Authentication Error" }),
|
|
89
|
+
/* @__PURE__ */ jsx("p", { className: "sc-login-error__description", children: "Please try again or contact support if the problem persists." })
|
|
90
|
+
] }),
|
|
91
|
+
/* @__PURE__ */ jsx("button", { className: "sc-login-error__button", onClick: handleRetry, children: "Retry" })
|
|
115
92
|
] }) });
|
|
116
93
|
}
|
|
117
94
|
typeof window < "u" && window.self !== window.top && import("@sanity/os/bridge");
|
package/dist/components.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"components.js","sources":["../src/components/auth/AuthError.ts","../src/components/auth/LoginFooter.tsx","../src/components/auth/LoginLayout.tsx","../src/components/auth/Login.tsx","../src/components/auth/LoginCallback.tsx","../src/components/auth/LoginError.tsx","../src/components/auth/AuthBoundary.tsx","../src/components/SanityApp.tsx"],"sourcesContent":["/**\n * Error class for authentication-related errors. Wraps errors thrown during the\n * authentication flow.\n *\n * @remarks\n * This class provides a consistent error type for authentication failures while\n * preserving the original error as the cause. If the original error has a\n * message property, it will be used as the error message.\n *\n * @alpha\n */\nexport class AuthError extends Error {\n constructor(error: unknown) {\n if (\n typeof error === 'object' &&\n !!error &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n super(error.message)\n } else {\n super()\n }\n\n this.cause = error\n }\n}\n","import {SanityLogo} from '@sanity/logos'\nimport {Box, Flex, Inline, Text} from '@sanity/ui'\n\nconst LINKS = [\n {\n url: 'https://slack.sanity.io/',\n i18nKey: 'workspaces.community-title',\n title: 'Community',\n },\n {\n url: 'https://www.sanity.io/docs',\n i18nKey: 'workspaces.docs-title',\n title: 'Docs',\n },\n {\n url: 'https://www.sanity.io/legal/privacy',\n i18nKey: 'workspaces.privacy-title',\n title: 'Privacy',\n },\n {\n url: 'https://www.sanity.io',\n i18nKey: 'workspaces.sanity-io-title',\n title: 'sanity.io',\n },\n]\n\n/**\n * Default footer component for login screens showing Sanity branding and legal\n * links.\n *\n * @alpha\n */\nexport function LoginFooter(): React.ReactNode {\n return (\n <Box>\n <Flex justify=\"center\">\n <SanityLogo />\n </Flex>\n\n <Flex justify=\"center\">\n <Inline space={2} paddingY={3}>\n {LINKS.map((link) => (\n <Text size={0} key={link.url}>\n <a\n href={link.url}\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n style={{color: 'inherit'}}\n >\n {link.title}\n </a>\n </Text>\n ))}\n </Inline>\n </Flex>\n </Box>\n )\n}\n","import {Card, Container} from '@sanity/ui'\n\nimport {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n * @internal\n */\nexport interface LoginLayoutProps {\n /** Optional header content rendered at top of card */\n header?: React.ReactNode\n\n /** Optional footer content rendered below card. Defaults to an internal login footer */\n footer?: React.ReactNode\n\n /** Main content rendered in card body */\n children?: React.ReactNode\n}\n\n/**\n * Layout component for login-related screens providing consistent styling and structure.\n * Renders content in a centered card with optional header and footer sections.\n *\n * Can be used to build custom login screens for the AuthBoundary component, including:\n * - Login provider selection (LoginComponent)\n * - OAuth callback handling (CallbackComponent)\n * - Error states (LoginErrorComponent)\n *\n * @example\n * ```tsx\n * // Custom login screen using the layout\n * function CustomLogin({header, footer}: LoginLayoutProps) {\n * return (\n * <LoginLayout\n * header={header}\n * footer={footer}\n * >\n * <CustomLoginContent />\n * </LoginLayout>\n * )\n * }\n *\n * // Use with AuthBoundary\n * <AuthBoundary\n * LoginComponent={CustomLogin}\n * header={<Logo />}\n * >\n * <ProtectedContent />\n * </AuthBoundary>\n * ```\n *\n * @alpha\n */\nexport function LoginLayout({\n children,\n footer = <LoginFooter />,\n header,\n}: LoginLayoutProps): React.ReactNode {\n return (\n <Container width={0}>\n <Card shadow={1} radius={2} padding={4}>\n {header && header}\n\n {children && children}\n\n {footer}\n </Card>\n </Container>\n )\n}\n","import {Box, Button, Flex, Heading, Spinner, Stack} from '@sanity/ui'\nimport {type JSX, Suspense} from 'react'\n\nimport {useLoginUrls} from '../../hooks/auth/useLoginUrls'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * Login component that displays available authentication providers.\n * Renders a list of login options with a loading fallback while providers load.\n *\n * @alpha\n * @internal\n */\nexport function Login({header, footer}: LoginLayoutProps): JSX.Element {\n return (\n <LoginLayout header={header} footer={footer}>\n <Heading as=\"h6\" align=\"center\">\n Choose login provider:\n </Heading>\n\n <Suspense\n fallback={\n <Box padding={5}>\n <Flex align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </Box>\n }\n >\n <Providers />\n </Suspense>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <Stack space={3} marginY={5}>\n {loginUrls.map(({title, url}) => (\n <Button\n key={url}\n as=\"a\"\n href={url}\n mode=\"ghost\"\n text={title}\n textAlign=\"center\"\n fontSize={2}\n ></Button>\n ))}\n </Stack>\n )\n}\n","import {Flex, Heading, Spinner} from '@sanity/ui'\nimport {useEffect} from 'react'\n\nimport {useHandleCallback} from '../../hooks/auth/useHandleCallback'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n/**\n * Component shown during auth callback processing that handles login completion.\n * Automatically processes the auth callback when mounted and updates the URL\n * to remove callback parameters without triggering a page reload.\n *\n * @alpha\n */\nexport function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNode {\n const handleCallback = useHandleCallback()\n\n useEffect(() => {\n const url = new URL(location.href)\n handleCallback(url.toString()).then((replacementLocation) => {\n if (replacementLocation) {\n // history API with `replaceState` is used to prevent a reload but still\n // remove the short-lived token from the URL\n history.replaceState(null, '', replacementLocation)\n }\n })\n }, [handleCallback])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <Heading as=\"h6\" align=\"center\">\n Logging you in…\n </Heading>\n <Flex paddingY={5} align=\"center\" justify=\"center\">\n <Spinner />\n </Flex>\n </LoginLayout>\n )\n}\n","import {Button, Heading, Stack, Text} from '@sanity/ui'\nimport {useCallback} from 'react'\nimport {type FallbackProps} from 'react-error-boundary'\n\nimport {useLogOut} from '../../hooks/auth/useLogOut'\nimport {AuthError} from './AuthError'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * @alpha\n */\nexport type LoginErrorProps = FallbackProps & LoginLayoutProps\n\n/**\n * Displays authentication error details and provides retry functionality.\n * Only handles {@link AuthError} instances - rethrows other error types.\n *\n * @alpha\n */\nexport function LoginError({\n error,\n resetErrorBoundary,\n header,\n footer,\n}: LoginErrorProps): React.ReactNode {\n if (!(error instanceof AuthError)) throw error\n const logout = useLogOut()\n\n const handleRetry = useCallback(async () => {\n await logout()\n resetErrorBoundary()\n }, [logout, resetErrorBoundary])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <Stack space={5} marginBottom={5}>\n <Heading as=\"h6\" align=\"center\">\n Authentication Error\n </Heading>\n <Text align=\"center\">Please try again or contact support if the problem persists.</Text>\n <Button mode=\"ghost\" onClick={handleRetry} text=\"Retry\" fontSize={2} />\n </Stack>\n </LoginLayout>\n )\n}\n","import {AuthStateType} from '@sanity/sdk'\nimport {useMemo} from 'react'\nimport {ErrorBoundary, type FallbackProps} from 'react-error-boundary'\n\nimport {useAuthState} from '../../hooks/auth/useAuthState'\nimport {AuthError} from './AuthError'\nimport {Login} from './Login'\nimport {LoginCallback} from './LoginCallback'\nimport {LoginError, type LoginErrorProps} from './LoginError'\nimport {type LoginLayoutProps} from './LoginLayout'\n\n// Only import bridge if we're in an iframe. This assumes that the app is\n// running withing SanityOS if it is in an iframe.\nif (typeof window !== 'undefined' && window.self !== window.top) {\n import('@sanity/os/bridge')\n}\n\n/**\n * @internal\n */\ninterface AuthBoundaryProps extends LoginLayoutProps {\n /**\n * Custom component to render the login screen.\n * Receives all login layout props. Defaults to {@link Login}.\n */\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render during OAuth callback processing.\n * Receives all login layout props. Defaults to {@link LoginCallback}.\n */\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render when authentication errors occur.\n * Receives login layout props and error boundary props. Defaults to\n * {@link LoginError}\n */\n LoginErrorComponent?: React.ComponentType<LoginErrorProps>\n}\n\n/**\n * A component that handles authentication flow and error boundaries for a\n * protected section of the application.\n *\n * @remarks\n * This component manages different authentication states and renders the\n * appropriate components based on that state.\n *\n * @example\n * ```tsx\n * function App() {\n * return (\n * <AuthBoundary header={<MyLogo />}>\n * <ProtectedContent />\n * </AuthBoundary>\n * )\n * }\n * ```\n *\n * @internal\n */\nexport function AuthBoundary({\n LoginErrorComponent = LoginError,\n ...props\n}: AuthBoundaryProps): React.ReactNode {\n const {header, footer} = props\n const FallbackComponent = useMemo(() => {\n return function LoginComponentWithLayoutProps(fallbackProps: FallbackProps) {\n return <LoginErrorComponent {...fallbackProps} header={header} footer={footer} />\n }\n }, [header, footer, LoginErrorComponent])\n\n return (\n <ErrorBoundary FallbackComponent={FallbackComponent}>\n <AuthSwitch {...props} />\n </ErrorBoundary>\n )\n}\n\ninterface AuthSwitchProps extends LoginLayoutProps {\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n}\n\nfunction AuthSwitch({\n LoginComponent = Login,\n CallbackComponent = LoginCallback,\n children,\n ...props\n}: AuthSwitchProps) {\n const authState = useAuthState()\n\n switch (authState.type) {\n case AuthStateType.ERROR: {\n throw new AuthError(authState.error)\n }\n case AuthStateType.LOGGING_IN: {\n return <CallbackComponent {...props} />\n }\n case AuthStateType.LOGGED_IN: {\n return children\n }\n default: {\n return <LoginComponent {...props} />\n }\n }\n}\n","import {createSanityInstance, type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\n\n/**\n * @public\n */\nexport interface SanityAppProps {\n sanityConfig: SanityConfig\n children: React.ReactNode\n}\n\n/**\n * @public\n *\n * The SanityApp component provides your Sanity application with access to your Sanity configuration,\n * as well as application context and state which is used by the Sanity React hooks. Your application\n * must be wrapped with the SanityApp component to function properly.\n *\n * @param props - Your Sanity configuration and the React children to render\n * @returns Your Sanity application, integrated with your Sanity configuration and application context\n *\n * @example\n * ```\n * import { SanityApp } from '@sanity/sdk-react\n *\n * import MyAppRoot from './Root'\n *\n * const mySanityConfig = {\n * procectId: 'my-project-id',\n * dataset: 'production',\n * }\n *\n * export default function MyApp() {\n * return (\n * <SanityApp sanityConfig={mySanityConfig}>\n * <MyAppRoot />\n * </SanityApp>\n * )\n * }\n * ```\n */\nexport function SanityApp({sanityConfig, children}: SanityAppProps): ReactElement {\n const sanityInstance = createSanityInstance(sanityConfig)\n\n return (\n <SanityProvider sanityInstance={sanityInstance}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n"],"names":[],"mappings":";;;;;;;;AAWO,MAAM,kBAA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/B,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK,cAAc;AACX,YAAA,IAAI,UAAU,UAAU,KAAK;AAAA,IAErC,KAAK,cAAc;AACV,aAAA,oBAAC,mBAAmB,EAAA,GAAG,MAAO,CAAA;AAAA,IAEvC,KAAK,cAAc;AACV,aAAA;AAAA,IAET;AACS,aAAA,oBAAC,gBAAgB,EAAA,GAAG,MAAO,CAAA;AAAA,EAAA;AAGxC;AC/DO,SAAS,UAAU,EAAC,cAAc,YAAyC;AAC1E,QAAA,iBAAiB,qBAAqB,YAAY;AAExD,6BACG,gBAAe,EAAA,gBACd,UAAC,oBAAA,cAAA,EAAc,SAAS,CAAA,GAC1B;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"components.js","sources":["../src/components/auth/AuthError.ts","../src/components/auth/LoginFooter.tsx","../src/components/auth/LoginLayout.tsx","../src/components/auth/Login.tsx","../src/components/auth/LoginCallback.tsx","../src/components/auth/LoginError.tsx","../src/components/auth/AuthBoundary.tsx","../src/components/SanityApp.tsx"],"sourcesContent":["/**\n * Error class for authentication-related errors. Wraps errors thrown during the\n * authentication flow.\n *\n * @remarks\n * This class provides a consistent error type for authentication failures while\n * preserving the original error as the cause. If the original error has a\n * message property, it will be used as the error message.\n *\n * @alpha\n */\nexport class AuthError extends Error {\n constructor(error: unknown) {\n if (\n typeof error === 'object' &&\n !!error &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n super(error.message)\n } else {\n super()\n }\n\n this.cause = error\n }\n}\n","import {SanityLogo} from '@sanity/logos'\nimport {Fragment} from 'react'\n\nconst LINKS = [\n {\n url: 'https://slack.sanity.io/',\n i18nKey: 'workspaces.community-title',\n title: 'Community',\n },\n {\n url: 'https://www.sanity.io/docs',\n i18nKey: 'workspaces.docs-title',\n title: 'Docs',\n },\n {\n url: 'https://www.sanity.io/legal/privacy',\n i18nKey: 'workspaces.privacy-title',\n title: 'Privacy',\n },\n {\n url: 'https://www.sanity.io',\n i18nKey: 'workspaces.sanity-io-title',\n title: 'sanity.io',\n },\n]\n\n/**\n * Default footer component for login screens showing Sanity branding and legal\n * links.\n *\n * @alpha\n */\nexport function LoginFooter(): React.ReactNode {\n return (\n <div className=\"sc-login-footer\">\n <SanityLogo className=\"sc-login-footer__logo\" />\n\n <ul className=\"sc-login-footer__links\">\n {LINKS.map((link) => (\n <Fragment key={link.title}>\n <li className=\"sc-login-footer__link\">\n <a href={link.url} target=\"_blank\" rel=\"noopener noreferrer\">\n {link.title}\n </a>\n </li>\n </Fragment>\n ))}\n </ul>\n </div>\n )\n}\n","import {LoginFooter} from './LoginFooter'\n\n/**\n * @alpha\n */\nexport interface LoginLayoutProps {\n /** Optional header content rendered at top of card */\n header?: React.ReactNode\n\n /** Optional footer content rendered below card. Defaults to an internal login footer */\n footer?: React.ReactNode\n\n /** Main content rendered in card body */\n children?: React.ReactNode\n}\n\n/**\n * Layout component for login-related screens providing consistent styling and structure.\n * Renders content in a centered card with optional header and footer sections.\n *\n * Can be used to build custom login screens for the AuthBoundary component, including:\n * - Login provider selection (LoginComponent)\n * - OAuth callback handling (CallbackComponent)\n * - Error states (LoginErrorComponent)\n *\n * @example\n * ```tsx\n * // Custom login screen using the layout\n * function CustomLogin({header, footer}: LoginLayoutProps) {\n * return (\n * <LoginLayout\n * header={header}\n * footer={footer}\n * >\n * <CustomLoginContent />\n * </LoginLayout>\n * )\n * }\n *\n * // Use with AuthBoundary\n * <AuthBoundary\n * LoginComponent={CustomLogin}\n * header={<Logo />}\n * >\n * <ProtectedContent />\n * </AuthBoundary>\n * ```\n *\n * @alpha\n */\nexport function LoginLayout({\n children,\n footer = <LoginFooter />,\n header,\n}: LoginLayoutProps): React.ReactNode {\n return (\n <div className=\"sc-login-layout\">\n <div className=\"sc-login-layout__container\">\n <div className=\"sc-login-layout__card\">\n {header && <div className=\"sc-login-layout__card-header\">{header}</div>}\n\n {children && <div className=\"sc-login-layout__card-body\">{children}</div>}\n </div>\n\n {footer}\n </div>\n </div>\n )\n}\n","import {type JSX, Suspense} from 'react'\n\nimport {useLoginUrls} from '../../hooks/auth/useLoginUrls'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * Login component that displays available authentication providers.\n * Renders a list of login options with a loading fallback while providers load.\n *\n * @alpha\n * @internal\n */\nexport function Login({header, footer}: LoginLayoutProps): JSX.Element {\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login\">\n <h1 className=\"sc-login__title\">Choose login provider</h1>\n\n <Suspense fallback={<div className=\"sc-login__loading\">Loading…</div>}>\n <Providers />\n </Suspense>\n </div>\n </LoginLayout>\n )\n}\n\nfunction Providers() {\n const loginUrls = useLoginUrls()\n\n return (\n <div className=\"sc-login-providers\">\n {loginUrls.map(({title, url}) => (\n <a key={url} href={url}>\n {title}\n </a>\n ))}\n </div>\n )\n}\n","import {useEffect} from 'react'\n\nimport {useHandleCallback} from '../../hooks/auth/useHandleCallback'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n/**\n * Component shown during auth callback processing that handles login completion.\n * Automatically processes the auth callback when mounted and updates the URL\n * to remove callback parameters without triggering a page reload.\n *\n * @alpha\n */\nexport function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNode {\n const handleCallback = useHandleCallback()\n\n useEffect(() => {\n const url = new URL(location.href)\n handleCallback(url.toString()).then((replacementLocation) => {\n if (replacementLocation) {\n // history API with `replaceState` is used to prevent a reload but still\n // remove the short-lived token from the URL\n history.replaceState(null, '', replacementLocation)\n }\n })\n }, [handleCallback])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-callback\">\n <h1 className=\"sc-login-callback__title\">Logging you in…</h1>\n <div className=\"sc-login-callback__loading\">Loading…</div>\n </div>\n </LoginLayout>\n )\n}\n","import {useCallback} from 'react'\nimport {type FallbackProps} from 'react-error-boundary'\n\nimport {useLogOut} from '../../hooks/auth/useLogOut'\nimport {AuthError} from './AuthError'\nimport {LoginLayout, type LoginLayoutProps} from './LoginLayout'\n\n/**\n * @alpha\n */\nexport type LoginErrorProps = FallbackProps & LoginLayoutProps\n\n/**\n * Displays authentication error details and provides retry functionality.\n * Only handles {@link AuthError} instances - rethrows other error types.\n *\n * @alpha\n */\nexport function LoginError({\n error,\n resetErrorBoundary,\n header,\n footer,\n}: LoginErrorProps): React.ReactNode {\n if (!(error instanceof AuthError)) throw error\n const logout = useLogOut()\n\n const handleRetry = useCallback(async () => {\n await logout()\n resetErrorBoundary()\n }, [logout, resetErrorBoundary])\n\n return (\n <LoginLayout header={header} footer={footer}>\n <div className=\"sc-login-error\">\n <div className=\"sc-login-error__content\">\n <h2 className=\"sc-login-error__title\">Authentication Error</h2>\n <p className=\"sc-login-error__description\">\n Please try again or contact support if the problem persists.\n </p>\n </div>\n\n <button className=\"sc-login-error__button\" onClick={handleRetry}>\n Retry\n </button>\n </div>\n </LoginLayout>\n )\n}\n","import {AuthStateType} from '@sanity/sdk'\nimport {useMemo} from 'react'\nimport {ErrorBoundary, type FallbackProps} from 'react-error-boundary'\n\nimport {useAuthState} from '../../hooks/auth/useAuthState'\nimport {AuthError} from './AuthError'\nimport {Login} from './Login'\nimport {LoginCallback} from './LoginCallback'\nimport {LoginError, type LoginErrorProps} from './LoginError'\nimport {type LoginLayoutProps} from './LoginLayout'\n\n// Only import bridge if we're in an iframe. This assumes that the app is\n// running withing SanityOS if it is in an iframe.\nif (typeof window !== 'undefined' && window.self !== window.top) {\n import('@sanity/os/bridge')\n}\n\n/**\n * @internal\n */\ninterface AuthBoundaryProps extends LoginLayoutProps {\n /**\n * Custom component to render the login screen.\n * Receives all login layout props. Defaults to {@link Login}.\n */\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render during OAuth callback processing.\n * Receives all login layout props. Defaults to {@link LoginCallback}.\n */\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n\n /**\n * Custom component to render when authentication errors occur.\n * Receives login layout props and error boundary props. Defaults to\n * {@link LoginError}\n */\n LoginErrorComponent?: React.ComponentType<LoginErrorProps>\n}\n\n/**\n * A component that handles authentication flow and error boundaries for a\n * protected section of the application.\n *\n * @remarks\n * This component manages different authentication states and renders the\n * appropriate components based on that state.\n *\n * @example\n * ```tsx\n * function App() {\n * return (\n * <AuthBoundary header={<MyLogo />}>\n * <ProtectedContent />\n * </AuthBoundary>\n * )\n * }\n * ```\n *\n * @internal\n */\nexport function AuthBoundary({\n LoginErrorComponent = LoginError,\n ...props\n}: AuthBoundaryProps): React.ReactNode {\n const {header, footer} = props\n const FallbackComponent = useMemo(() => {\n return function LoginComponentWithLayoutProps(fallbackProps: FallbackProps) {\n return <LoginErrorComponent {...fallbackProps} header={header} footer={footer} />\n }\n }, [header, footer, LoginErrorComponent])\n\n return (\n <ErrorBoundary FallbackComponent={FallbackComponent}>\n <AuthSwitch {...props} />\n </ErrorBoundary>\n )\n}\n\ninterface AuthSwitchProps extends LoginLayoutProps {\n LoginComponent?: React.ComponentType<LoginLayoutProps>\n CallbackComponent?: React.ComponentType<LoginLayoutProps>\n}\n\nfunction AuthSwitch({\n LoginComponent = Login,\n CallbackComponent = LoginCallback,\n children,\n ...props\n}: AuthSwitchProps) {\n const authState = useAuthState()\n\n switch (authState.type) {\n case AuthStateType.ERROR: {\n throw new AuthError(authState.error)\n }\n case AuthStateType.LOGGING_IN: {\n return <CallbackComponent {...props} />\n }\n case AuthStateType.LOGGED_IN: {\n return children\n }\n default: {\n return <LoginComponent {...props} />\n }\n }\n}\n","import {createSanityInstance, type SanityConfig} from '@sanity/sdk'\nimport {type ReactElement} from 'react'\n\nimport {SanityProvider} from '../context/SanityProvider'\nimport {AuthBoundary} from './auth/AuthBoundary'\n\n/**\n * @public\n */\nexport interface SanityAppProps {\n sanityConfig: SanityConfig\n children: React.ReactNode\n}\n\n/**\n * @public\n *\n * The SanityApp component provides your Sanity application with access to your Sanity configuration,\n * as well as application context and state which is used by the Sanity React hooks. Your application\n * must be wrapped with the SanityApp component to function properly.\n *\n * @param props - Your Sanity configuration and the React children to render\n * @returns Your Sanity application, integrated with your Sanity configuration and application context\n *\n * @example\n * ```\n * import { SanityApp } from '@sanity/sdk-react\n *\n * import MyAppRoot from './Root'\n *\n * const mySanityConfig = {\n * procectId: 'my-project-id',\n * dataset: 'production',\n * }\n *\n * export default function MyApp() {\n * return (\n * <SanityApp sanityConfig={mySanityConfig}>\n * <MyAppRoot />\n * </SanityApp>\n * )\n * }\n * ```\n */\nexport function SanityApp({sanityConfig, children}: SanityAppProps): ReactElement {\n const sanityInstance = createSanityInstance(sanityConfig)\n\n return (\n <SanityProvider sanityInstance={sanityInstance}>\n <AuthBoundary>{children}</AuthBoundary>\n </SanityProvider>\n )\n}\n"],"names":[],"mappings":";;;;;;;AAWO,MAAM,kBAAkB,MAAM;AAAA,EACnC,YAAY,OAAgB;AAExB,WAAO,SAAU,YACf,SACF,aAAa,SACb,OAAO,MAAM,WAAY,WAEzB,MAAM,MAAM,OAAO,IAEnB,MAAM,GAGR,KAAK,QAAQ;AAAA,EAAA;AAEjB;ACvBA,MAAM,QAAQ;AAAA,EACZ;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EACT;AAAA,EACA;AAAA,IACE,KAAK;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,EAAA;AAEX;AAQO,SAAS,cAA+B;AAE3C,SAAA,qBAAC,OAAI,EAAA,WAAU,mBACb,UAAA;AAAA,IAAC,oBAAA,YAAA,EAAW,WAAU,wBAAwB,CAAA;AAAA,IAE7C,oBAAA,MAAA,EAAG,WAAU,0BACX,UAAM,MAAA,IAAI,CAAC,SACT,oBAAA,UAAA,EACC,UAAC,oBAAA,MAAA,EAAG,WAAU,yBACZ,UAAA,oBAAC,KAAE,EAAA,MAAM,KAAK,KAAK,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AAC1E,WAAQ,oBAAA,qBAAA,EAAqB,GAAG,eAAe,QAAgB,QAAgB;AAAA,EAEhF,GAAA,CAAC,QAAQ,QAAQ,mBAAmB,CAAC;AAExC,6BACG,eAAc,EAAA,mBACb,8BAAC,YAAY,EAAA,GAAG,MAAO,CAAA,GACzB;AAEJ;AAOA,SAAS,WAAW;AAAA,EAClB,iBAAiB;AAAA,EACjB,oBAAoB;AAAA,EACpB;AAAA,EACA,GAAG;AACL,GAAoB;AAClB,QAAM,YAAY,aAAa;AAE/B,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK,cAAc;AACX,YAAA,IAAI,UAAU,UAAU,KAAK;AAAA,IAErC,KAAK,cAAc;AACV,aAAA,oBAAC,mBAAmB,EAAA,GAAG,MAAO,CAAA;AAAA,IAEvC,KAAK,cAAc;AACV,aAAA;AAAA,IAET;AACS,aAAA,oBAAC,gBAAgB,EAAA,GAAG,MAAO,CAAA;AAAA,EAAA;AAGxC;AC/DO,SAAS,UAAU,EAAC,cAAc,YAAyC;AAC1E,QAAA,iBAAiB,qBAAqB,YAAY;AAExD,6BACG,gBAAe,EAAA,gBACd,UAAC,oBAAA,cAAA,EAAc,SAAS,CAAA,GAC1B;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/sdk-react",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.11",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Sanity SDK React toolkit for Content OS",
|
|
6
6
|
"keywords": [
|
|
@@ -70,10 +70,9 @@
|
|
|
70
70
|
"@sanity/logos": "^2.1.13",
|
|
71
71
|
"@sanity/os": "^0.2.0",
|
|
72
72
|
"@sanity/types": "^3.67.1",
|
|
73
|
-
"@sanity/ui": "^2.8.19",
|
|
74
73
|
"react-error-boundary": "^4.1.2",
|
|
75
74
|
"rxjs": "^7.8.1",
|
|
76
|
-
"@sanity/sdk": "0.0.0-alpha.
|
|
75
|
+
"@sanity/sdk": "0.0.0-alpha.10"
|
|
77
76
|
},
|
|
78
77
|
"devDependencies": {
|
|
79
78
|
"@sanity/client": "^6.27.2",
|
|
@@ -96,8 +95,8 @@
|
|
|
96
95
|
"vitest": "^3.0.5",
|
|
97
96
|
"@repo/config-eslint": "0.0.0",
|
|
98
97
|
"@repo/config-test": "0.0.1",
|
|
99
|
-
"@repo/
|
|
100
|
-
"@repo/
|
|
98
|
+
"@repo/tsconfig": "0.0.1",
|
|
99
|
+
"@repo/package.config": "0.0.1"
|
|
101
100
|
},
|
|
102
101
|
"peerDependencies": {
|
|
103
102
|
"react": "^18.0.0 || ^19.0.0",
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {AuthStateType, createSanityInstance} from '@sanity/sdk'
|
|
2
|
+
import {SanityProvider} from '@sanity/sdk-react/context'
|
|
3
|
+
import {useAuthState, useLoginUrls} from '@sanity/sdk-react/hooks'
|
|
4
|
+
import {render, screen} from '@testing-library/react'
|
|
5
|
+
import React from 'react'
|
|
6
|
+
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
7
|
+
|
|
8
|
+
import {LoginLinks} from './LoginLinks'
|
|
9
|
+
|
|
10
|
+
// Mock the hooks and SDK functions
|
|
11
|
+
vi.mock('../../hooks/auth/useLoginUrls', () => ({
|
|
12
|
+
useLoginUrls: vi.fn(() => [
|
|
13
|
+
{
|
|
14
|
+
name: 'google',
|
|
15
|
+
title: 'Google',
|
|
16
|
+
url: 'https://google.com/auth',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: 'github',
|
|
20
|
+
title: 'GitHub',
|
|
21
|
+
url: 'https://github.com/auth',
|
|
22
|
+
},
|
|
23
|
+
]),
|
|
24
|
+
}))
|
|
25
|
+
vi.mock('@sanity/sdk', async () => {
|
|
26
|
+
const actual = await vi.importActual('@sanity/sdk')
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
...actual,
|
|
30
|
+
tradeTokenForSession: vi.fn(),
|
|
31
|
+
getSidUrlHash: vi.fn().mockReturnValue(null),
|
|
32
|
+
getSidUrlSearch: vi.fn(),
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
vi.mock('../../hooks/auth/useAuthState', () => ({
|
|
37
|
+
useAuthState: vi.fn(() => 'logged-out'),
|
|
38
|
+
}))
|
|
39
|
+
|
|
40
|
+
vi.mock('../../hooks/auth/useHandleCallback', () => ({
|
|
41
|
+
useHandleCallback: vi.fn(),
|
|
42
|
+
}))
|
|
43
|
+
|
|
44
|
+
describe('LoginLinks', () => {
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.clearAllMocks()
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const sanityInstance = createSanityInstance({projectId: 'test-project-id', dataset: 'production'})
|
|
50
|
+
const renderWithWrappers = (ui: React.ReactElement) => {
|
|
51
|
+
return render(<SanityProvider sanityInstance={sanityInstance}>{ui}</SanityProvider>)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
it('renders auth provider links correctly when not authenticated', () => {
|
|
55
|
+
vi.mocked(useAuthState).mockReturnValue({
|
|
56
|
+
type: AuthStateType.LOGGED_OUT,
|
|
57
|
+
isDestroyingSession: false,
|
|
58
|
+
})
|
|
59
|
+
renderWithWrappers(<LoginLinks />)
|
|
60
|
+
|
|
61
|
+
expect(screen.getByText('Choose login provider')).toBeInTheDocument()
|
|
62
|
+
|
|
63
|
+
const authProviders = useLoginUrls()
|
|
64
|
+
authProviders.forEach((provider) => {
|
|
65
|
+
const button = screen.getByRole('link', {name: provider.title})
|
|
66
|
+
expect(button).toBeInTheDocument()
|
|
67
|
+
expect(button).toHaveAttribute('href', provider.url)
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('shows loading state while logging in', () => {
|
|
72
|
+
vi.mocked(useAuthState).mockReturnValue({
|
|
73
|
+
type: AuthStateType.LOGGING_IN,
|
|
74
|
+
isExchangingToken: false,
|
|
75
|
+
})
|
|
76
|
+
renderWithWrappers(<LoginLinks />)
|
|
77
|
+
|
|
78
|
+
expect(screen.getByText('Logging in...')).toBeInTheDocument()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('shows success message when logged in', () => {
|
|
82
|
+
vi.mocked(useAuthState).mockReturnValue({
|
|
83
|
+
type: AuthStateType.LOGGED_IN,
|
|
84
|
+
token: 'test-token',
|
|
85
|
+
currentUser: null,
|
|
86
|
+
})
|
|
87
|
+
renderWithWrappers(<LoginLinks />)
|
|
88
|
+
|
|
89
|
+
expect(screen.getByText('You are logged in')).toBeInTheDocument()
|
|
90
|
+
})
|
|
91
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {type ReactElement} from 'react'
|
|
2
|
+
|
|
3
|
+
import {useAuthState} from '../../hooks/auth/useAuthState'
|
|
4
|
+
import {useHandleCallback} from '../../hooks/auth/useHandleCallback'
|
|
5
|
+
import {useLoginUrls} from '../../hooks/auth/useLoginUrls'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Component that handles Sanity authentication flow and renders login provider options
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*
|
|
12
|
+
* @returns Rendered component
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* The component handles three states:
|
|
16
|
+
* 1. Loading state during token exchange
|
|
17
|
+
* 2. Success state after successful authentication
|
|
18
|
+
* 3. Provider selection UI when not authenticated
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* const config = { projectId: 'your-project-id', dataset: 'production' }
|
|
23
|
+
* return <LoginLinks sanityInstance={config} />
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const LoginLinks = (): ReactElement => {
|
|
27
|
+
const loginUrls = useLoginUrls()
|
|
28
|
+
const authState = useAuthState()
|
|
29
|
+
useHandleCallback()
|
|
30
|
+
|
|
31
|
+
if (authState.type === 'logging-in') {
|
|
32
|
+
return <div className="sc-login-links__logging-in">Logging in...</div>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Show success state after authentication
|
|
36
|
+
if (authState.type === 'logged-in') {
|
|
37
|
+
return <div className="sc-login-links__logged-in">You are logged in</div>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Render provider selection UI
|
|
42
|
+
*/
|
|
43
|
+
return (
|
|
44
|
+
<div className="sc-login-links">
|
|
45
|
+
<h2 className="sc-login-links__title">Choose login provider</h2>
|
|
46
|
+
|
|
47
|
+
<ul className="sc-login-links__list">
|
|
48
|
+
{loginUrls.map((provider, index) => (
|
|
49
|
+
<li key={`${provider.url}_${index}`} className="sc-login-links__item">
|
|
50
|
+
<a href={provider.url} className="sc-login-links__link">
|
|
51
|
+
{provider.title}
|
|
52
|
+
</a>
|
|
53
|
+
</li>
|
|
54
|
+
))}
|
|
55
|
+
</ul>
|
|
56
|
+
</div>
|
|
57
|
+
)
|
|
58
|
+
}
|
|
@@ -54,7 +54,7 @@ describe('AuthBoundary', () => {
|
|
|
54
54
|
renderWithWrappers(<AuthBoundary>Protected Content</AuthBoundary>)
|
|
55
55
|
|
|
56
56
|
// The login screen should show "Choose login provider" by default
|
|
57
|
-
expect(screen.getByText('Choose login provider
|
|
57
|
+
expect(screen.getByText('Choose login provider')).toBeInTheDocument()
|
|
58
58
|
expect(screen.queryByText('Protected Content')).not.toBeInTheDocument()
|
|
59
59
|
})
|
|
60
60
|
|
|
@@ -14,7 +14,7 @@ vi.mock('../../hooks/auth/useLoginUrls', () => ({
|
|
|
14
14
|
describe('Login', () => {
|
|
15
15
|
it('renders login providers', () => {
|
|
16
16
|
renderWithWrappers(<Login />)
|
|
17
|
-
expect(screen.getByText('Choose login provider
|
|
17
|
+
expect(screen.getByText('Choose login provider')).toBeInTheDocument()
|
|
18
18
|
expect(screen.getByRole('link', {name: 'Provider A'})).toHaveAttribute(
|
|
19
19
|
'href',
|
|
20
20
|
'https://provider-a.com/auth',
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {Box, Button, Flex, Heading, Spinner, Stack} from '@sanity/ui'
|
|
2
1
|
import {type JSX, Suspense} from 'react'
|
|
3
2
|
|
|
4
3
|
import {useLoginUrls} from '../../hooks/auth/useLoginUrls'
|
|
@@ -14,21 +13,13 @@ import {LoginLayout, type LoginLayoutProps} from './LoginLayout'
|
|
|
14
13
|
export function Login({header, footer}: LoginLayoutProps): JSX.Element {
|
|
15
14
|
return (
|
|
16
15
|
<LoginLayout header={header} footer={footer}>
|
|
17
|
-
<
|
|
18
|
-
Choose login provider
|
|
19
|
-
</Heading>
|
|
16
|
+
<div className="sc-login">
|
|
17
|
+
<h1 className="sc-login__title">Choose login provider</h1>
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
<Spinner />
|
|
26
|
-
</Flex>
|
|
27
|
-
</Box>
|
|
28
|
-
}
|
|
29
|
-
>
|
|
30
|
-
<Providers />
|
|
31
|
-
</Suspense>
|
|
19
|
+
<Suspense fallback={<div className="sc-login__loading">Loading…</div>}>
|
|
20
|
+
<Providers />
|
|
21
|
+
</Suspense>
|
|
22
|
+
</div>
|
|
32
23
|
</LoginLayout>
|
|
33
24
|
)
|
|
34
25
|
}
|
|
@@ -37,18 +28,12 @@ function Providers() {
|
|
|
37
28
|
const loginUrls = useLoginUrls()
|
|
38
29
|
|
|
39
30
|
return (
|
|
40
|
-
<
|
|
31
|
+
<div className="sc-login-providers">
|
|
41
32
|
{loginUrls.map(({title, url}) => (
|
|
42
|
-
<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
href={url}
|
|
46
|
-
mode="ghost"
|
|
47
|
-
text={title}
|
|
48
|
-
textAlign="center"
|
|
49
|
-
fontSize={2}
|
|
50
|
-
></Button>
|
|
33
|
+
<a key={url} href={url}>
|
|
34
|
+
{title}
|
|
35
|
+
</a>
|
|
51
36
|
))}
|
|
52
|
-
</
|
|
37
|
+
</div>
|
|
53
38
|
)
|
|
54
39
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {Flex, Heading, Spinner} from '@sanity/ui'
|
|
2
1
|
import {useEffect} from 'react'
|
|
3
2
|
|
|
4
3
|
import {useHandleCallback} from '../../hooks/auth/useHandleCallback'
|
|
@@ -28,12 +27,10 @@ export function LoginCallback({header, footer}: LoginLayoutProps): React.ReactNo
|
|
|
28
27
|
|
|
29
28
|
return (
|
|
30
29
|
<LoginLayout header={header} footer={footer}>
|
|
31
|
-
<
|
|
32
|
-
Logging you in
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
<Spinner />
|
|
36
|
-
</Flex>
|
|
30
|
+
<div className="sc-login-callback">
|
|
31
|
+
<h1 className="sc-login-callback__title">Logging you in…</h1>
|
|
32
|
+
<div className="sc-login-callback__loading">Loading…</div>
|
|
33
|
+
</div>
|
|
37
34
|
</LoginLayout>
|
|
38
35
|
)
|
|
39
36
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {Button, Heading, Stack, Text} from '@sanity/ui'
|
|
2
1
|
import {useCallback} from 'react'
|
|
3
2
|
import {type FallbackProps} from 'react-error-boundary'
|
|
4
3
|
|
|
@@ -33,13 +32,18 @@ export function LoginError({
|
|
|
33
32
|
|
|
34
33
|
return (
|
|
35
34
|
<LoginLayout header={header} footer={footer}>
|
|
36
|
-
<
|
|
37
|
-
<
|
|
38
|
-
Authentication Error
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
<div className="sc-login-error">
|
|
36
|
+
<div className="sc-login-error__content">
|
|
37
|
+
<h2 className="sc-login-error__title">Authentication Error</h2>
|
|
38
|
+
<p className="sc-login-error__description">
|
|
39
|
+
Please try again or contact support if the problem persists.
|
|
40
|
+
</p>
|
|
41
|
+
</div>
|
|
42
|
+
|
|
43
|
+
<button className="sc-login-error__button" onClick={handleRetry}>
|
|
44
|
+
Retry
|
|
45
|
+
</button>
|
|
46
|
+
</div>
|
|
43
47
|
</LoginLayout>
|
|
44
48
|
)
|
|
45
49
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {SanityLogo} from '@sanity/logos'
|
|
2
|
-
import {
|
|
2
|
+
import {Fragment} from 'react'
|
|
3
3
|
|
|
4
4
|
const LINKS = [
|
|
5
5
|
{
|
|
@@ -32,27 +32,20 @@ const LINKS = [
|
|
|
32
32
|
*/
|
|
33
33
|
export function LoginFooter(): React.ReactNode {
|
|
34
34
|
return (
|
|
35
|
-
<
|
|
36
|
-
<
|
|
37
|
-
<SanityLogo />
|
|
38
|
-
</Flex>
|
|
35
|
+
<div className="sc-login-footer">
|
|
36
|
+
<SanityLogo className="sc-login-footer__logo" />
|
|
39
37
|
|
|
40
|
-
<
|
|
41
|
-
|
|
42
|
-
{
|
|
43
|
-
<
|
|
44
|
-
<a
|
|
45
|
-
href={link.url}
|
|
46
|
-
target="_blank"
|
|
47
|
-
rel="noopener noreferrer"
|
|
48
|
-
style={{color: 'inherit'}}
|
|
49
|
-
>
|
|
38
|
+
<ul className="sc-login-footer__links">
|
|
39
|
+
{LINKS.map((link) => (
|
|
40
|
+
<Fragment key={link.title}>
|
|
41
|
+
<li className="sc-login-footer__link">
|
|
42
|
+
<a href={link.url} target="_blank" rel="noopener noreferrer">
|
|
50
43
|
{link.title}
|
|
51
44
|
</a>
|
|
52
|
-
</
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
</
|
|
56
|
-
</
|
|
45
|
+
</li>
|
|
46
|
+
</Fragment>
|
|
47
|
+
))}
|
|
48
|
+
</ul>
|
|
49
|
+
</div>
|
|
57
50
|
)
|
|
58
51
|
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {Card, Container} from '@sanity/ui'
|
|
2
|
-
|
|
3
1
|
import {LoginFooter} from './LoginFooter'
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* @alpha
|
|
7
|
-
* @internal
|
|
8
5
|
*/
|
|
9
6
|
export interface LoginLayoutProps {
|
|
10
7
|
/** Optional header content rendered at top of card */
|
|
@@ -57,14 +54,16 @@ export function LoginLayout({
|
|
|
57
54
|
header,
|
|
58
55
|
}: LoginLayoutProps): React.ReactNode {
|
|
59
56
|
return (
|
|
60
|
-
<
|
|
61
|
-
<
|
|
62
|
-
|
|
57
|
+
<div className="sc-login-layout">
|
|
58
|
+
<div className="sc-login-layout__container">
|
|
59
|
+
<div className="sc-login-layout__card">
|
|
60
|
+
{header && <div className="sc-login-layout__card-header">{header}</div>}
|
|
63
61
|
|
|
64
|
-
|
|
62
|
+
{children && <div className="sc-login-layout__card-body">{children}</div>}
|
|
63
|
+
</div>
|
|
65
64
|
|
|
66
65
|
{footer}
|
|
67
|
-
</
|
|
68
|
-
</
|
|
66
|
+
</div>
|
|
67
|
+
</div>
|
|
69
68
|
)
|
|
70
69
|
}
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import {createSanityInstance} from '@sanity/sdk'
|
|
2
|
-
import {ThemeProvider} from '@sanity/ui'
|
|
3
|
-
import {buildTheme} from '@sanity/ui/theme'
|
|
4
2
|
import {render, type RenderResult} from '@testing-library/react'
|
|
5
3
|
import React from 'react'
|
|
6
4
|
|
|
7
5
|
import {SanityProvider} from '../../context/SanityProvider'
|
|
8
6
|
|
|
9
7
|
const sanityInstance = createSanityInstance({projectId: 'test-project-id', dataset: 'production'})
|
|
10
|
-
const theme = buildTheme()
|
|
11
8
|
|
|
12
9
|
export const renderWithWrappers = (ui: React.ReactElement): RenderResult => {
|
|
13
|
-
return render(
|
|
14
|
-
<SanityProvider sanityInstance={sanityInstance}>
|
|
15
|
-
<ThemeProvider theme={theme}>{ui}</ThemeProvider>
|
|
16
|
-
</SanityProvider>,
|
|
17
|
-
)
|
|
10
|
+
return render(<SanityProvider sanityInstance={sanityInstance}>{ui}</SanityProvider>)
|
|
18
11
|
}
|