@stytch/nextjs 0.0.2-0
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/LICENSE +17 -0
- package/README.md +65 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.esm.d.ts +77 -0
- package/dist/index.esm.js +161 -0
- package/dist/index.headless.d.ts +3 -0
- package/dist/index.headless.esm.d.ts +3 -0
- package/dist/index.headless.esm.js +48 -0
- package/dist/index.headless.js +52 -0
- package/dist/index.js +176 -0
- package/dist/index.ui.d.ts +3 -0
- package/dist/index.ui.esm.d.ts +3 -0
- package/dist/index.ui.esm.js +48 -0
- package/dist/index.ui.js +52 -0
- package/headless/package.json +6 -0
- package/package.json +35 -0
- package/ui/package.json +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2021 stytchauth
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all
|
|
10
|
+
copies or substantial portions of the Software.
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
12
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
14
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
16
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
17
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @stytch/nextjs
|
|
2
|
+
|
|
3
|
+
Stytch's Next.js Library
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
With `npm`
|
|
8
|
+
`npm install @stytch/nextjs @stytch/vanilla-js --save`
|
|
9
|
+
|
|
10
|
+
## Documentation
|
|
11
|
+
|
|
12
|
+
For full documentation please refer to Stytch's javascript SDK documentation on https://stytch.com/docs/sdks.
|
|
13
|
+
|
|
14
|
+
## Example Usage
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
import { StytchProvider } from '@stytch/nextjs';
|
|
18
|
+
import { createStytchUIClient } from '@stytch/nextjs/ui';
|
|
19
|
+
|
|
20
|
+
const stytch = new createStytchUIClient('public-token-<find yours in the stytch dashboard>');
|
|
21
|
+
|
|
22
|
+
// Wrap your App in the StytchProvider
|
|
23
|
+
ReactDOM.render(
|
|
24
|
+
<StytchProvider stytch={stytch}>
|
|
25
|
+
<App />
|
|
26
|
+
</StytchProvider>,
|
|
27
|
+
document.getElementById('root'),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Now use Stytch in your components
|
|
31
|
+
const App = () => {
|
|
32
|
+
const stytchProps = {
|
|
33
|
+
config: {
|
|
34
|
+
products: ['emailMagicLinks'],
|
|
35
|
+
emailMagicLinksOptions: {
|
|
36
|
+
loginRedirectURL: 'https://example.com/authenticate',
|
|
37
|
+
loginExpirationMinutes: 30,
|
|
38
|
+
signupRedirectURL: 'https://example.com/authenticate',
|
|
39
|
+
signupExpirationMinutes: 30,
|
|
40
|
+
createUserAsPending: true,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
styles: {
|
|
44
|
+
fontFamily: '"Helvetica New", Helvetica, sans-serif',
|
|
45
|
+
width: '321px',
|
|
46
|
+
primaryColor: '#0577CA',
|
|
47
|
+
},
|
|
48
|
+
callbacks: {
|
|
49
|
+
onEvent: (message) => console.log(message),
|
|
50
|
+
onSuccess: (message) => console.log(message),
|
|
51
|
+
onError: (message) => console.log(message),
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div id="login">
|
|
57
|
+
<Stytch config={stytchProps.config} styles={stytchProps.styles} callbacks={stytchProps.callbacks} />
|
|
58
|
+
</div>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Typescript Support
|
|
64
|
+
|
|
65
|
+
There are built in typescript definitions in the npm package.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { ReactNode } from "react";
|
|
4
|
+
import { User, Session, StytchUIClient, Callbacks, Config } from "@stytch/vanilla-js";
|
|
5
|
+
import { StytchHeadlessClient } from "@stytch/vanilla-js/headless";
|
|
6
|
+
/**
|
|
7
|
+
* TODO: How much of this package can be reused from stytch-react?
|
|
8
|
+
*/
|
|
9
|
+
type StytchClient = StytchUIClient | StytchHeadlessClient;
|
|
10
|
+
type SWRUser = {
|
|
11
|
+
user: null;
|
|
12
|
+
fromCache: false;
|
|
13
|
+
isInitialized: false;
|
|
14
|
+
} | {
|
|
15
|
+
user: User | null;
|
|
16
|
+
fromCache: true;
|
|
17
|
+
isInitialized: true;
|
|
18
|
+
} | {
|
|
19
|
+
user: User | null;
|
|
20
|
+
fromCache: false;
|
|
21
|
+
isInitialized: true;
|
|
22
|
+
};
|
|
23
|
+
type SWRSession = {
|
|
24
|
+
session: null;
|
|
25
|
+
fromCache: false;
|
|
26
|
+
isInitialized: false;
|
|
27
|
+
} | {
|
|
28
|
+
session: Session | null;
|
|
29
|
+
fromCache: true;
|
|
30
|
+
isInitialized: true;
|
|
31
|
+
} | {
|
|
32
|
+
session: Session | null;
|
|
33
|
+
fromCache: false;
|
|
34
|
+
isInitialized: true;
|
|
35
|
+
};
|
|
36
|
+
declare const useStytchUser: () => SWRUser;
|
|
37
|
+
declare const useStytchSession: () => SWRSession;
|
|
38
|
+
declare const useStytch: () => StytchClient;
|
|
39
|
+
declare const withStytch: <T extends object>(Component: React.ComponentType<T & {
|
|
40
|
+
stytch: StytchClient;
|
|
41
|
+
}>) => React.ComponentType<T>;
|
|
42
|
+
declare const withStytchUser: <T extends object>(Component: React.ComponentType<T & {
|
|
43
|
+
stytchUser: User | null;
|
|
44
|
+
stytchUserIsInitialized: boolean;
|
|
45
|
+
stytchUserIsFromCache: boolean;
|
|
46
|
+
}>) => React.ComponentType<T>;
|
|
47
|
+
declare const withStytchSession: <T extends object>(Component: React.ComponentType<T & {
|
|
48
|
+
stytchSession: Session | null;
|
|
49
|
+
stytchSessionIsInitialized: boolean;
|
|
50
|
+
stytchSessionIsFromCache: boolean;
|
|
51
|
+
}>) => React.ComponentType<T>;
|
|
52
|
+
type StytchProviderProps = {
|
|
53
|
+
stytch: StytchClient;
|
|
54
|
+
children?: ReactNode;
|
|
55
|
+
};
|
|
56
|
+
declare const StytchProvider: ({ stytch, children }: StytchProviderProps) => JSX.Element;
|
|
57
|
+
/**
|
|
58
|
+
* Stytch JS React Component
|
|
59
|
+
*
|
|
60
|
+
* [Documentation](https://stytch.com/docs/javascript-sdk)
|
|
61
|
+
*/
|
|
62
|
+
declare const Stytch: ({ config, styles, callbacks }: {
|
|
63
|
+
config: Config;
|
|
64
|
+
styles?: Partial<{
|
|
65
|
+
fontFamily: string;
|
|
66
|
+
width: string;
|
|
67
|
+
primaryColor: string;
|
|
68
|
+
primaryTextColor: string;
|
|
69
|
+
secondaryTextColor: string;
|
|
70
|
+
lightGrey: string;
|
|
71
|
+
darkGrey: string;
|
|
72
|
+
hideHeaderText: boolean;
|
|
73
|
+
}> | undefined;
|
|
74
|
+
callbacks?: Callbacks | undefined;
|
|
75
|
+
}) => JSX.Element;
|
|
76
|
+
export { StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser, Stytch };
|
|
77
|
+
export type { StytchProviderProps };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { ReactNode } from "react";
|
|
4
|
+
import { User, Session, StytchUIClient, Callbacks, Config } from "@stytch/vanilla-js";
|
|
5
|
+
import { StytchHeadlessClient } from "@stytch/vanilla-js/headless";
|
|
6
|
+
/**
|
|
7
|
+
* TODO: How much of this package can be reused from stytch-react?
|
|
8
|
+
*/
|
|
9
|
+
type StytchClient = StytchUIClient | StytchHeadlessClient;
|
|
10
|
+
type SWRUser = {
|
|
11
|
+
user: null;
|
|
12
|
+
fromCache: false;
|
|
13
|
+
isInitialized: false;
|
|
14
|
+
} | {
|
|
15
|
+
user: User | null;
|
|
16
|
+
fromCache: true;
|
|
17
|
+
isInitialized: true;
|
|
18
|
+
} | {
|
|
19
|
+
user: User | null;
|
|
20
|
+
fromCache: false;
|
|
21
|
+
isInitialized: true;
|
|
22
|
+
};
|
|
23
|
+
type SWRSession = {
|
|
24
|
+
session: null;
|
|
25
|
+
fromCache: false;
|
|
26
|
+
isInitialized: false;
|
|
27
|
+
} | {
|
|
28
|
+
session: Session | null;
|
|
29
|
+
fromCache: true;
|
|
30
|
+
isInitialized: true;
|
|
31
|
+
} | {
|
|
32
|
+
session: Session | null;
|
|
33
|
+
fromCache: false;
|
|
34
|
+
isInitialized: true;
|
|
35
|
+
};
|
|
36
|
+
declare const useStytchUser: () => SWRUser;
|
|
37
|
+
declare const useStytchSession: () => SWRSession;
|
|
38
|
+
declare const useStytch: () => StytchClient;
|
|
39
|
+
declare const withStytch: <T extends object>(Component: React.ComponentType<T & {
|
|
40
|
+
stytch: StytchClient;
|
|
41
|
+
}>) => React.ComponentType<T>;
|
|
42
|
+
declare const withStytchUser: <T extends object>(Component: React.ComponentType<T & {
|
|
43
|
+
stytchUser: User | null;
|
|
44
|
+
stytchUserIsInitialized: boolean;
|
|
45
|
+
stytchUserIsFromCache: boolean;
|
|
46
|
+
}>) => React.ComponentType<T>;
|
|
47
|
+
declare const withStytchSession: <T extends object>(Component: React.ComponentType<T & {
|
|
48
|
+
stytchSession: Session | null;
|
|
49
|
+
stytchSessionIsInitialized: boolean;
|
|
50
|
+
stytchSessionIsFromCache: boolean;
|
|
51
|
+
}>) => React.ComponentType<T>;
|
|
52
|
+
type StytchProviderProps = {
|
|
53
|
+
stytch: StytchClient;
|
|
54
|
+
children?: ReactNode;
|
|
55
|
+
};
|
|
56
|
+
declare const StytchProvider: ({ stytch, children }: StytchProviderProps) => JSX.Element;
|
|
57
|
+
/**
|
|
58
|
+
* Stytch JS React Component
|
|
59
|
+
*
|
|
60
|
+
* [Documentation](https://stytch.com/docs/javascript-sdk)
|
|
61
|
+
*/
|
|
62
|
+
declare const Stytch: ({ config, styles, callbacks }: {
|
|
63
|
+
config: Config;
|
|
64
|
+
styles?: Partial<{
|
|
65
|
+
fontFamily: string;
|
|
66
|
+
width: string;
|
|
67
|
+
primaryColor: string;
|
|
68
|
+
primaryTextColor: string;
|
|
69
|
+
secondaryTextColor: string;
|
|
70
|
+
lightGrey: string;
|
|
71
|
+
darkGrey: string;
|
|
72
|
+
hideHeaderText: boolean;
|
|
73
|
+
}> | undefined;
|
|
74
|
+
callbacks?: Callbacks | undefined;
|
|
75
|
+
}) => JSX.Element;
|
|
76
|
+
export { StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser, Stytch };
|
|
77
|
+
export type { StytchProviderProps };
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import React, { useRef, useState, useEffect, useCallback, createContext, useContext, useMemo, useLayoutEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const noProviderError = (item) => `${item} can only be used inside <StytchProvider>.`;
|
|
4
|
+
const noHeadlessClientError = `Tried to create a Stytch Login UI element using the Stytch Headless SDK.
|
|
5
|
+
You must use the UI SDK to use UI elements.
|
|
6
|
+
Please make sure you are importing createStytchHeadlessClient from @stytch/nextjs/ui and not from @stytch/nextjs/headless.`;
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
function invariant(cond, message) {
|
|
10
|
+
if (!cond)
|
|
11
|
+
throw new Error(message);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// useState can cause memory leaks if it is set after the component unmounted. For example, if it is
|
|
15
|
+
// set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
|
|
16
|
+
const useAsyncState = (initialState) => {
|
|
17
|
+
const isMounted = useRef(true);
|
|
18
|
+
const [state, setState] = useState(initialState);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
isMounted.current = true;
|
|
21
|
+
return () => {
|
|
22
|
+
isMounted.current = false;
|
|
23
|
+
};
|
|
24
|
+
}, []);
|
|
25
|
+
const setStateAction = useCallback((newState) => {
|
|
26
|
+
isMounted.current && setState(newState);
|
|
27
|
+
}, []);
|
|
28
|
+
return [state, setStateAction];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
const isStytchSSRProxy = (proxy) => {
|
|
34
|
+
return !!proxy[SSRStubKey];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const initialUser = {
|
|
38
|
+
user: null,
|
|
39
|
+
fromCache: false,
|
|
40
|
+
isInitialized: false,
|
|
41
|
+
};
|
|
42
|
+
const initialSession = {
|
|
43
|
+
session: null,
|
|
44
|
+
fromCache: false,
|
|
45
|
+
isInitialized: false,
|
|
46
|
+
};
|
|
47
|
+
const StytchContext = createContext({ isMounted: false });
|
|
48
|
+
const StytchUserContext = createContext(initialUser);
|
|
49
|
+
const StytchSessionContext = createContext(initialSession);
|
|
50
|
+
const useIsMounted__INTERNAL = () => useContext(StytchContext).isMounted;
|
|
51
|
+
const isUIClient = (client) => {
|
|
52
|
+
return client.mount !== undefined;
|
|
53
|
+
};
|
|
54
|
+
const useStytchUser = () => {
|
|
55
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchUser'));
|
|
56
|
+
return useContext(StytchUserContext);
|
|
57
|
+
};
|
|
58
|
+
const useStytchSession = () => {
|
|
59
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchSession'));
|
|
60
|
+
return useContext(StytchSessionContext);
|
|
61
|
+
};
|
|
62
|
+
const useStytch = () => {
|
|
63
|
+
const ctx = useContext(StytchContext);
|
|
64
|
+
invariant(ctx.isMounted, noProviderError('useStytch'));
|
|
65
|
+
return ctx.client;
|
|
66
|
+
};
|
|
67
|
+
const withStytch = (Component) => {
|
|
68
|
+
const WithStytch = (props) => {
|
|
69
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytch'));
|
|
70
|
+
return React.createElement(Component, { ...props, stytch: useStytch() });
|
|
71
|
+
};
|
|
72
|
+
WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
|
|
73
|
+
return WithStytch;
|
|
74
|
+
};
|
|
75
|
+
const withStytchUser = (Component) => {
|
|
76
|
+
const WithStytchUser = (props) => {
|
|
77
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytchUser'));
|
|
78
|
+
const { user, isInitialized, fromCache } = useStytchUser();
|
|
79
|
+
return (React.createElement(Component, { ...props, stytchUser: user, stytchUserIsInitialized: isInitialized, stytchUserIsFromCache: fromCache }));
|
|
80
|
+
};
|
|
81
|
+
WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
|
|
82
|
+
return WithStytchUser;
|
|
83
|
+
};
|
|
84
|
+
const withStytchSession = (Component) => {
|
|
85
|
+
const WithStytchSession = (props) => {
|
|
86
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytchSession'));
|
|
87
|
+
const { session, isInitialized, fromCache } = useStytchSession();
|
|
88
|
+
return (React.createElement(Component, { ...props, stytchSession: session, stytchSessionIsInitialized: isInitialized, stytchSessionIsFromCache: fromCache }));
|
|
89
|
+
};
|
|
90
|
+
WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
|
|
91
|
+
return WithStytchSession;
|
|
92
|
+
};
|
|
93
|
+
const StytchProvider = ({ stytch, children }) => {
|
|
94
|
+
// invariant(!useIsMounted__INTERNAL(), providerMustBeUniqueError);
|
|
95
|
+
const ctx = useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
|
|
96
|
+
const [user, setUser] = useAsyncState(initialUser);
|
|
97
|
+
const [session, setSession] = useAsyncState(initialSession);
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (isStytchSSRProxy(stytch)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
setUser({
|
|
103
|
+
user: stytch.user.getSync(),
|
|
104
|
+
fromCache: true,
|
|
105
|
+
isInitialized: true,
|
|
106
|
+
});
|
|
107
|
+
setSession({
|
|
108
|
+
session: stytch.session.getSync(),
|
|
109
|
+
fromCache: true,
|
|
110
|
+
isInitialized: true,
|
|
111
|
+
});
|
|
112
|
+
const unsubscribeUser = stytch.user.onChange((user) => setUser({ user, fromCache: false, isInitialized: true }));
|
|
113
|
+
const unsubscribeSession = stytch.session.onChange((session) => setSession({ session, fromCache: false, isInitialized: true }));
|
|
114
|
+
return () => {
|
|
115
|
+
unsubscribeUser();
|
|
116
|
+
unsubscribeSession();
|
|
117
|
+
};
|
|
118
|
+
}, [stytch, setUser, setSession]);
|
|
119
|
+
const finalSess = session.isInitialized === user.isInitialized ? session : initialSession;
|
|
120
|
+
const finalUser = session.isInitialized === user.isInitialized ? user : initialUser;
|
|
121
|
+
return (React.createElement(StytchContext.Provider, { value: ctx },
|
|
122
|
+
React.createElement(StytchUserContext.Provider, { value: finalUser },
|
|
123
|
+
React.createElement(StytchSessionContext.Provider, { value: finalSess }, children))));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// cc https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a
|
|
127
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Stytch JS React Component
|
|
131
|
+
*
|
|
132
|
+
* [Documentation](https://stytch.com/docs/javascript-sdk)
|
|
133
|
+
*/
|
|
134
|
+
const Stytch = ({ config, styles, callbacks, }) => {
|
|
135
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('<Stytch />'));
|
|
136
|
+
const stytchClient = useStytch();
|
|
137
|
+
const containerEl = useRef(null);
|
|
138
|
+
useIsomorphicLayoutEffect(() => {
|
|
139
|
+
if (!isUIClient(stytchClient)) {
|
|
140
|
+
throw Error(noHeadlessClientError);
|
|
141
|
+
}
|
|
142
|
+
if (!containerEl.current) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (containerEl.current.id) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const randId = Math.floor(Math.random() * 1e6);
|
|
149
|
+
const elementId = `stytch-magic-link-${randId}`;
|
|
150
|
+
containerEl.current.id = elementId;
|
|
151
|
+
stytchClient.mount({
|
|
152
|
+
config,
|
|
153
|
+
callbacks,
|
|
154
|
+
elementId: `#${elementId}`,
|
|
155
|
+
styles,
|
|
156
|
+
});
|
|
157
|
+
}, [stytchClient]);
|
|
158
|
+
return React.createElement("div", { ref: containerEl });
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export { Stytch, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { StytchHeadlessClient } from '@stytch/vanilla-js/headless';
|
|
2
|
+
|
|
3
|
+
const cannotInvokeMethodOnServerError = (path) => `[Stytch] Invalid serverside function call to ${path}.
|
|
4
|
+
The Stytch Javascript SDK is intended to ony be used on the client side.
|
|
5
|
+
Make sure to wrap your API calls in a hook to ensure they are executed on the client.
|
|
6
|
+
\`\`\`
|
|
7
|
+
const myComponent = () => {
|
|
8
|
+
const stytch = useStytch();
|
|
9
|
+
// This will error out on the server.
|
|
10
|
+
stytch.magicLinks.authenticate(...);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
// This will work well
|
|
13
|
+
stytch.magicLinks.authenticate(...);
|
|
14
|
+
}, []);
|
|
15
|
+
}
|
|
16
|
+
\`\`\`
|
|
17
|
+
|
|
18
|
+
If you want to make API calls from server environments, please use the Stytch Node Library
|
|
19
|
+
https://www.npmjs.com/package/stytch.
|
|
20
|
+
`;
|
|
21
|
+
|
|
22
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
const createProxy = (path) => {
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
26
|
+
const noop = () => { };
|
|
27
|
+
return new Proxy(noop, {
|
|
28
|
+
get(target, p) {
|
|
29
|
+
if (p === SSRStubKey) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return createProxy(path + '.' + String(p));
|
|
33
|
+
},
|
|
34
|
+
apply() {
|
|
35
|
+
throw new Error(cannotInvokeMethodOnServerError(path));
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const createStytchSSRProxy = () => createProxy('stytch');
|
|
40
|
+
|
|
41
|
+
const createStytchHeadlessClient = (...args) => {
|
|
42
|
+
if (typeof window === 'undefined') {
|
|
43
|
+
return createStytchSSRProxy();
|
|
44
|
+
}
|
|
45
|
+
return new StytchHeadlessClient(...args);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export { createStytchHeadlessClient };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var headless = require('@stytch/vanilla-js/headless');
|
|
6
|
+
|
|
7
|
+
const cannotInvokeMethodOnServerError = (path) => `[Stytch] Invalid serverside function call to ${path}.
|
|
8
|
+
The Stytch Javascript SDK is intended to ony be used on the client side.
|
|
9
|
+
Make sure to wrap your API calls in a hook to ensure they are executed on the client.
|
|
10
|
+
\`\`\`
|
|
11
|
+
const myComponent = () => {
|
|
12
|
+
const stytch = useStytch();
|
|
13
|
+
// This will error out on the server.
|
|
14
|
+
stytch.magicLinks.authenticate(...);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
// This will work well
|
|
17
|
+
stytch.magicLinks.authenticate(...);
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
20
|
+
\`\`\`
|
|
21
|
+
|
|
22
|
+
If you want to make API calls from server environments, please use the Stytch Node Library
|
|
23
|
+
https://www.npmjs.com/package/stytch.
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
const createProxy = (path) => {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
30
|
+
const noop = () => { };
|
|
31
|
+
return new Proxy(noop, {
|
|
32
|
+
get(target, p) {
|
|
33
|
+
if (p === SSRStubKey) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return createProxy(path + '.' + String(p));
|
|
37
|
+
},
|
|
38
|
+
apply() {
|
|
39
|
+
throw new Error(cannotInvokeMethodOnServerError(path));
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
const createStytchSSRProxy = () => createProxy('stytch');
|
|
44
|
+
|
|
45
|
+
const createStytchHeadlessClient = (...args) => {
|
|
46
|
+
if (typeof window === 'undefined') {
|
|
47
|
+
return createStytchSSRProxy();
|
|
48
|
+
}
|
|
49
|
+
return new headless.StytchHeadlessClient(...args);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
exports.createStytchHeadlessClient = createStytchHeadlessClient;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
10
|
+
|
|
11
|
+
const noProviderError = (item) => `${item} can only be used inside <StytchProvider>.`;
|
|
12
|
+
const noHeadlessClientError = `Tried to create a Stytch Login UI element using the Stytch Headless SDK.
|
|
13
|
+
You must use the UI SDK to use UI elements.
|
|
14
|
+
Please make sure you are importing createStytchHeadlessClient from @stytch/nextjs/ui and not from @stytch/nextjs/headless.`;
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
function invariant(cond, message) {
|
|
18
|
+
if (!cond)
|
|
19
|
+
throw new Error(message);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// useState can cause memory leaks if it is set after the component unmounted. For example, if it is
|
|
23
|
+
// set after `await`, or in a `then`, `catch`, or `finally`, or in a setTimout/setInterval.
|
|
24
|
+
const useAsyncState = (initialState) => {
|
|
25
|
+
const isMounted = React.useRef(true);
|
|
26
|
+
const [state, setState] = React.useState(initialState);
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
isMounted.current = true;
|
|
29
|
+
return () => {
|
|
30
|
+
isMounted.current = false;
|
|
31
|
+
};
|
|
32
|
+
}, []);
|
|
33
|
+
const setStateAction = React.useCallback((newState) => {
|
|
34
|
+
isMounted.current && setState(newState);
|
|
35
|
+
}, []);
|
|
36
|
+
return [state, setStateAction];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
const isStytchSSRProxy = (proxy) => {
|
|
42
|
+
return !!proxy[SSRStubKey];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const initialUser = {
|
|
46
|
+
user: null,
|
|
47
|
+
fromCache: false,
|
|
48
|
+
isInitialized: false,
|
|
49
|
+
};
|
|
50
|
+
const initialSession = {
|
|
51
|
+
session: null,
|
|
52
|
+
fromCache: false,
|
|
53
|
+
isInitialized: false,
|
|
54
|
+
};
|
|
55
|
+
const StytchContext = React.createContext({ isMounted: false });
|
|
56
|
+
const StytchUserContext = React.createContext(initialUser);
|
|
57
|
+
const StytchSessionContext = React.createContext(initialSession);
|
|
58
|
+
const useIsMounted__INTERNAL = () => React.useContext(StytchContext).isMounted;
|
|
59
|
+
const isUIClient = (client) => {
|
|
60
|
+
return client.mount !== undefined;
|
|
61
|
+
};
|
|
62
|
+
const useStytchUser = () => {
|
|
63
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchUser'));
|
|
64
|
+
return React.useContext(StytchUserContext);
|
|
65
|
+
};
|
|
66
|
+
const useStytchSession = () => {
|
|
67
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('useStytchSession'));
|
|
68
|
+
return React.useContext(StytchSessionContext);
|
|
69
|
+
};
|
|
70
|
+
const useStytch = () => {
|
|
71
|
+
const ctx = React.useContext(StytchContext);
|
|
72
|
+
invariant(ctx.isMounted, noProviderError('useStytch'));
|
|
73
|
+
return ctx.client;
|
|
74
|
+
};
|
|
75
|
+
const withStytch = (Component) => {
|
|
76
|
+
const WithStytch = (props) => {
|
|
77
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytch'));
|
|
78
|
+
return React__default["default"].createElement(Component, { ...props, stytch: useStytch() });
|
|
79
|
+
};
|
|
80
|
+
WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
|
|
81
|
+
return WithStytch;
|
|
82
|
+
};
|
|
83
|
+
const withStytchUser = (Component) => {
|
|
84
|
+
const WithStytchUser = (props) => {
|
|
85
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytchUser'));
|
|
86
|
+
const { user, isInitialized, fromCache } = useStytchUser();
|
|
87
|
+
return (React__default["default"].createElement(Component, { ...props, stytchUser: user, stytchUserIsInitialized: isInitialized, stytchUserIsFromCache: fromCache }));
|
|
88
|
+
};
|
|
89
|
+
WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
|
|
90
|
+
return WithStytchUser;
|
|
91
|
+
};
|
|
92
|
+
const withStytchSession = (Component) => {
|
|
93
|
+
const WithStytchSession = (props) => {
|
|
94
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('withStytchSession'));
|
|
95
|
+
const { session, isInitialized, fromCache } = useStytchSession();
|
|
96
|
+
return (React__default["default"].createElement(Component, { ...props, stytchSession: session, stytchSessionIsInitialized: isInitialized, stytchSessionIsFromCache: fromCache }));
|
|
97
|
+
};
|
|
98
|
+
WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
|
|
99
|
+
return WithStytchSession;
|
|
100
|
+
};
|
|
101
|
+
const StytchProvider = ({ stytch, children }) => {
|
|
102
|
+
// invariant(!useIsMounted__INTERNAL(), providerMustBeUniqueError);
|
|
103
|
+
const ctx = React.useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);
|
|
104
|
+
const [user, setUser] = useAsyncState(initialUser);
|
|
105
|
+
const [session, setSession] = useAsyncState(initialSession);
|
|
106
|
+
React.useEffect(() => {
|
|
107
|
+
if (isStytchSSRProxy(stytch)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
setUser({
|
|
111
|
+
user: stytch.user.getSync(),
|
|
112
|
+
fromCache: true,
|
|
113
|
+
isInitialized: true,
|
|
114
|
+
});
|
|
115
|
+
setSession({
|
|
116
|
+
session: stytch.session.getSync(),
|
|
117
|
+
fromCache: true,
|
|
118
|
+
isInitialized: true,
|
|
119
|
+
});
|
|
120
|
+
const unsubscribeUser = stytch.user.onChange((user) => setUser({ user, fromCache: false, isInitialized: true }));
|
|
121
|
+
const unsubscribeSession = stytch.session.onChange((session) => setSession({ session, fromCache: false, isInitialized: true }));
|
|
122
|
+
return () => {
|
|
123
|
+
unsubscribeUser();
|
|
124
|
+
unsubscribeSession();
|
|
125
|
+
};
|
|
126
|
+
}, [stytch, setUser, setSession]);
|
|
127
|
+
const finalSess = session.isInitialized === user.isInitialized ? session : initialSession;
|
|
128
|
+
const finalUser = session.isInitialized === user.isInitialized ? user : initialUser;
|
|
129
|
+
return (React__default["default"].createElement(StytchContext.Provider, { value: ctx },
|
|
130
|
+
React__default["default"].createElement(StytchUserContext.Provider, { value: finalUser },
|
|
131
|
+
React__default["default"].createElement(StytchSessionContext.Provider, { value: finalSess }, children))));
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// cc https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a
|
|
135
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Stytch JS React Component
|
|
139
|
+
*
|
|
140
|
+
* [Documentation](https://stytch.com/docs/javascript-sdk)
|
|
141
|
+
*/
|
|
142
|
+
const Stytch = ({ config, styles, callbacks, }) => {
|
|
143
|
+
invariant(useIsMounted__INTERNAL(), noProviderError('<Stytch />'));
|
|
144
|
+
const stytchClient = useStytch();
|
|
145
|
+
const containerEl = React.useRef(null);
|
|
146
|
+
useIsomorphicLayoutEffect(() => {
|
|
147
|
+
if (!isUIClient(stytchClient)) {
|
|
148
|
+
throw Error(noHeadlessClientError);
|
|
149
|
+
}
|
|
150
|
+
if (!containerEl.current) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (containerEl.current.id) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const randId = Math.floor(Math.random() * 1e6);
|
|
157
|
+
const elementId = `stytch-magic-link-${randId}`;
|
|
158
|
+
containerEl.current.id = elementId;
|
|
159
|
+
stytchClient.mount({
|
|
160
|
+
config,
|
|
161
|
+
callbacks,
|
|
162
|
+
elementId: `#${elementId}`,
|
|
163
|
+
styles,
|
|
164
|
+
});
|
|
165
|
+
}, [stytchClient]);
|
|
166
|
+
return React__default["default"].createElement("div", { ref: containerEl });
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
exports.Stytch = Stytch;
|
|
170
|
+
exports.StytchProvider = StytchProvider;
|
|
171
|
+
exports.useStytch = useStytch;
|
|
172
|
+
exports.useStytchSession = useStytchSession;
|
|
173
|
+
exports.useStytchUser = useStytchUser;
|
|
174
|
+
exports.withStytch = withStytch;
|
|
175
|
+
exports.withStytchSession = withStytchSession;
|
|
176
|
+
exports.withStytchUser = withStytchUser;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { StytchUIClient } from '@stytch/vanilla-js';
|
|
2
|
+
|
|
3
|
+
const cannotInvokeMethodOnServerError = (path) => `[Stytch] Invalid serverside function call to ${path}.
|
|
4
|
+
The Stytch Javascript SDK is intended to ony be used on the client side.
|
|
5
|
+
Make sure to wrap your API calls in a hook to ensure they are executed on the client.
|
|
6
|
+
\`\`\`
|
|
7
|
+
const myComponent = () => {
|
|
8
|
+
const stytch = useStytch();
|
|
9
|
+
// This will error out on the server.
|
|
10
|
+
stytch.magicLinks.authenticate(...);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
// This will work well
|
|
13
|
+
stytch.magicLinks.authenticate(...);
|
|
14
|
+
}, []);
|
|
15
|
+
}
|
|
16
|
+
\`\`\`
|
|
17
|
+
|
|
18
|
+
If you want to make API calls from server environments, please use the Stytch Node Library
|
|
19
|
+
https://www.npmjs.com/package/stytch.
|
|
20
|
+
`;
|
|
21
|
+
|
|
22
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
const createProxy = (path) => {
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
26
|
+
const noop = () => { };
|
|
27
|
+
return new Proxy(noop, {
|
|
28
|
+
get(target, p) {
|
|
29
|
+
if (p === SSRStubKey) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return createProxy(path + '.' + String(p));
|
|
33
|
+
},
|
|
34
|
+
apply() {
|
|
35
|
+
throw new Error(cannotInvokeMethodOnServerError(path));
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const createStytchSSRProxy = () => createProxy('stytch');
|
|
40
|
+
|
|
41
|
+
const createStytchUIClient = (...args) => {
|
|
42
|
+
if (typeof window === 'undefined') {
|
|
43
|
+
return createStytchSSRProxy();
|
|
44
|
+
}
|
|
45
|
+
return new StytchUIClient(...args);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export { createStytchUIClient };
|
package/dist/index.ui.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var vanillaJs = require('@stytch/vanilla-js');
|
|
6
|
+
|
|
7
|
+
const cannotInvokeMethodOnServerError = (path) => `[Stytch] Invalid serverside function call to ${path}.
|
|
8
|
+
The Stytch Javascript SDK is intended to ony be used on the client side.
|
|
9
|
+
Make sure to wrap your API calls in a hook to ensure they are executed on the client.
|
|
10
|
+
\`\`\`
|
|
11
|
+
const myComponent = () => {
|
|
12
|
+
const stytch = useStytch();
|
|
13
|
+
// This will error out on the server.
|
|
14
|
+
stytch.magicLinks.authenticate(...);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
// This will work well
|
|
17
|
+
stytch.magicLinks.authenticate(...);
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
20
|
+
\`\`\`
|
|
21
|
+
|
|
22
|
+
If you want to make API calls from server environments, please use the Stytch Node Library
|
|
23
|
+
https://www.npmjs.com/package/stytch.
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const SSRStubKey = Symbol('__stytch_SSRStubKey');
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
const createProxy = (path) => {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
30
|
+
const noop = () => { };
|
|
31
|
+
return new Proxy(noop, {
|
|
32
|
+
get(target, p) {
|
|
33
|
+
if (p === SSRStubKey) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return createProxy(path + '.' + String(p));
|
|
37
|
+
},
|
|
38
|
+
apply() {
|
|
39
|
+
throw new Error(cannotInvokeMethodOnServerError(path));
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
const createStytchSSRProxy = () => createProxy('stytch');
|
|
44
|
+
|
|
45
|
+
const createStytchUIClient = (...args) => {
|
|
46
|
+
if (typeof window === 'undefined') {
|
|
47
|
+
return createStytchSSRProxy();
|
|
48
|
+
}
|
|
49
|
+
return new vanillaJs.StytchUIClient(...args);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
exports.createStytchUIClient = createStytchUIClient;
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stytch/nextjs",
|
|
3
|
+
"version": "0.0.2-0",
|
|
4
|
+
"description": "Stytch's official Next.js Library",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.esm.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"headless",
|
|
11
|
+
"ui"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "npm run clean && npm run compile",
|
|
15
|
+
"clean": "rm -rf ./dist",
|
|
16
|
+
"compile": "rollup -c",
|
|
17
|
+
"dev": "tsc -p tsconfig.build.json --watch",
|
|
18
|
+
"test": "jest"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Stytch",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@babel/runtime": "^7.18.6",
|
|
24
|
+
"@stytch/vanilla-js": "0.0.2-0",
|
|
25
|
+
"eslint-config-custom": "0.0.0",
|
|
26
|
+
"rollup": "^2.56.3",
|
|
27
|
+
"typescript": "^4.5.3"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@stytch/vanilla-js": "^0.0.2-0",
|
|
31
|
+
"react": ">= 17.0.2",
|
|
32
|
+
"react-dom": ">= 17.0.2"
|
|
33
|
+
},
|
|
34
|
+
"stableVersion": "0.0.1"
|
|
35
|
+
}
|