@stytch/stytch-react 2.0.0 → 3.0.3

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/README.md CHANGED
@@ -57,3 +57,12 @@ const App = () => {
57
57
  ## Typescript Support
58
58
 
59
59
  There are built in typescript definitions in the npm package.
60
+
61
+ ### Changelog
62
+ **[3.0.1] - 2020-8-25**
63
+ - `@stytch/stytch-js` has been bumped to include new OAuth types
64
+ - `SDKProductTypes` and `OAuthProvidersTypes` are now re-exported for convenience
65
+ - Dependency bumps
66
+
67
+ **[3.0.0] - 2020-8-1**
68
+ - This package now uses the [@stytch/stytch-js](https://www.npmjs.com/package/@stytch/stytch-js) library to load stytch.js
package/dist/index.d.ts CHANGED
@@ -1,106 +1,16 @@
1
1
  /// <reference types="react" />
2
- declare global {
3
- interface Window {
4
- Stytch: (publicToken: string, options: CallbackOptions) => StytchClient;
5
- }
6
- }
7
- // Client object types
8
- interface StytchClient {
9
- mount: (mountArgs: StytchMountArgs) => void;
10
- }
2
+ import { LoginOrSignupView, StyleConfig, CallbackOptions } from "@stytch/stytch-js";
11
3
  interface StytchProps {
12
4
  publicToken: string;
13
- loginOrSignupView: LoginOrSignupView;
5
+ loginOrSignupView?: LoginOrSignupView;
14
6
  style?: StyleConfig;
15
7
  callbacks?: CallbackOptions;
16
- // Do not change this prop. This defaults to https://js.stytch.com/stytch.js
17
- _url?: string;
18
- }
19
- interface StytchMountArgs {
20
- elementId: string;
21
- loginOrSignupView: LoginOrSignupView;
22
- style: StyleConfig;
23
- }
24
- // Config types
25
- interface LoginConfig {
26
- magicLinkUrl: string;
27
- expirationMinutes?: number;
28
- emailTemplateId?: string;
29
- }
30
- interface CreateUserConfig {
31
- magicLinkUrl: string;
32
- expirationMinutes?: number;
33
- emailTemplateId?: string;
34
- createUserAsPending?: boolean;
35
- }
36
- interface LoginOrCreateUserConfig {
37
- loginConfig: LoginConfig;
38
- createUserConfig: CreateUserConfig;
39
- }
40
- interface StyleConfig {
41
- fontFamily?: string;
42
- width?: string;
43
- primaryColor?: string;
44
- primaryTextColor?: string;
45
- secondaryTextColor?: string;
46
- lightGrey?: string;
47
- darkGrey?: string;
48
- hideHeaderText?: boolean;
49
- }
50
- interface EmailMagicLinksOptions {
51
- loginRedirectURL: string;
52
- loginExpirationMinutes: number;
53
- signupRedirectURL: string;
54
- signupExpirationMinutes: number;
55
- createUserAsPending: boolean;
56
- }
57
- interface LoginOrSignupView {
58
- products: SDKProductTypes[];
59
- emailMagicLinksOptions: EmailMagicLinksOptions;
60
- }
61
- // Callback types
62
- declare enum EventType {
63
- CallbackEvent = "CALLBACK_EVENT",
64
- ErrorEvent = "ERROR_EVENT",
65
- SuccessEvent = "SUCCESS_EVENT"
66
- }
67
- declare enum CallbackEventType {
68
- UserEventType = "USER_EVENT_TYPE"
69
- }
70
- interface UserEventData {
71
- type: CallbackEventType.UserEventType;
72
- email: string;
73
- emailId: string;
74
- userId: string;
75
- }
76
- interface MagicLinkSentData {
77
- message: string;
78
- }
79
- interface StytchError {
80
- message: string;
81
- }
82
- interface EventData {
83
- eventType: EventType.CallbackEvent;
84
- eventData: UserEventData;
85
- }
86
- interface SuccessData {
87
- eventType: EventType.SuccessEvent;
88
- eventData: MagicLinkSentData;
89
- }
90
- interface ErrorData {
91
- eventType: EventType.ErrorEvent;
92
- error: StytchError;
93
- }
94
- interface CallbackOptions {
95
- onEvent?: (data?: EventData) => void | Promise<void>;
96
- onSuccess?: (data?: SuccessData) => void | Promise<void>;
97
- onError?: (data?: ErrorData) => void | Promise<void>;
98
8
  }
99
- type SDKProductTypes = "emailMagicLinks";
100
9
  /**
101
10
  * Stytch JS React Component
102
11
  *
103
12
  * [Documentation](https://stytch.com/docs/javascript-sdk)
104
13
  */
105
14
  declare const Stytch: (props: StytchProps) => JSX.Element;
106
- export { Stytch, StytchClient, StytchProps, StytchMountArgs, LoginConfig, CreateUserConfig, LoginOrCreateUserConfig, StyleConfig, EmailMagicLinksOptions, LoginOrSignupView, EventType, CallbackEventType, UserEventData, MagicLinkSentData, StytchError, EventData, SuccessData, ErrorData, CallbackOptions, SDKProductTypes };
15
+ export { SDKProductTypes, OAuthProvidersTypes } from "@stytch/stytch-js";
16
+ export { Stytch, StytchProps };
@@ -1,106 +1,16 @@
1
1
  /// <reference types="react" />
2
- declare global {
3
- interface Window {
4
- Stytch: (publicToken: string, options: CallbackOptions) => StytchClient;
5
- }
6
- }
7
- // Client object types
8
- interface StytchClient {
9
- mount: (mountArgs: StytchMountArgs) => void;
10
- }
2
+ import { LoginOrSignupView, StyleConfig, CallbackOptions } from "@stytch/stytch-js";
11
3
  interface StytchProps {
12
4
  publicToken: string;
13
- loginOrSignupView: LoginOrSignupView;
5
+ loginOrSignupView?: LoginOrSignupView;
14
6
  style?: StyleConfig;
15
7
  callbacks?: CallbackOptions;
16
- // Do not change this prop. This defaults to https://js.stytch.com/stytch.js
17
- _url?: string;
18
- }
19
- interface StytchMountArgs {
20
- elementId: string;
21
- loginOrSignupView: LoginOrSignupView;
22
- style: StyleConfig;
23
- }
24
- // Config types
25
- interface LoginConfig {
26
- magicLinkUrl: string;
27
- expirationMinutes?: number;
28
- emailTemplateId?: string;
29
- }
30
- interface CreateUserConfig {
31
- magicLinkUrl: string;
32
- expirationMinutes?: number;
33
- emailTemplateId?: string;
34
- createUserAsPending?: boolean;
35
- }
36
- interface LoginOrCreateUserConfig {
37
- loginConfig: LoginConfig;
38
- createUserConfig: CreateUserConfig;
39
- }
40
- interface StyleConfig {
41
- fontFamily?: string;
42
- width?: string;
43
- primaryColor?: string;
44
- primaryTextColor?: string;
45
- secondaryTextColor?: string;
46
- lightGrey?: string;
47
- darkGrey?: string;
48
- hideHeaderText?: boolean;
49
- }
50
- interface EmailMagicLinksOptions {
51
- loginRedirectURL: string;
52
- loginExpirationMinutes: number;
53
- signupRedirectURL: string;
54
- signupExpirationMinutes: number;
55
- createUserAsPending: boolean;
56
- }
57
- interface LoginOrSignupView {
58
- products: SDKProductTypes[];
59
- emailMagicLinksOptions: EmailMagicLinksOptions;
60
- }
61
- // Callback types
62
- declare enum EventType {
63
- CallbackEvent = "CALLBACK_EVENT",
64
- ErrorEvent = "ERROR_EVENT",
65
- SuccessEvent = "SUCCESS_EVENT"
66
- }
67
- declare enum CallbackEventType {
68
- UserEventType = "USER_EVENT_TYPE"
69
- }
70
- interface UserEventData {
71
- type: CallbackEventType.UserEventType;
72
- email: string;
73
- emailId: string;
74
- userId: string;
75
- }
76
- interface MagicLinkSentData {
77
- message: string;
78
- }
79
- interface StytchError {
80
- message: string;
81
- }
82
- interface EventData {
83
- eventType: EventType.CallbackEvent;
84
- eventData: UserEventData;
85
- }
86
- interface SuccessData {
87
- eventType: EventType.SuccessEvent;
88
- eventData: MagicLinkSentData;
89
- }
90
- interface ErrorData {
91
- eventType: EventType.ErrorEvent;
92
- error: StytchError;
93
- }
94
- interface CallbackOptions {
95
- onEvent?: (data?: EventData) => void | Promise<void>;
96
- onSuccess?: (data?: SuccessData) => void | Promise<void>;
97
- onError?: (data?: ErrorData) => void | Promise<void>;
98
8
  }
99
- type SDKProductTypes = "emailMagicLinks";
100
9
  /**
101
10
  * Stytch JS React Component
102
11
  *
103
12
  * [Documentation](https://stytch.com/docs/javascript-sdk)
104
13
  */
105
14
  declare const Stytch: (props: StytchProps) => JSX.Element;
106
- export { Stytch, StytchClient, StytchProps, StytchMountArgs, LoginConfig, CreateUserConfig, LoginOrCreateUserConfig, StyleConfig, EmailMagicLinksOptions, LoginOrSignupView, EventType, CallbackEventType, UserEventData, MagicLinkSentData, StytchError, EventData, SuccessData, ErrorData, CallbackOptions, SDKProductTypes };
15
+ export { SDKProductTypes, OAuthProvidersTypes } from "@stytch/stytch-js";
16
+ export { Stytch, StytchProps };
package/dist/index.esm.js CHANGED
@@ -1,138 +1,75 @@
1
1
  import React from 'react';
2
2
 
3
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
4
-
5
- function unwrapExports (x) {
6
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
7
- }
8
-
9
- function createCommonjsModule(fn, module) {
10
- return module = { exports: {} }, fn(module, module.exports), module.exports;
11
- }
12
-
13
- var useScript_1 = createCommonjsModule(function (module, exports) {
14
- var __rest = (commonjsGlobal && commonjsGlobal.__rest) || function (s, e) {
15
- var t = {};
16
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
17
- t[p] = s[p];
18
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
19
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
20
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
21
- t[p[i]] = s[p[i]];
3
+ var PUBLIC_URL = 'https://js.stytch.com/stytch.js';
4
+ function getJSUrl() {
5
+ try {
6
+ if (process.env.STYTCH_JS_SDK_URL) {
7
+ return process.env.STYTCH_JS_SDK_URL;
22
8
  }
23
- return t;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
-
27
- function useScript(_a) {
28
- var src = _a.src, _b = _a.checkForExisting, checkForExisting = _b === void 0 ? false : _b, attributes = __rest(_a, ["src", "checkForExisting"]);
29
- var _c = React.useState(Boolean(src)), loading = _c[0], setLoading = _c[1];
30
- var _d = React.useState(null), error = _d[0], setError = _d[1];
31
- React.useEffect(function () {
32
- if (!isBrowser || !src)
33
- return;
34
- if (checkForExisting) {
35
- var existing = document.querySelectorAll("script[src=\"" + src + "\"]");
36
- if (existing.length > 0) {
37
- setLoading(false);
38
- return;
39
- }
9
+ if (process.env.REACT_APP_STYTCH_JS_SDK_URL) {
10
+ return process.env.REACT_APP_STYTCH_JS_SDK_URL;
40
11
  }
41
- var scriptEl = document.createElement('script');
42
- scriptEl.setAttribute('src', src);
43
- Object.keys(attributes).forEach(function (key) {
44
- if (scriptEl[key] === undefined) {
45
- scriptEl.setAttribute(key, attributes[key]);
46
- }
47
- else {
48
- scriptEl[key] = attributes[key];
49
- }
50
- });
51
- var handleLoad = function () {
52
- setLoading(false);
53
- };
54
- var handleError = function (error) {
55
- setError(error);
56
- };
57
- scriptEl.addEventListener('load', handleLoad);
58
- scriptEl.addEventListener('error', handleError);
59
- document.body.appendChild(scriptEl);
60
- return function () {
61
- scriptEl.removeEventListener('load', handleLoad);
62
- scriptEl.removeEventListener('error', handleError);
63
- };
64
- // we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call
65
- // eslint-disable-next-line react-hooks/exhaustive-deps
66
- }, [src]);
67
- return [loading, error];
12
+ if (process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL) {
13
+ return process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL;
14
+ }
15
+ if (process.env.STORYBOOK_STYTCH_JS_SDK_URL) {
16
+ return process.env.STORYBOOK_STYTCH_JS_SDK_URL;
17
+ }
18
+ return PUBLIC_URL;
19
+ }
20
+ catch (err) {
21
+ return PUBLIC_URL;
22
+ }
68
23
  }
69
- exports.default = useScript;
70
- var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
71
-
72
- });
24
+ var STYTCH_JS_URL = getJSUrl();
73
25
 
74
- unwrapExports(useScript_1);
75
-
76
- var lib = createCommonjsModule(function (module, exports) {
77
- var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
78
- return (mod && mod.__esModule) ? mod : { "default": mod };
26
+ var findOrCreateScript = function () {
27
+ var scripts = document.querySelectorAll("script[src=\"" + STYTCH_JS_URL + "\"]");
28
+ if (scripts[0]) {
29
+ return scripts[0];
30
+ }
31
+ var script = document.createElement('script');
32
+ script.src = STYTCH_JS_URL;
33
+ document.head.appendChild(script);
34
+ return script;
79
35
  };
80
- Object.defineProperty(exports, "__esModule", { value: true });
81
- exports.default = void 0;
82
-
83
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(useScript_1).default; } });
84
-
85
- });
86
-
87
- var useScript = unwrapExports(lib);
88
-
89
- var STYTCH_JS_URL = "https://js.stytch.com/stytch.js";
90
-
91
- /**
92
- * Stytch JS React Component
93
- *
94
- * [Documentation](https://stytch.com/docs/javascript-sdk)
95
- */
96
- var Stytch = function (props) {
97
- // url check
98
- var url = props._url || STYTCH_JS_URL;
99
- if (url !== STYTCH_JS_URL) {
100
- console.warn("Please do not change _url prop. Changing _url prop will result in Stytch SDK not working properly.");
101
- if (!props._url.endsWith("stytch.com/stytch.js") &&
102
- !props._url.includes("localhost")) {
103
- return;
104
- }
36
+ var loadScript = function () {
37
+ var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
38
+ if (!isBrowser) {
39
+ return Promise.resolve(null);
105
40
  }
106
- var _a = useScript({
107
- src: url || STYTCH_JS_URL,
108
- checkForExisting: true,
109
- }), loading = _a[0], error = _a[1];
110
- var publicToken = props.publicToken, style = props.style, callbacks = props.callbacks, loginOrSignupView = props.loginOrSignupView;
111
- React.useEffect(function () {
112
- if (loading) {
113
- return;
114
- }
115
- if (error || !window.Stytch) {
116
- // eslint-disable-next-line no-console
117
- console.error("Error loading Stytch", error);
118
- return;
41
+ return new Promise(function (resolve, reject) {
42
+ if (window.Stytch) {
43
+ console.warn('Stytch.js has already been loaded');
44
+ resolve(window.Stytch);
119
45
  }
120
- else {
121
- // Initialize Stytch.js with your public token. You can find this in your Stytch dashboard under API Keys.
122
- var stytch = window.Stytch(publicToken, callbacks);
123
- if (loginOrSignupView) {
124
- stytch.mount({
125
- elementId: "#stytch-magic-link",
126
- style: style,
127
- loginOrSignupView: loginOrSignupView,
128
- });
46
+ var script = findOrCreateScript();
47
+ script.addEventListener('load', function () {
48
+ if (window.Stytch) {
49
+ resolve(window.Stytch);
129
50
  }
130
51
  else {
131
- console.error("loginOrSignupView is a required prop");
52
+ reject(new Error('Stytch.js could not be loaded'));
132
53
  }
133
- }
134
- }, [loading]);
135
- return React.createElement("div", { id: "stytch-magic-link" });
54
+ });
55
+ script.addEventListener('error', function () {
56
+ reject(new Error('Stytch.js could not be loaded'));
57
+ });
58
+ });
59
+ };
60
+ // Execute our own script injection after a tick to give users time to do their
61
+ // own script injection.
62
+ var stytchPromise = Promise.resolve().then(function () { return loadScript(); });
63
+ var loadCalled = false;
64
+ // We will log a warning if it doesn't look like the host code has checked or invoked the promise
65
+ stytchPromise.catch(function (err) {
66
+ if (!loadCalled) {
67
+ console.warn(err);
68
+ }
69
+ });
70
+ var loadStytch = function () {
71
+ loadCalled = true;
72
+ return stytchPromise;
136
73
  };
137
74
 
138
75
  // Callback types
@@ -146,6 +83,61 @@ var CallbackEventType;
146
83
  (function (CallbackEventType) {
147
84
  CallbackEventType["UserEventType"] = "USER_EVENT_TYPE";
148
85
  })(CallbackEventType || (CallbackEventType = {}));
86
+ var SDKProductTypes;
87
+ (function (SDKProductTypes) {
88
+ SDKProductTypes["emailMagicLinks"] = "emailMagicLinks";
89
+ SDKProductTypes["oauth"] = "oauth";
90
+ })(SDKProductTypes || (SDKProductTypes = {}));
91
+ var OAuthProvidersTypes;
92
+ (function (OAuthProvidersTypes) {
93
+ OAuthProvidersTypes["Google"] = "google";
94
+ OAuthProvidersTypes["Microsoft"] = "microsoft";
95
+ OAuthProvidersTypes["Apple"] = "apple";
96
+ })(OAuthProvidersTypes || (OAuthProvidersTypes = {}));
97
+ var OneTapPositions;
98
+ (function (OneTapPositions) {
99
+ OneTapPositions["embedded"] = "embedded";
100
+ OneTapPositions["floating"] = "floating";
101
+ })(OneTapPositions || (OneTapPositions = {}));
102
+
103
+ var useStytchFactory = function () {
104
+ var _a = React.useState(), stytchFactory = _a[0], setStytchFactory = _a[1];
105
+ React.useEffect(function () {
106
+ // If we pass the function in directly React will execute it as a SetStateAction
107
+ // Need to wrap in an anon - see https://medium.com/swlh/how-to-store-a-function-with-the-usestate-hook-in-react-8a88dd4eede1
108
+ loadStytch().then(function (globalStytch) { return setStytchFactory(function () { return globalStytch; }); });
109
+ }, []);
110
+ return stytchFactory;
111
+ };
112
+ var useUniqueElementId = function () {
113
+ var elementId = React.useState(function () {
114
+ var randId = Math.floor(Math.random() * 1e6);
115
+ return "stytch-magic-link-" + randId;
116
+ })[0];
117
+ return elementId;
118
+ };
119
+ /**
120
+ * Stytch JS React Component
121
+ *
122
+ * [Documentation](https://stytch.com/docs/javascript-sdk)
123
+ */
124
+ var Stytch = function (props) {
125
+ var StytchFactory = useStytchFactory();
126
+ var elementId = useUniqueElementId();
127
+ var publicToken = props.publicToken, style = props.style, callbacks = props.callbacks, loginOrSignupView = props.loginOrSignupView;
128
+ React.useEffect(function () {
129
+ if (!StytchFactory) {
130
+ return;
131
+ }
132
+ var stytch = StytchFactory(publicToken, callbacks);
133
+ stytch.mount({
134
+ elementId: "#" + elementId,
135
+ style: style,
136
+ loginOrSignupView: loginOrSignupView,
137
+ });
138
+ }, [StytchFactory, elementId]);
139
+ return React.createElement("div", { id: elementId });
140
+ };
149
141
 
150
- export { CallbackEventType, EventType, Stytch };
142
+ export { OAuthProvidersTypes, SDKProductTypes, Stytch };
151
143
  //# sourceMappingURL=index.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../node_modules/react-script-hook/lib/use-script.js","../node_modules/react-script-hook/lib/index.js","../src/constants.tsx","../src/Stytch.tsx","../src/types/index.ts"],"sourcesContent":["\"use strict\";\nvar __rest = (this && this.__rest) || function (s, e) {\n var t = {};\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\n t[p] = s[p];\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\n t[p[i]] = s[p[i]];\n }\n return t;\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar react_1 = require(\"react\");\nfunction useScript(_a) {\n var src = _a.src, _b = _a.checkForExisting, checkForExisting = _b === void 0 ? false : _b, attributes = __rest(_a, [\"src\", \"checkForExisting\"]);\n var _c = react_1.useState(Boolean(src)), loading = _c[0], setLoading = _c[1];\n var _d = react_1.useState(null), error = _d[0], setError = _d[1];\n react_1.useEffect(function () {\n if (!isBrowser || !src)\n return;\n if (checkForExisting) {\n var existing = document.querySelectorAll(\"script[src=\\\"\" + src + \"\\\"]\");\n if (existing.length > 0) {\n setLoading(false);\n return;\n }\n }\n var scriptEl = document.createElement('script');\n scriptEl.setAttribute('src', src);\n Object.keys(attributes).forEach(function (key) {\n if (scriptEl[key] === undefined) {\n scriptEl.setAttribute(key, attributes[key]);\n }\n else {\n scriptEl[key] = attributes[key];\n }\n });\n var handleLoad = function () {\n setLoading(false);\n };\n var handleError = function (error) {\n setError(error);\n };\n scriptEl.addEventListener('load', handleLoad);\n scriptEl.addEventListener('error', handleError);\n document.body.appendChild(scriptEl);\n return function () {\n scriptEl.removeEventListener('load', handleLoad);\n scriptEl.removeEventListener('error', handleError);\n };\n // we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [src]);\n return [loading, error];\n}\nexports.default = useScript;\nvar isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';\n//# sourceMappingURL=use-script.js.map","\"use strict\";\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.default = void 0;\nvar use_script_1 = require(\"./use-script\");\nObject.defineProperty(exports, \"default\", { enumerable: true, get: function () { return __importDefault(use_script_1).default; } });\n//# sourceMappingURL=index.js.map","export const STYTCH_JS_URL = \"https://js.stytch.com/stytch.js\";\n","import React from \"react\";\nimport useScript from \"react-script-hook\";\nimport { STYTCH_JS_URL } from \"./constants\";\nimport { StytchProps } from \"./types\";\n\n/**\n * Stytch JS React Component\n *\n * [Documentation](https://stytch.com/docs/javascript-sdk)\n */\nexport const Stytch = (props: StytchProps) => {\n // url check\n const url = props._url || STYTCH_JS_URL;\n if (url !== STYTCH_JS_URL) {\n console.warn(\n \"Please do not change _url prop. Changing _url prop will result in Stytch SDK not working properly.\"\n );\n if (\n !props._url.endsWith(\"stytch.com/stytch.js\") &&\n !props._url.includes(\"localhost\")\n ) {\n return;\n }\n }\n\n const [loading, error] = useScript({\n src: url || STYTCH_JS_URL,\n checkForExisting: true,\n });\n const { publicToken, style, callbacks, loginOrSignupView } = props;\n\n React.useEffect(() => {\n if (loading) {\n return;\n }\n if (error || !window.Stytch) {\n // eslint-disable-next-line no-console\n console.error(\"Error loading Stytch\", error);\n return;\n } else {\n // Initialize Stytch.js with your public token. You can find this in your Stytch dashboard under API Keys.\n var stytch = window.Stytch(publicToken, callbacks);\n if (loginOrSignupView) {\n stytch.mount({\n elementId: \"#stytch-magic-link\",\n style,\n loginOrSignupView,\n });\n } else {\n console.error(\"loginOrSignupView is a required prop\");\n }\n }\n }, [loading]);\n\n return <div id=\"stytch-magic-link\"></div>;\n};\n","declare global {\n interface Window {\n Stytch: (publicToken: string, options: CallbackOptions) => StytchClient;\n }\n}\n\n// Client object types\nexport interface StytchClient {\n mount: (mountArgs: StytchMountArgs) => void;\n}\n\nexport interface StytchProps {\n publicToken: string;\n loginOrSignupView: LoginOrSignupView;\n style?: StyleConfig;\n callbacks?: CallbackOptions;\n // Do not change this prop. This defaults to https://js.stytch.com/stytch.js\n _url?: string;\n}\n\nexport interface StytchMountArgs {\n elementId: string;\n loginOrSignupView: LoginOrSignupView;\n style: StyleConfig;\n}\n\n// Config types\nexport interface LoginConfig {\n magicLinkUrl: string;\n expirationMinutes?: number;\n emailTemplateId?: string;\n}\n\nexport interface CreateUserConfig {\n magicLinkUrl: string;\n expirationMinutes?: number;\n emailTemplateId?: string;\n createUserAsPending?: boolean;\n}\n\nexport interface LoginOrCreateUserConfig {\n loginConfig: LoginConfig;\n createUserConfig: CreateUserConfig;\n}\n\nexport interface StyleConfig {\n fontFamily?: string;\n width?: string;\n primaryColor?: string;\n primaryTextColor?: string;\n secondaryTextColor?: string;\n lightGrey?: string;\n darkGrey?: string;\n hideHeaderText?: boolean;\n}\n\nexport interface EmailMagicLinksOptions {\n loginRedirectURL: string;\n loginExpirationMinutes: number;\n signupRedirectURL: string;\n signupExpirationMinutes: number;\n createUserAsPending: boolean;\n}\n\nexport interface LoginOrSignupView {\n products: SDKProductTypes[];\n emailMagicLinksOptions: EmailMagicLinksOptions;\n}\n\n// Callback types\nexport enum EventType {\n CallbackEvent = \"CALLBACK_EVENT\",\n ErrorEvent = \"ERROR_EVENT\",\n SuccessEvent = \"SUCCESS_EVENT\",\n}\nexport enum CallbackEventType {\n UserEventType = \"USER_EVENT_TYPE\",\n}\nexport interface UserEventData {\n type: CallbackEventType.UserEventType;\n email: string;\n emailId: string;\n userId: string;\n}\nexport interface MagicLinkSentData {\n message: string;\n}\nexport interface StytchError {\n message: string;\n}\nexport interface EventData {\n eventType: EventType.CallbackEvent;\n eventData: UserEventData;\n}\nexport interface SuccessData {\n eventType: EventType.SuccessEvent;\n eventData: MagicLinkSentData;\n}\nexport interface ErrorData {\n eventType: EventType.ErrorEvent;\n error: StytchError;\n}\nexport interface CallbackOptions {\n onEvent?: (data?: EventData) => void | Promise<void>;\n onSuccess?: (data?: SuccessData) => void | Promise<void>;\n onError?: (data?: ErrorData) => void | Promise<void>;\n}\n\nexport type SDKProductTypes = \"emailMagicLinks\";\n"],"names":["this","react_1","use_script_1"],"mappings":";;;;;;;;;;;;;AACA,IAAI,MAAM,GAAG,CAACA,cAAI,IAAIA,cAAI,CAAC,MAAM,KAAK,UAAU,CAAC,EAAE,CAAC,EAAE;AACtD,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;AACf,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;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IAEd,OAAO,6BAAK,EAAE,EAAC,mBAAmB,GAAO,CAAC;AAC5C;;ACcA;IACY;AAAZ,WAAY,SAAS;IACnB,6CAAgC,CAAA;IAChC,uCAA0B,CAAA;IAC1B,2CAA8B,CAAA;AAChC,CAAC,EAJW,SAAS,KAAT,SAAS,QAIpB;IACW;AAAZ,WAAY,iBAAiB;IAC3B,sDAAiC,CAAA;AACnC,CAAC,EAFW,iBAAiB,KAAjB,iBAAiB;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../node_modules/@stytch/stytch-js/dist/index.esm.js","../src/Stytch.tsx"],"sourcesContent":["var PUBLIC_URL = 'https://js.stytch.com/stytch.js';\nfunction getJSUrl() {\n try {\n if (process.env.STYTCH_JS_SDK_URL) {\n return process.env.STYTCH_JS_SDK_URL;\n }\n if (process.env.REACT_APP_STYTCH_JS_SDK_URL) {\n return process.env.REACT_APP_STYTCH_JS_SDK_URL;\n }\n if (process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL) {\n return process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL;\n }\n if (process.env.STORYBOOK_STYTCH_JS_SDK_URL) {\n return process.env.STORYBOOK_STYTCH_JS_SDK_URL;\n }\n return PUBLIC_URL;\n }\n catch (err) {\n return PUBLIC_URL;\n }\n}\nvar STYTCH_JS_URL = getJSUrl();\n\nvar findOrCreateScript = function () {\n var scripts = document.querySelectorAll(\"script[src=\\\"\" + STYTCH_JS_URL + \"\\\"]\");\n if (scripts[0]) {\n return scripts[0];\n }\n var script = document.createElement('script');\n script.src = STYTCH_JS_URL;\n document.head.appendChild(script);\n return script;\n};\nvar loadScript = function () {\n var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';\n if (!isBrowser) {\n return Promise.resolve(null);\n }\n return new Promise(function (resolve, reject) {\n if (window.Stytch) {\n console.warn('Stytch.js has already been loaded');\n resolve(window.Stytch);\n }\n var script = findOrCreateScript();\n script.addEventListener('load', function () {\n if (window.Stytch) {\n resolve(window.Stytch);\n }\n else {\n reject(new Error('Stytch.js could not be loaded'));\n }\n });\n script.addEventListener('error', function () {\n reject(new Error('Stytch.js could not be loaded'));\n });\n });\n};\n// Execute our own script injection after a tick to give users time to do their\n// own script injection.\nvar stytchPromise = Promise.resolve().then(function () { return loadScript(); });\nvar loadCalled = false;\n// We will log a warning if it doesn't look like the host code has checked or invoked the promise\nstytchPromise.catch(function (err) {\n if (!loadCalled) {\n console.warn(err);\n }\n});\nvar loadStytch = function () {\n loadCalled = true;\n return stytchPromise;\n};\n\n// Callback types\nvar EventType;\n(function (EventType) {\n EventType[\"CallbackEvent\"] = \"CALLBACK_EVENT\";\n EventType[\"ErrorEvent\"] = \"ERROR_EVENT\";\n EventType[\"SuccessEvent\"] = \"SUCCESS_EVENT\";\n})(EventType || (EventType = {}));\nvar CallbackEventType;\n(function (CallbackEventType) {\n CallbackEventType[\"UserEventType\"] = \"USER_EVENT_TYPE\";\n})(CallbackEventType || (CallbackEventType = {}));\nvar SDKProductTypes;\n(function (SDKProductTypes) {\n SDKProductTypes[\"emailMagicLinks\"] = \"emailMagicLinks\";\n SDKProductTypes[\"oauth\"] = \"oauth\";\n})(SDKProductTypes || (SDKProductTypes = {}));\nvar OAuthProvidersTypes;\n(function (OAuthProvidersTypes) {\n OAuthProvidersTypes[\"Google\"] = \"google\";\n OAuthProvidersTypes[\"Microsoft\"] = \"microsoft\";\n OAuthProvidersTypes[\"Apple\"] = \"apple\";\n})(OAuthProvidersTypes || (OAuthProvidersTypes = {}));\nvar OneTapPositions;\n(function (OneTapPositions) {\n OneTapPositions[\"embedded\"] = \"embedded\";\n OneTapPositions[\"floating\"] = \"floating\";\n})(OneTapPositions || (OneTapPositions = {}));\n\nexport { CallbackEventType, EventType, OAuthProvidersTypes, OneTapPositions, SDKProductTypes, loadStytch };\n//# sourceMappingURL=index.esm.js.map\n","import React from \"react\";\nimport {loadStytch, LoginOrSignupView, StyleConfig, CallbackOptions, StytchFactory} from '@stytch/stytch-js'\n\nexport {SDKProductTypes, OAuthProvidersTypes} from '@stytch/stytch-js'\n\nexport interface StytchProps {\n publicToken: string;\n loginOrSignupView?: LoginOrSignupView;\n style?: StyleConfig;\n callbacks?: CallbackOptions;\n}\n\nconst useStytchFactory = (): StytchFactory => {\n const [stytchFactory, setStytchFactory] = React.useState<StytchFactory>();\n React.useEffect(() => {\n // If we pass the function in directly React will execute it as a SetStateAction\n // Need to wrap in an anon - see https://medium.com/swlh/how-to-store-a-function-with-the-usestate-hook-in-react-8a88dd4eede1\n loadStytch().then(globalStytch => setStytchFactory(() => globalStytch))\n }, []);\n\n return stytchFactory;\n}\n\nconst useUniqueElementId = (): string => {\n const [elementId] = React.useState<string>(() => {\n const randId = Math.floor(Math.random() * 1e6);\n return `stytch-magic-link-${randId}`;\n });\n return elementId;\n}\n\n/**\n * Stytch JS React Component\n *\n * [Documentation](https://stytch.com/docs/javascript-sdk)\n */\nexport const Stytch = (props: StytchProps) => {\n const StytchFactory = useStytchFactory();\n const elementId = useUniqueElementId();\n\n const {publicToken, style, callbacks, loginOrSignupView} = props;\n\n React.useEffect(() => {\n if (!StytchFactory) {\n return;\n }\n const stytch = StytchFactory(publicToken, callbacks);\n\n stytch.mount({\n elementId: `#${elementId}`,\n style,\n loginOrSignupView,\n });\n }, [StytchFactory, elementId]);\n\n return <div id={elementId}></div>;\n};\n"],"names":[],"mappings":";;AAAA,IAAI,UAAU,GAAG,iCAAiC,CAAC;AACnD,SAAS,QAAQ,GAAG;AACpB,IAAI,IAAI;AACR,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE;AAC3C,YAAY,OAAO,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;AACjD,SAAS;AACT,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE;AACrD,YAAY,OAAO,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC;AAC3D,SAAS;AACT,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,6BAA6B,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
9
9
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
10
10
 
11
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
12
-
13
- function unwrapExports (x) {
14
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
15
- }
16
-
17
- function createCommonjsModule(fn, module) {
18
- return module = { exports: {} }, fn(module, module.exports), module.exports;
11
+ var PUBLIC_URL = 'https://js.stytch.com/stytch.js';
12
+ function getJSUrl() {
13
+ try {
14
+ if (process.env.STYTCH_JS_SDK_URL) {
15
+ return process.env.STYTCH_JS_SDK_URL;
16
+ }
17
+ if (process.env.REACT_APP_STYTCH_JS_SDK_URL) {
18
+ return process.env.REACT_APP_STYTCH_JS_SDK_URL;
19
+ }
20
+ if (process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL) {
21
+ return process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL;
22
+ }
23
+ if (process.env.STORYBOOK_STYTCH_JS_SDK_URL) {
24
+ return process.env.STORYBOOK_STYTCH_JS_SDK_URL;
25
+ }
26
+ return PUBLIC_URL;
27
+ }
28
+ catch (err) {
29
+ return PUBLIC_URL;
30
+ }
19
31
  }
32
+ var STYTCH_JS_URL = getJSUrl();
20
33
 
21
- var useScript_1 = createCommonjsModule(function (module, exports) {
22
- var __rest = (commonjsGlobal && commonjsGlobal.__rest) || function (s, e) {
23
- var t = {};
24
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
25
- t[p] = s[p];
26
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
27
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
28
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
29
- t[p[i]] = s[p[i]];
30
- }
31
- return t;
34
+ var findOrCreateScript = function () {
35
+ var scripts = document.querySelectorAll("script[src=\"" + STYTCH_JS_URL + "\"]");
36
+ if (scripts[0]) {
37
+ return scripts[0];
38
+ }
39
+ var script = document.createElement('script');
40
+ script.src = STYTCH_JS_URL;
41
+ document.head.appendChild(script);
42
+ return script;
32
43
  };
33
- Object.defineProperty(exports, "__esModule", { value: true });
34
-
35
- function useScript(_a) {
36
- var src = _a.src, _b = _a.checkForExisting, checkForExisting = _b === void 0 ? false : _b, attributes = __rest(_a, ["src", "checkForExisting"]);
37
- var _c = React__default['default'].useState(Boolean(src)), loading = _c[0], setLoading = _c[1];
38
- var _d = React__default['default'].useState(null), error = _d[0], setError = _d[1];
39
- React__default['default'].useEffect(function () {
40
- if (!isBrowser || !src)
41
- return;
42
- if (checkForExisting) {
43
- var existing = document.querySelectorAll("script[src=\"" + src + "\"]");
44
- if (existing.length > 0) {
45
- setLoading(false);
46
- return;
47
- }
44
+ var loadScript = function () {
45
+ var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
46
+ if (!isBrowser) {
47
+ return Promise.resolve(null);
48
+ }
49
+ return new Promise(function (resolve, reject) {
50
+ if (window.Stytch) {
51
+ console.warn('Stytch.js has already been loaded');
52
+ resolve(window.Stytch);
48
53
  }
49
- var scriptEl = document.createElement('script');
50
- scriptEl.setAttribute('src', src);
51
- Object.keys(attributes).forEach(function (key) {
52
- if (scriptEl[key] === undefined) {
53
- scriptEl.setAttribute(key, attributes[key]);
54
+ var script = findOrCreateScript();
55
+ script.addEventListener('load', function () {
56
+ if (window.Stytch) {
57
+ resolve(window.Stytch);
54
58
  }
55
59
  else {
56
- scriptEl[key] = attributes[key];
60
+ reject(new Error('Stytch.js could not be loaded'));
57
61
  }
58
62
  });
59
- var handleLoad = function () {
60
- setLoading(false);
61
- };
62
- var handleError = function (error) {
63
- setError(error);
64
- };
65
- scriptEl.addEventListener('load', handleLoad);
66
- scriptEl.addEventListener('error', handleError);
67
- document.body.appendChild(scriptEl);
68
- return function () {
69
- scriptEl.removeEventListener('load', handleLoad);
70
- scriptEl.removeEventListener('error', handleError);
71
- };
72
- // we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call
73
- // eslint-disable-next-line react-hooks/exhaustive-deps
74
- }, [src]);
75
- return [loading, error];
76
- }
77
- exports.default = useScript;
78
- var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
79
-
80
- });
81
-
82
- unwrapExports(useScript_1);
83
-
84
- var lib = createCommonjsModule(function (module, exports) {
85
- var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
86
- return (mod && mod.__esModule) ? mod : { "default": mod };
63
+ script.addEventListener('error', function () {
64
+ reject(new Error('Stytch.js could not be loaded'));
65
+ });
66
+ });
87
67
  };
88
- Object.defineProperty(exports, "__esModule", { value: true });
89
- exports.default = void 0;
90
-
91
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(useScript_1).default; } });
92
-
68
+ // Execute our own script injection after a tick to give users time to do their
69
+ // own script injection.
70
+ var stytchPromise = Promise.resolve().then(function () { return loadScript(); });
71
+ var loadCalled = false;
72
+ // We will log a warning if it doesn't look like the host code has checked or invoked the promise
73
+ stytchPromise.catch(function (err) {
74
+ if (!loadCalled) {
75
+ console.warn(err);
76
+ }
93
77
  });
78
+ var loadStytch = function () {
79
+ loadCalled = true;
80
+ return stytchPromise;
81
+ };
94
82
 
95
- var useScript = unwrapExports(lib);
96
-
97
- var STYTCH_JS_URL = "https://js.stytch.com/stytch.js";
98
-
83
+ // Callback types
84
+ var EventType;
85
+ (function (EventType) {
86
+ EventType["CallbackEvent"] = "CALLBACK_EVENT";
87
+ EventType["ErrorEvent"] = "ERROR_EVENT";
88
+ EventType["SuccessEvent"] = "SUCCESS_EVENT";
89
+ })(EventType || (EventType = {}));
90
+ var CallbackEventType;
91
+ (function (CallbackEventType) {
92
+ CallbackEventType["UserEventType"] = "USER_EVENT_TYPE";
93
+ })(CallbackEventType || (CallbackEventType = {}));
94
+ exports.SDKProductTypes = void 0;
95
+ (function (SDKProductTypes) {
96
+ SDKProductTypes["emailMagicLinks"] = "emailMagicLinks";
97
+ SDKProductTypes["oauth"] = "oauth";
98
+ })(exports.SDKProductTypes || (exports.SDKProductTypes = {}));
99
+ exports.OAuthProvidersTypes = void 0;
100
+ (function (OAuthProvidersTypes) {
101
+ OAuthProvidersTypes["Google"] = "google";
102
+ OAuthProvidersTypes["Microsoft"] = "microsoft";
103
+ OAuthProvidersTypes["Apple"] = "apple";
104
+ })(exports.OAuthProvidersTypes || (exports.OAuthProvidersTypes = {}));
105
+ var OneTapPositions;
106
+ (function (OneTapPositions) {
107
+ OneTapPositions["embedded"] = "embedded";
108
+ OneTapPositions["floating"] = "floating";
109
+ })(OneTapPositions || (OneTapPositions = {}));
110
+
111
+ var useStytchFactory = function () {
112
+ var _a = React__default['default'].useState(), stytchFactory = _a[0], setStytchFactory = _a[1];
113
+ React__default['default'].useEffect(function () {
114
+ // If we pass the function in directly React will execute it as a SetStateAction
115
+ // Need to wrap in an anon - see https://medium.com/swlh/how-to-store-a-function-with-the-usestate-hook-in-react-8a88dd4eede1
116
+ loadStytch().then(function (globalStytch) { return setStytchFactory(function () { return globalStytch; }); });
117
+ }, []);
118
+ return stytchFactory;
119
+ };
120
+ var useUniqueElementId = function () {
121
+ var elementId = React__default['default'].useState(function () {
122
+ var randId = Math.floor(Math.random() * 1e6);
123
+ return "stytch-magic-link-" + randId;
124
+ })[0];
125
+ return elementId;
126
+ };
99
127
  /**
100
128
  * Stytch JS React Component
101
129
  *
102
130
  * [Documentation](https://stytch.com/docs/javascript-sdk)
103
131
  */
104
132
  var Stytch = function (props) {
105
- // url check
106
- var url = props._url || STYTCH_JS_URL;
107
- if (url !== STYTCH_JS_URL) {
108
- console.warn("Please do not change _url prop. Changing _url prop will result in Stytch SDK not working properly.");
109
- if (!props._url.endsWith("stytch.com/stytch.js") &&
110
- !props._url.includes("localhost")) {
111
- return;
112
- }
113
- }
114
- var _a = useScript({
115
- src: url || STYTCH_JS_URL,
116
- checkForExisting: true,
117
- }), loading = _a[0], error = _a[1];
133
+ var StytchFactory = useStytchFactory();
134
+ var elementId = useUniqueElementId();
118
135
  var publicToken = props.publicToken, style = props.style, callbacks = props.callbacks, loginOrSignupView = props.loginOrSignupView;
119
136
  React__default['default'].useEffect(function () {
120
- if (loading) {
121
- return;
122
- }
123
- if (error || !window.Stytch) {
124
- // eslint-disable-next-line no-console
125
- console.error("Error loading Stytch", error);
137
+ if (!StytchFactory) {
126
138
  return;
127
139
  }
128
- else {
129
- // Initialize Stytch.js with your public token. You can find this in your Stytch dashboard under API Keys.
130
- var stytch = window.Stytch(publicToken, callbacks);
131
- if (loginOrSignupView) {
132
- stytch.mount({
133
- elementId: "#stytch-magic-link",
134
- style: style,
135
- loginOrSignupView: loginOrSignupView,
136
- });
137
- }
138
- else {
139
- console.error("loginOrSignupView is a required prop");
140
- }
141
- }
142
- }, [loading]);
143
- return React__default['default'].createElement("div", { id: "stytch-magic-link" });
140
+ var stytch = StytchFactory(publicToken, callbacks);
141
+ stytch.mount({
142
+ elementId: "#" + elementId,
143
+ style: style,
144
+ loginOrSignupView: loginOrSignupView,
145
+ });
146
+ }, [StytchFactory, elementId]);
147
+ return React__default['default'].createElement("div", { id: elementId });
144
148
  };
145
149
 
146
- // Callback types
147
- exports.EventType = void 0;
148
- (function (EventType) {
149
- EventType["CallbackEvent"] = "CALLBACK_EVENT";
150
- EventType["ErrorEvent"] = "ERROR_EVENT";
151
- EventType["SuccessEvent"] = "SUCCESS_EVENT";
152
- })(exports.EventType || (exports.EventType = {}));
153
- exports.CallbackEventType = void 0;
154
- (function (CallbackEventType) {
155
- CallbackEventType["UserEventType"] = "USER_EVENT_TYPE";
156
- })(exports.CallbackEventType || (exports.CallbackEventType = {}));
157
-
158
150
  exports.Stytch = Stytch;
159
151
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../node_modules/react-script-hook/lib/use-script.js","../node_modules/react-script-hook/lib/index.js","../src/constants.tsx","../src/Stytch.tsx","../src/types/index.ts"],"sourcesContent":["\"use strict\";\nvar __rest = (this && this.__rest) || function (s, e) {\n var t = {};\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\n t[p] = s[p];\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\n t[p[i]] = s[p[i]];\n }\n return t;\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar react_1 = require(\"react\");\nfunction useScript(_a) {\n var src = _a.src, _b = _a.checkForExisting, checkForExisting = _b === void 0 ? false : _b, attributes = __rest(_a, [\"src\", \"checkForExisting\"]);\n var _c = react_1.useState(Boolean(src)), loading = _c[0], setLoading = _c[1];\n var _d = react_1.useState(null), error = _d[0], setError = _d[1];\n react_1.useEffect(function () {\n if (!isBrowser || !src)\n return;\n if (checkForExisting) {\n var existing = document.querySelectorAll(\"script[src=\\\"\" + src + \"\\\"]\");\n if (existing.length > 0) {\n setLoading(false);\n return;\n }\n }\n var scriptEl = document.createElement('script');\n scriptEl.setAttribute('src', src);\n Object.keys(attributes).forEach(function (key) {\n if (scriptEl[key] === undefined) {\n scriptEl.setAttribute(key, attributes[key]);\n }\n else {\n scriptEl[key] = attributes[key];\n }\n });\n var handleLoad = function () {\n setLoading(false);\n };\n var handleError = function (error) {\n setError(error);\n };\n scriptEl.addEventListener('load', handleLoad);\n scriptEl.addEventListener('error', handleError);\n document.body.appendChild(scriptEl);\n return function () {\n scriptEl.removeEventListener('load', handleLoad);\n scriptEl.removeEventListener('error', handleError);\n };\n // we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [src]);\n return [loading, error];\n}\nexports.default = useScript;\nvar isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';\n//# sourceMappingURL=use-script.js.map","\"use strict\";\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.default = void 0;\nvar use_script_1 = require(\"./use-script\");\nObject.defineProperty(exports, \"default\", { enumerable: true, get: function () { return __importDefault(use_script_1).default; } });\n//# sourceMappingURL=index.js.map","export const STYTCH_JS_URL = \"https://js.stytch.com/stytch.js\";\n","import React from \"react\";\nimport useScript from \"react-script-hook\";\nimport { STYTCH_JS_URL } from \"./constants\";\nimport { StytchProps } from \"./types\";\n\n/**\n * Stytch JS React Component\n *\n * [Documentation](https://stytch.com/docs/javascript-sdk)\n */\nexport const Stytch = (props: StytchProps) => {\n // url check\n const url = props._url || STYTCH_JS_URL;\n if (url !== STYTCH_JS_URL) {\n console.warn(\n \"Please do not change _url prop. Changing _url prop will result in Stytch SDK not working properly.\"\n );\n if (\n !props._url.endsWith(\"stytch.com/stytch.js\") &&\n !props._url.includes(\"localhost\")\n ) {\n return;\n }\n }\n\n const [loading, error] = useScript({\n src: url || STYTCH_JS_URL,\n checkForExisting: true,\n });\n const { publicToken, style, callbacks, loginOrSignupView } = props;\n\n React.useEffect(() => {\n if (loading) {\n return;\n }\n if (error || !window.Stytch) {\n // eslint-disable-next-line no-console\n console.error(\"Error loading Stytch\", error);\n return;\n } else {\n // Initialize Stytch.js with your public token. You can find this in your Stytch dashboard under API Keys.\n var stytch = window.Stytch(publicToken, callbacks);\n if (loginOrSignupView) {\n stytch.mount({\n elementId: \"#stytch-magic-link\",\n style,\n loginOrSignupView,\n });\n } else {\n console.error(\"loginOrSignupView is a required prop\");\n }\n }\n }, [loading]);\n\n return <div id=\"stytch-magic-link\"></div>;\n};\n","declare global {\n interface Window {\n Stytch: (publicToken: string, options: CallbackOptions) => StytchClient;\n }\n}\n\n// Client object types\nexport interface StytchClient {\n mount: (mountArgs: StytchMountArgs) => void;\n}\n\nexport interface StytchProps {\n publicToken: string;\n loginOrSignupView: LoginOrSignupView;\n style?: StyleConfig;\n callbacks?: CallbackOptions;\n // Do not change this prop. This defaults to https://js.stytch.com/stytch.js\n _url?: string;\n}\n\nexport interface StytchMountArgs {\n elementId: string;\n loginOrSignupView: LoginOrSignupView;\n style: StyleConfig;\n}\n\n// Config types\nexport interface LoginConfig {\n magicLinkUrl: string;\n expirationMinutes?: number;\n emailTemplateId?: string;\n}\n\nexport interface CreateUserConfig {\n magicLinkUrl: string;\n expirationMinutes?: number;\n emailTemplateId?: string;\n createUserAsPending?: boolean;\n}\n\nexport interface LoginOrCreateUserConfig {\n loginConfig: LoginConfig;\n createUserConfig: CreateUserConfig;\n}\n\nexport interface StyleConfig {\n fontFamily?: string;\n width?: string;\n primaryColor?: string;\n primaryTextColor?: string;\n secondaryTextColor?: string;\n lightGrey?: string;\n darkGrey?: string;\n hideHeaderText?: boolean;\n}\n\nexport interface EmailMagicLinksOptions {\n loginRedirectURL: string;\n loginExpirationMinutes: number;\n signupRedirectURL: string;\n signupExpirationMinutes: number;\n createUserAsPending: boolean;\n}\n\nexport interface LoginOrSignupView {\n products: SDKProductTypes[];\n emailMagicLinksOptions: EmailMagicLinksOptions;\n}\n\n// Callback types\nexport enum EventType {\n CallbackEvent = \"CALLBACK_EVENT\",\n ErrorEvent = \"ERROR_EVENT\",\n SuccessEvent = \"SUCCESS_EVENT\",\n}\nexport enum CallbackEventType {\n UserEventType = \"USER_EVENT_TYPE\",\n}\nexport interface UserEventData {\n type: CallbackEventType.UserEventType;\n email: string;\n emailId: string;\n userId: string;\n}\nexport interface MagicLinkSentData {\n message: string;\n}\nexport interface StytchError {\n message: string;\n}\nexport interface EventData {\n eventType: EventType.CallbackEvent;\n eventData: UserEventData;\n}\nexport interface SuccessData {\n eventType: EventType.SuccessEvent;\n eventData: MagicLinkSentData;\n}\nexport interface ErrorData {\n eventType: EventType.ErrorEvent;\n error: StytchError;\n}\nexport interface CallbackOptions {\n onEvent?: (data?: EventData) => void | Promise<void>;\n onSuccess?: (data?: SuccessData) => void | Promise<void>;\n onError?: (data?: ErrorData) => void | Promise<void>;\n}\n\nexport type SDKProductTypes = \"emailMagicLinks\";\n"],"names":["this","react_1","use_script_1","React","EventType","CallbackEventType"],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,IAAI,MAAM,GAAG,CAACA,cAAI,IAAIA,cAAI,CAAC,MAAM,KAAK,UAAU,CAAC,EAAE,CAAC,EAAE;AACtD,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;AACf,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;AACvF,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AACpB,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,OAAO,MAAM,CAAC,qBAAqB,KAAK,UAAU;AACvE,QAAQ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AAChF,YAAY,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1F,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAClC,SAAS;AACT,IAAI,OAAO,CAAC,CAAC;AACb,CAAC,CAAC;AACF,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI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X,OAAO;SACR;QACD,IAAI,KAAK,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;;YAE3B,OAAO,CAAC,KAAK,CAAC,sBAAsB,EAAE,KAAK,CAAC,CAAC;YAC7C,OAAO;SACR;aAAM;;YAEL,IAAI,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;YACnD,IAAI,iBAAiB,EAAE;gBACrB,MAAM,CAAC,KAAK,CAAC;oBACX,SAAS,EAAE,oBAAoB;oBAC/B,KAAK,OAAA;oBACL,iBAAiB,mBAAA;iBAClB,CAAC,CAAC;aACJ;iBAAM;gBACL,OAAO,CAAC,KAAK,CAAC,sCAAsC,CAAC,CAAC;aACvD;SACF;KACF,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,OAAOA,iDAAK,EAAE,EAAC,mBAAmB,GAAO,CAAC;AAC5C;;ACcA;AACYC;AAAZ,WAAY,SAAS;IACnB,6CAAgC,CAAA;IAChC,uCAA0B,CAAA;IAC1B,2CAA8B,CAAA;AAChC,CAAC,EAJWA,iBAAS,KAATA,iBAAS,QAIpB;AACWC;AAAZ,WAAY,iBAAiB;IAC3B,sDAAiC,CAAA;AACnC,CAAC,EAFWA,yBAAiB,KAAjBA,yBAAiB;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../node_modules/@stytch/stytch-js/dist/index.esm.js","../src/Stytch.tsx"],"sourcesContent":["var PUBLIC_URL = 'https://js.stytch.com/stytch.js';\nfunction getJSUrl() {\n try {\n if (process.env.STYTCH_JS_SDK_URL) {\n return process.env.STYTCH_JS_SDK_URL;\n }\n if (process.env.REACT_APP_STYTCH_JS_SDK_URL) {\n return process.env.REACT_APP_STYTCH_JS_SDK_URL;\n }\n if (process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL) {\n return process.env.NEXT_PUBLIC_STYTCH_JS_SDK_URL;\n }\n if (process.env.STORYBOOK_STYTCH_JS_SDK_URL) {\n return process.env.STORYBOOK_STYTCH_JS_SDK_URL;\n }\n return PUBLIC_URL;\n }\n catch (err) {\n return PUBLIC_URL;\n }\n}\nvar STYTCH_JS_URL = getJSUrl();\n\nvar findOrCreateScript = function () {\n var scripts = document.querySelectorAll(\"script[src=\\\"\" + STYTCH_JS_URL + \"\\\"]\");\n if (scripts[0]) {\n return scripts[0];\n }\n var script = document.createElement('script');\n script.src = STYTCH_JS_URL;\n document.head.appendChild(script);\n return script;\n};\nvar loadScript = function () {\n var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';\n if (!isBrowser) {\n return Promise.resolve(null);\n }\n return new Promise(function (resolve, reject) {\n if (window.Stytch) {\n console.warn('Stytch.js has already been loaded');\n resolve(window.Stytch);\n }\n var script = findOrCreateScript();\n script.addEventListener('load', function () {\n if (window.Stytch) {\n resolve(window.Stytch);\n }\n else {\n reject(new Error('Stytch.js could not be loaded'));\n }\n });\n script.addEventListener('error', function () {\n reject(new Error('Stytch.js could not be loaded'));\n });\n });\n};\n// Execute our own script injection after a tick to give users time to do their\n// own script injection.\nvar stytchPromise = Promise.resolve().then(function () { return loadScript(); });\nvar loadCalled = false;\n// We will log a warning if it doesn't look like the host code has checked or invoked the promise\nstytchPromise.catch(function (err) {\n if (!loadCalled) {\n console.warn(err);\n }\n});\nvar loadStytch = function () {\n loadCalled = true;\n return stytchPromise;\n};\n\n// Callback types\nvar EventType;\n(function (EventType) {\n EventType[\"CallbackEvent\"] = \"CALLBACK_EVENT\";\n EventType[\"ErrorEvent\"] = \"ERROR_EVENT\";\n EventType[\"SuccessEvent\"] = \"SUCCESS_EVENT\";\n})(EventType || (EventType = {}));\nvar CallbackEventType;\n(function (CallbackEventType) {\n CallbackEventType[\"UserEventType\"] = \"USER_EVENT_TYPE\";\n})(CallbackEventType || (CallbackEventType = {}));\nvar SDKProductTypes;\n(function (SDKProductTypes) {\n SDKProductTypes[\"emailMagicLinks\"] = \"emailMagicLinks\";\n SDKProductTypes[\"oauth\"] = \"oauth\";\n})(SDKProductTypes || (SDKProductTypes = {}));\nvar OAuthProvidersTypes;\n(function (OAuthProvidersTypes) {\n OAuthProvidersTypes[\"Google\"] = \"google\";\n OAuthProvidersTypes[\"Microsoft\"] = \"microsoft\";\n OAuthProvidersTypes[\"Apple\"] = \"apple\";\n})(OAuthProvidersTypes || (OAuthProvidersTypes = {}));\nvar OneTapPositions;\n(function (OneTapPositions) {\n OneTapPositions[\"embedded\"] = \"embedded\";\n OneTapPositions[\"floating\"] = \"floating\";\n})(OneTapPositions || (OneTapPositions = {}));\n\nexport { CallbackEventType, EventType, OAuthProvidersTypes, OneTapPositions, SDKProductTypes, loadStytch };\n//# sourceMappingURL=index.esm.js.map\n","import React from \"react\";\nimport {loadStytch, LoginOrSignupView, StyleConfig, CallbackOptions, StytchFactory} from '@stytch/stytch-js'\n\nexport {SDKProductTypes, OAuthProvidersTypes} from '@stytch/stytch-js'\n\nexport interface StytchProps {\n publicToken: string;\n loginOrSignupView?: LoginOrSignupView;\n style?: StyleConfig;\n callbacks?: CallbackOptions;\n}\n\nconst useStytchFactory = (): StytchFactory => {\n const [stytchFactory, setStytchFactory] = React.useState<StytchFactory>();\n React.useEffect(() => {\n // If we pass the function in directly React will execute it as a SetStateAction\n // Need to wrap in an anon - see https://medium.com/swlh/how-to-store-a-function-with-the-usestate-hook-in-react-8a88dd4eede1\n loadStytch().then(globalStytch => setStytchFactory(() => globalStytch))\n }, []);\n\n return stytchFactory;\n}\n\nconst useUniqueElementId = (): string => {\n const [elementId] = React.useState<string>(() => {\n const randId = Math.floor(Math.random() * 1e6);\n return `stytch-magic-link-${randId}`;\n });\n return elementId;\n}\n\n/**\n * Stytch JS React Component\n *\n * [Documentation](https://stytch.com/docs/javascript-sdk)\n */\nexport const Stytch = (props: StytchProps) => {\n const StytchFactory = useStytchFactory();\n const elementId = useUniqueElementId();\n\n const {publicToken, style, callbacks, loginOrSignupView} = props;\n\n React.useEffect(() => {\n if (!StytchFactory) {\n return;\n }\n const stytch = StytchFactory(publicToken, callbacks);\n\n stytch.mount({\n elementId: `#${elementId}`,\n style,\n loginOrSignupView,\n });\n }, [StytchFactory, elementId]);\n\n return <div id={elementId}></div>;\n};\n"],"names":["SDKProductTypes","OAuthProvidersTypes","React"],"mappings":";;;;;;;;;;AAAA,IAAI,UAAU,GAAG,iCAAiC,CAAC;AACnD,SAAS,QAAQ,GAAG;AACpB,IAAI,IAAI;AACR,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE;AAC3C,YAAY,OAAO,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;AACjD,SAAS;AACT,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE;AACrD,YAAY,OAAO,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC;AAC3D,SAAS;AACT,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE;AACvD,YAAY,OAAO,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC;AAC7D,SAAS;AACT,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE;AACrD,YAAY,OAAO,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC;AAC3D,SAAS;AACT,QAAQ,OAAO,UAAU,CAAC;AAC1B,KAAK;AACL,IAAI,OAAO,GAAG,EAAE;AAChB,QAAQ,OAAO,UAAU,CAAC;AAC1B,KAAK;AACL,CAAC;AACD,IAAI,aAAa,GAAG,QAAQ,EAAE,CAAC;AAC/B;AACA,IAAI,kBAAkB,GAAG,YAAY;AACrC,IAAI,IAAI,OAAO,GAAG,QAAQ,CAAC,gBAAgB,CAAC,eAAe,GAAG,aAAa,GAAG,KAAK,CAAC,CAAC;AACrF,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE;AACpB,QAAQ,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1B,KAAK;AACL,IAAI,IAAI,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC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AK,MAAvC,EAAE,SAAS,GAAuB,KAAK,UAA5B,EAAE,iBAAiB,GAAI,KAAK,kBAAT,CAAU;IAEjEA,yBAAK,CAAC,SAAS,CAAC;QACd,IAAI,CAAC,aAAa,EAAE;YAClB,OAAO;SACR;QACD,IAAM,MAAM,GAAG,aAAa,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;QAErD,MAAM,CAAC,KAAK,CAAC;YACX,SAAS,EAAE,MAAI,SAAW;YAC1B,KAAK,OAAA;YACL,iBAAiB,mBAAA;SAClB,CAAC,CAAC;KACJ,EAAE,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC,CAAC;IAE/B,OAAOA,iDAAK,EAAE,EAAE,SAAS,GAAQ,CAAC;AACpC;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stytch/stytch-react",
3
- "version": "2.0.0",
4
- "description": "Stytch.js React Bindings",
3
+ "version": "3.0.3",
4
+ "description": "Stytch's official React Library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "scripts": {
@@ -9,17 +9,18 @@
9
9
  "build": "rollup -c",
10
10
  "prepare": "rollup -c"
11
11
  },
12
- "author": "Nathan",
12
+ "author": "Stytch",
13
13
  "license": "MIT",
14
14
  "devDependencies": {
15
- "@babel/core": "^7.12.17",
16
- "@babel/preset-env": "^7.12.17",
15
+ "@babel/core": "^7.15.5",
16
+ "@babel/preset-env": "^7.15.6",
17
17
  "@babel/preset-react": "^7.12.13",
18
- "@babel/preset-typescript": "^7.13.0",
19
- "@rollup/plugin-typescript": "^8.2.0",
20
- "@types/enzyme": "^3.10.8",
18
+ "@babel/preset-typescript": "^7.15.0",
19
+ "@rollup/plugin-typescript": "^8.2.5",
20
+ "@types/enzyme": "^3.10.10",
21
21
  "@types/enzyme-adapter-react-16": "^1.0.6",
22
22
  "@types/jest": "^26.0.20",
23
+ "@types/react-dom": "^17.0.10",
23
24
  "@wessberg/rollup-plugin-ts": "^1.3.8",
24
25
  "babel-loader": "^8.2.2",
25
26
  "enzyme": "^3.11.0",
@@ -27,13 +28,11 @@
27
28
  "jest": "^26.6.3",
28
29
  "react": "^16.14.0",
29
30
  "react-dom": "^16.14.0",
30
- "rollup": "^2.42.4",
31
+ "rollup": "^2.56.3",
31
32
  "rollup-plugin-babel": "^4.4.0",
32
33
  "rollup-plugin-commonjs": "^10.1.0",
33
34
  "rollup-plugin-node-resolve": "^5.2.0",
34
- "styled-jsx": "^3.4.4",
35
- "tslib": "^2.1.0",
36
- "typescript": "^4.2.3"
35
+ "typescript": "^4.4.4"
37
36
  },
38
37
  "repository": {
39
38
  "type": "git",
@@ -51,7 +50,7 @@
51
50
  "package.json"
52
51
  ],
53
52
  "dependencies": {
54
- "react-script-hook": "^1.4.1"
53
+ "@stytch/stytch-js": "^2.0.4"
55
54
  },
56
55
  "peerDependencies": {
57
56
  "react": ">= 16.x.x"
package/src/Stytch.tsx CHANGED
@@ -1,7 +1,33 @@
1
1
  import React from "react";
2
- import useScript from "react-script-hook";
3
- import { STYTCH_JS_URL } from "./constants";
4
- import { StytchProps } from "./types";
2
+ import {loadStytch, LoginOrSignupView, StyleConfig, CallbackOptions, StytchFactory} from '@stytch/stytch-js'
3
+
4
+ export {SDKProductTypes, OAuthProvidersTypes} from '@stytch/stytch-js'
5
+
6
+ export interface StytchProps {
7
+ publicToken: string;
8
+ loginOrSignupView?: LoginOrSignupView;
9
+ style?: StyleConfig;
10
+ callbacks?: CallbackOptions;
11
+ }
12
+
13
+ const useStytchFactory = (): StytchFactory => {
14
+ const [stytchFactory, setStytchFactory] = React.useState<StytchFactory>();
15
+ React.useEffect(() => {
16
+ // If we pass the function in directly React will execute it as a SetStateAction
17
+ // Need to wrap in an anon - see https://medium.com/swlh/how-to-store-a-function-with-the-usestate-hook-in-react-8a88dd4eede1
18
+ loadStytch().then(globalStytch => setStytchFactory(() => globalStytch))
19
+ }, []);
20
+
21
+ return stytchFactory;
22
+ }
23
+
24
+ const useUniqueElementId = (): string => {
25
+ const [elementId] = React.useState<string>(() => {
26
+ const randId = Math.floor(Math.random() * 1e6);
27
+ return `stytch-magic-link-${randId}`;
28
+ });
29
+ return elementId;
30
+ }
5
31
 
6
32
  /**
7
33
  * Stytch JS React Component
@@ -9,48 +35,23 @@ import { StytchProps } from "./types";
9
35
  * [Documentation](https://stytch.com/docs/javascript-sdk)
10
36
  */
11
37
  export const Stytch = (props: StytchProps) => {
12
- // url check
13
- const url = props._url || STYTCH_JS_URL;
14
- if (url !== STYTCH_JS_URL) {
15
- console.warn(
16
- "Please do not change _url prop. Changing _url prop will result in Stytch SDK not working properly."
17
- );
18
- if (
19
- !props._url.endsWith("stytch.com/stytch.js") &&
20
- !props._url.includes("localhost")
21
- ) {
22
- return;
23
- }
24
- }
38
+ const StytchFactory = useStytchFactory();
39
+ const elementId = useUniqueElementId();
25
40
 
26
- const [loading, error] = useScript({
27
- src: url || STYTCH_JS_URL,
28
- checkForExisting: true,
29
- });
30
- const { publicToken, style, callbacks, loginOrSignupView } = props;
41
+ const {publicToken, style, callbacks, loginOrSignupView} = props;
31
42
 
32
43
  React.useEffect(() => {
33
- if (loading) {
44
+ if (!StytchFactory) {
34
45
  return;
35
46
  }
36
- if (error || !window.Stytch) {
37
- // eslint-disable-next-line no-console
38
- console.error("Error loading Stytch", error);
39
- return;
40
- } else {
41
- // Initialize Stytch.js with your public token. You can find this in your Stytch dashboard under API Keys.
42
- var stytch = window.Stytch(publicToken, callbacks);
43
- if (loginOrSignupView) {
44
- stytch.mount({
45
- elementId: "#stytch-magic-link",
46
- style,
47
- loginOrSignupView,
48
- });
49
- } else {
50
- console.error("loginOrSignupView is a required prop");
51
- }
52
- }
53
- }, [loading]);
47
+ const stytch = StytchFactory(publicToken, callbacks);
48
+
49
+ stytch.mount({
50
+ elementId: `#${elementId}`,
51
+ style,
52
+ loginOrSignupView,
53
+ });
54
+ }, [StytchFactory, elementId]);
54
55
 
55
- return <div id="stytch-magic-link"></div>;
56
+ return <div id={elementId}></div>;
56
57
  };
package/src/index.ts CHANGED
@@ -1,2 +1 @@
1
- export { Stytch } from "./Stytch";
2
- export * from "./types";
1
+ export { Stytch, StytchProps, SDKProductTypes, OAuthProvidersTypes } from "./Stytch";
package/src/constants.tsx DELETED
@@ -1 +0,0 @@
1
- export const STYTCH_JS_URL = "https://js.stytch.com/stytch.js";
@@ -1,109 +0,0 @@
1
- declare global {
2
- interface Window {
3
- Stytch: (publicToken: string, options: CallbackOptions) => StytchClient;
4
- }
5
- }
6
-
7
- // Client object types
8
- export interface StytchClient {
9
- mount: (mountArgs: StytchMountArgs) => void;
10
- }
11
-
12
- export interface StytchProps {
13
- publicToken: string;
14
- loginOrSignupView: LoginOrSignupView;
15
- style?: StyleConfig;
16
- callbacks?: CallbackOptions;
17
- // Do not change this prop. This defaults to https://js.stytch.com/stytch.js
18
- _url?: string;
19
- }
20
-
21
- export interface StytchMountArgs {
22
- elementId: string;
23
- loginOrSignupView: LoginOrSignupView;
24
- style: StyleConfig;
25
- }
26
-
27
- // Config types
28
- export interface LoginConfig {
29
- magicLinkUrl: string;
30
- expirationMinutes?: number;
31
- emailTemplateId?: string;
32
- }
33
-
34
- export interface CreateUserConfig {
35
- magicLinkUrl: string;
36
- expirationMinutes?: number;
37
- emailTemplateId?: string;
38
- createUserAsPending?: boolean;
39
- }
40
-
41
- export interface LoginOrCreateUserConfig {
42
- loginConfig: LoginConfig;
43
- createUserConfig: CreateUserConfig;
44
- }
45
-
46
- export interface StyleConfig {
47
- fontFamily?: string;
48
- width?: string;
49
- primaryColor?: string;
50
- primaryTextColor?: string;
51
- secondaryTextColor?: string;
52
- lightGrey?: string;
53
- darkGrey?: string;
54
- hideHeaderText?: boolean;
55
- }
56
-
57
- export interface EmailMagicLinksOptions {
58
- loginRedirectURL: string;
59
- loginExpirationMinutes: number;
60
- signupRedirectURL: string;
61
- signupExpirationMinutes: number;
62
- createUserAsPending: boolean;
63
- }
64
-
65
- export interface LoginOrSignupView {
66
- products: SDKProductTypes[];
67
- emailMagicLinksOptions: EmailMagicLinksOptions;
68
- }
69
-
70
- // Callback types
71
- export enum EventType {
72
- CallbackEvent = "CALLBACK_EVENT",
73
- ErrorEvent = "ERROR_EVENT",
74
- SuccessEvent = "SUCCESS_EVENT",
75
- }
76
- export enum CallbackEventType {
77
- UserEventType = "USER_EVENT_TYPE",
78
- }
79
- export interface UserEventData {
80
- type: CallbackEventType.UserEventType;
81
- email: string;
82
- emailId: string;
83
- userId: string;
84
- }
85
- export interface MagicLinkSentData {
86
- message: string;
87
- }
88
- export interface StytchError {
89
- message: string;
90
- }
91
- export interface EventData {
92
- eventType: EventType.CallbackEvent;
93
- eventData: UserEventData;
94
- }
95
- export interface SuccessData {
96
- eventType: EventType.SuccessEvent;
97
- eventData: MagicLinkSentData;
98
- }
99
- export interface ErrorData {
100
- eventType: EventType.ErrorEvent;
101
- error: StytchError;
102
- }
103
- export interface CallbackOptions {
104
- onEvent?: (data?: EventData) => void | Promise<void>;
105
- onSuccess?: (data?: SuccessData) => void | Promise<void>;
106
- onError?: (data?: ErrorData) => void | Promise<void>;
107
- }
108
-
109
- export type SDKProductTypes = "emailMagicLinks";