@stytch/react 0.0.0-rc.1

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 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,70 @@
1
+ # Stytch-React
2
+
3
+ Stytch's React Library
4
+
5
+ ## Install
6
+
7
+ With `npm`
8
+ `npm install @stytch/react @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/react';
18
+ import { StytchUIClient } from '@stytch/vanilla-js';
19
+
20
+ const stytch = new StytchUIClient('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
+ loginOrSignupView: {
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
+ style: {
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
58
+ publicToken={stytchProps.publicToken}
59
+ loginOrSignupView={stytchProps.loginOrSignupView}
60
+ style={stytchProps.style}
61
+ callbacks={stytchProps.callbacks}
62
+ />
63
+ </div>
64
+ );
65
+ };
66
+ ```
67
+
68
+ ## Typescript Support
69
+
70
+ There are built in typescript definitions in the npm package.
@@ -0,0 +1,32 @@
1
+ /// <reference types="react" />
2
+ import { Config, StyleConfig, User, Session, StytchUIClient } from "@stytch/vanilla-js";
3
+ import * as React from "react";
4
+ import { StytchHeadlessClient } from "@stytch/vanilla-js/headless";
5
+ /**
6
+ * Stytch JS React Component
7
+ *
8
+ * [Documentation](https://stytch.com/docs/javascript-sdk)
9
+ */
10
+ declare const Stytch: ({ config, styles }: {
11
+ config: Config;
12
+ styles: StyleConfig;
13
+ }) => JSX.Element | null;
14
+ type StytchClient = StytchUIClient | StytchHeadlessClient;
15
+ declare const useStytchUser: () => User | null;
16
+ declare const useStytchSession: () => Session | null;
17
+ declare const useStytch: () => StytchClient;
18
+ declare const withStytch: <T extends object>(Component: React.ComponentType<T & {
19
+ stytch: StytchClient;
20
+ }>) => React.ComponentType<T>;
21
+ declare const withStytchUser: <T extends object>(Component: React.ComponentType<T & {
22
+ stytchUser: User | null;
23
+ }>) => React.ComponentType<T>;
24
+ declare const withStytchSession: <T extends object>(Component: React.ComponentType<T & {
25
+ stytchSession: Session | null;
26
+ }>) => React.ComponentType<T>;
27
+ type StytchProviderProps = {
28
+ stytch: StytchClient;
29
+ children?: React.ReactNode;
30
+ };
31
+ declare const StytchProvider: ({ stytch, children }: StytchProviderProps) => JSX.Element;
32
+ export { Stytch, useStytchUser, useStytchSession, useStytch, withStytch, withStytchUser, withStytchSession, StytchProviderProps, StytchProvider };
@@ -0,0 +1,32 @@
1
+ /// <reference types="react" />
2
+ import { Config, StyleConfig, User, Session, StytchUIClient } from "@stytch/vanilla-js";
3
+ import * as React from "react";
4
+ import { StytchHeadlessClient } from "@stytch/vanilla-js/headless";
5
+ /**
6
+ * Stytch JS React Component
7
+ *
8
+ * [Documentation](https://stytch.com/docs/javascript-sdk)
9
+ */
10
+ declare const Stytch: ({ config, styles }: {
11
+ config: Config;
12
+ styles: StyleConfig;
13
+ }) => JSX.Element | null;
14
+ type StytchClient = StytchUIClient | StytchHeadlessClient;
15
+ declare const useStytchUser: () => User | null;
16
+ declare const useStytchSession: () => Session | null;
17
+ declare const useStytch: () => StytchClient;
18
+ declare const withStytch: <T extends object>(Component: React.ComponentType<T & {
19
+ stytch: StytchClient;
20
+ }>) => React.ComponentType<T>;
21
+ declare const withStytchUser: <T extends object>(Component: React.ComponentType<T & {
22
+ stytchUser: User | null;
23
+ }>) => React.ComponentType<T>;
24
+ declare const withStytchSession: <T extends object>(Component: React.ComponentType<T & {
25
+ stytchSession: Session | null;
26
+ }>) => React.ComponentType<T>;
27
+ type StytchProviderProps = {
28
+ stytch: StytchClient;
29
+ children?: React.ReactNode;
30
+ };
31
+ declare const StytchProvider: ({ stytch, children }: StytchProviderProps) => JSX.Element;
32
+ export { Stytch, useStytchUser, useStytchSession, useStytch, withStytch, withStytchUser, withStytchSession, StytchProviderProps, StytchProvider };
@@ -0,0 +1,94 @@
1
+ import * as React from 'react';
2
+ import { StytchUIClient } from '@stytch/vanilla-js';
3
+
4
+ const StytchUserContext = React.createContext(null);
5
+ const StytchSessionContext = React.createContext(null);
6
+ const StytchContext = React.createContext({});
7
+ const useStytchUser = () => {
8
+ return React.useContext(StytchUserContext);
9
+ };
10
+ const useStytchSession = () => {
11
+ return React.useContext(StytchSessionContext);
12
+ };
13
+ const useStytch = () => {
14
+ return React.useContext(StytchContext);
15
+ };
16
+ const withStytch = (Component) => {
17
+ const WithStytch = (props) => {
18
+ return React.createElement(Component, { ...props, stytch: useStytch() });
19
+ };
20
+ WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
21
+ return WithStytch;
22
+ };
23
+ const withStytchUser = (Component) => {
24
+ const WithStytchUser = (props) => {
25
+ return React.createElement(Component, { ...props, stytchUser: useStytchUser() });
26
+ };
27
+ WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
28
+ return WithStytchUser;
29
+ };
30
+ const withStytchSession = (Component) => {
31
+ const WithStytchSession = (props) => {
32
+ return React.createElement(Component, { ...props, stytchSession: useStytchSession() });
33
+ };
34
+ WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
35
+ return WithStytchSession;
36
+ };
37
+ const StytchProvider = ({ stytch, children }) => {
38
+ const [user, setUser] = React.useState(null);
39
+ const [session, setSession] = React.useState(null);
40
+ React.useEffect(() => {
41
+ const unsubscribeUser = stytch.user.onChange((user) => setUser(user));
42
+ const unsubscribeSession = stytch.session.onChange((session) => setSession(session));
43
+ return () => {
44
+ unsubscribeUser();
45
+ unsubscribeSession();
46
+ };
47
+ }, [stytch, setUser, setSession]);
48
+ return (React.createElement(StytchContext.Provider, { value: stytch },
49
+ React.createElement(StytchUserContext.Provider, { value: session && user },
50
+ React.createElement(StytchSessionContext.Provider, { value: user && session }, children))));
51
+ };
52
+
53
+ /**
54
+ * Returns a unique element ID.
55
+ * Client-side only - will return null for server-side
56
+ * resolves: https://stytch.slack.com/archives/C015UDB4X33/p1641450131000800
57
+ * based on: https://github.com/vercel/next.js/issues/7322#issuecomment-968858477
58
+ */
59
+ const useUniqueElementId = () => {
60
+ const [elementId, setElementId] = React.useState(null);
61
+ React.useEffect(() => {
62
+ const randId = Math.floor(Math.random() * 1e6);
63
+ setElementId(`stytch-magic-link-${randId}`);
64
+ }, []);
65
+ return elementId;
66
+ };
67
+ /**
68
+ * Stytch JS React Component
69
+ *
70
+ * [Documentation](https://stytch.com/docs/javascript-sdk)
71
+ */
72
+ const Stytch = ({ config, styles }) => {
73
+ const stytchClient = useStytch();
74
+ const elementId = useUniqueElementId();
75
+ React.useEffect(() => {
76
+ if (!stytchClient || !elementId) {
77
+ return;
78
+ }
79
+ if (!(stytchClient instanceof StytchUIClient)) {
80
+ throw Error(`Tried to create a Stytch Login UI element using the Stytch Headless SDK.
81
+ You must use the UI SDK to use UI elements.
82
+ Please make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.
83
+ `);
84
+ }
85
+ stytchClient.mount({
86
+ config,
87
+ elementId: `#${elementId}`,
88
+ styles,
89
+ });
90
+ }, [elementId, stytchClient]);
91
+ return elementId ? React.createElement("div", { id: elementId }) : null;
92
+ };
93
+
94
+ export { Stytch, StytchProvider, useStytch, useStytchSession, useStytchUser, withStytch, withStytchSession, withStytchUser };
package/dist/index.js ADDED
@@ -0,0 +1,125 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var React = require('react');
6
+ var vanillaJs = require('@stytch/vanilla-js');
7
+
8
+ function _interopNamespace(e) {
9
+ if (e && e.__esModule) return e;
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n["default"] = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
27
+
28
+ const StytchUserContext = React__namespace.createContext(null);
29
+ const StytchSessionContext = React__namespace.createContext(null);
30
+ const StytchContext = React__namespace.createContext({});
31
+ const useStytchUser = () => {
32
+ return React__namespace.useContext(StytchUserContext);
33
+ };
34
+ const useStytchSession = () => {
35
+ return React__namespace.useContext(StytchSessionContext);
36
+ };
37
+ const useStytch = () => {
38
+ return React__namespace.useContext(StytchContext);
39
+ };
40
+ const withStytch = (Component) => {
41
+ const WithStytch = (props) => {
42
+ return React__namespace.createElement(Component, { ...props, stytch: useStytch() });
43
+ };
44
+ WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;
45
+ return WithStytch;
46
+ };
47
+ const withStytchUser = (Component) => {
48
+ const WithStytchUser = (props) => {
49
+ return React__namespace.createElement(Component, { ...props, stytchUser: useStytchUser() });
50
+ };
51
+ WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;
52
+ return WithStytchUser;
53
+ };
54
+ const withStytchSession = (Component) => {
55
+ const WithStytchSession = (props) => {
56
+ return React__namespace.createElement(Component, { ...props, stytchSession: useStytchSession() });
57
+ };
58
+ WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;
59
+ return WithStytchSession;
60
+ };
61
+ const StytchProvider = ({ stytch, children }) => {
62
+ const [user, setUser] = React__namespace.useState(null);
63
+ const [session, setSession] = React__namespace.useState(null);
64
+ React__namespace.useEffect(() => {
65
+ const unsubscribeUser = stytch.user.onChange((user) => setUser(user));
66
+ const unsubscribeSession = stytch.session.onChange((session) => setSession(session));
67
+ return () => {
68
+ unsubscribeUser();
69
+ unsubscribeSession();
70
+ };
71
+ }, [stytch, setUser, setSession]);
72
+ return (React__namespace.createElement(StytchContext.Provider, { value: stytch },
73
+ React__namespace.createElement(StytchUserContext.Provider, { value: session && user },
74
+ React__namespace.createElement(StytchSessionContext.Provider, { value: user && session }, children))));
75
+ };
76
+
77
+ /**
78
+ * Returns a unique element ID.
79
+ * Client-side only - will return null for server-side
80
+ * resolves: https://stytch.slack.com/archives/C015UDB4X33/p1641450131000800
81
+ * based on: https://github.com/vercel/next.js/issues/7322#issuecomment-968858477
82
+ */
83
+ const useUniqueElementId = () => {
84
+ const [elementId, setElementId] = React__namespace.useState(null);
85
+ React__namespace.useEffect(() => {
86
+ const randId = Math.floor(Math.random() * 1e6);
87
+ setElementId(`stytch-magic-link-${randId}`);
88
+ }, []);
89
+ return elementId;
90
+ };
91
+ /**
92
+ * Stytch JS React Component
93
+ *
94
+ * [Documentation](https://stytch.com/docs/javascript-sdk)
95
+ */
96
+ const Stytch = ({ config, styles }) => {
97
+ const stytchClient = useStytch();
98
+ const elementId = useUniqueElementId();
99
+ React__namespace.useEffect(() => {
100
+ if (!stytchClient || !elementId) {
101
+ return;
102
+ }
103
+ if (!(stytchClient instanceof vanillaJs.StytchUIClient)) {
104
+ throw Error(`Tried to create a Stytch Login UI element using the Stytch Headless SDK.
105
+ You must use the UI SDK to use UI elements.
106
+ Please make sure you are importing from @stytch/vanilla-js and not from the @stytch/vanilla-js/headless.
107
+ `);
108
+ }
109
+ stytchClient.mount({
110
+ config,
111
+ elementId: `#${elementId}`,
112
+ styles,
113
+ });
114
+ }, [elementId, stytchClient]);
115
+ return elementId ? React__namespace.createElement("div", { id: elementId }) : null;
116
+ };
117
+
118
+ exports.Stytch = Stytch;
119
+ exports.StytchProvider = StytchProvider;
120
+ exports.useStytch = useStytch;
121
+ exports.useStytchSession = useStytchSession;
122
+ exports.useStytchUser = useStytchUser;
123
+ exports.withStytch = withStytch;
124
+ exports.withStytchSession = withStytchSession;
125
+ exports.withStytchUser = withStytchUser;
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@stytch/react",
3
+ "version": "0.0.0-rc.1",
4
+ "description": "Stytch's official React Library",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.esm.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "npm run clean && npm run compile",
13
+ "clean": "rm -rf ./dist",
14
+ "compile": "rollup -c",
15
+ "dev": "tsc -p tsconfig.build.json --watch"
16
+ },
17
+ "license": "MIT",
18
+ "author": "Stytch",
19
+ "devDependencies": {
20
+ "@babel/runtime": "^7.18.6",
21
+ "@stytch/vanilla-js": "0.0.0-rc.1",
22
+ "eslint-config-custom": "0.0.0",
23
+ "rollup": "^2.56.3",
24
+ "typescript": "^4.5.3"
25
+ },
26
+ "peerDependencies": {
27
+ "@stytch/vanilla-js": "0.0.0-rc.1",
28
+ "react": ">= 17.0.2",
29
+ "react-dom": ">= 17.0.2"
30
+ },
31
+ "stableVersion": "0.0.0"
32
+ }