@descope/react-sdk 0.0.52-alpha.2 → 0.0.52-alpha.21
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 +21 -0
- package/README.md +145 -12
- package/dist/cjs/index.cjs.js +2 -0
- package/dist/cjs/index.cjs.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/index.d.ts +33 -18
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +100 -78
- package/dist/index.js +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Descope
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,29 +1,56 @@
|
|
|
1
1
|
# @descope/react-sdk
|
|
2
|
+
|
|
2
3
|
This library lets you consume your login pages created by Descope console-app in your application
|
|
3
4
|
Under the hood, it uses [web-js-sdk](https://github.com/descope/web-js-sdk)
|
|
4
5
|
|
|
5
6
|
## Usage
|
|
7
|
+
|
|
6
8
|
### Install the package
|
|
9
|
+
|
|
7
10
|
```bash
|
|
8
11
|
npm install @descope/react-sdk
|
|
9
12
|
```
|
|
10
13
|
|
|
11
14
|
### Render it in your application
|
|
15
|
+
|
|
12
16
|
#### Wrap your app with Auth Provider
|
|
17
|
+
|
|
13
18
|
```js
|
|
14
|
-
import { AuthProvider } from '@descope/react-sdk'
|
|
19
|
+
import { AuthProvider } from '@descope/react-sdk';
|
|
15
20
|
|
|
16
21
|
const AppRoot = () => {
|
|
22
|
+
return (
|
|
23
|
+
<AuthProvider projectId="myProjectId">
|
|
24
|
+
<App />
|
|
25
|
+
</AuthProvider>
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
#### Use Descope to render specific flow
|
|
31
|
+
|
|
32
|
+
You can use **default flows** or **provide flow id** directly to the Descope component
|
|
33
|
+
|
|
34
|
+
##### 1. Default flows
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { SignInFlow } from '@descope/react-sdk'
|
|
38
|
+
// you can choose flow to run from the following
|
|
39
|
+
// import { SignUpFlow } from '@descope/react-sdk'
|
|
40
|
+
// import { SignUpOrInFlow } from '@descope/react-sdk'
|
|
41
|
+
|
|
42
|
+
const App = () => {
|
|
17
43
|
return (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
44
|
+
{...}
|
|
45
|
+
<SignInFlow
|
|
46
|
+
onSuccess={(e) => console.log('Logged in!')}
|
|
47
|
+
onError={(e) => console.log('Could not logged in!')}
|
|
48
|
+
/>
|
|
23
49
|
)
|
|
24
50
|
}
|
|
25
51
|
```
|
|
26
|
-
|
|
52
|
+
|
|
53
|
+
##### 2. Provide flow id
|
|
27
54
|
|
|
28
55
|
```js
|
|
29
56
|
import { Descope } from '@descope/react-sdk'
|
|
@@ -31,14 +58,120 @@ import { Descope } from '@descope/react-sdk'
|
|
|
31
58
|
const App = () => {
|
|
32
59
|
return (
|
|
33
60
|
{...}
|
|
34
|
-
<Descope
|
|
35
|
-
flowId="myFlowId"
|
|
61
|
+
<Descope
|
|
62
|
+
flowId="myFlowId"
|
|
36
63
|
onSuccess={(e) => console.log('Logged in!')}
|
|
37
64
|
onError={(e) => console.log('Could not logged in!')}
|
|
38
|
-
|
|
65
|
+
// theme can be "light" or "dark". If empty, Descope will use the OS theme
|
|
66
|
+
// theme="light"
|
|
67
|
+
/>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### Use the `useAuth` hook in your components in order to access authentication state and utilities
|
|
73
|
+
|
|
74
|
+
This can be helpful to implement application-specific logic. Examples:
|
|
75
|
+
|
|
76
|
+
- Render different components if current session is authenticated
|
|
77
|
+
- Render user's content
|
|
78
|
+
- Logout button
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import { useAuth } from '@descope/react-sdk'
|
|
82
|
+
|
|
83
|
+
const App = () => {
|
|
84
|
+
// NOTE - `useAuth` should be used inside `AuthProvider` context,
|
|
85
|
+
// and will throw an exception if this requirement is not met
|
|
86
|
+
const { authenticated, user, logout } = useAuth()
|
|
87
|
+
return (
|
|
88
|
+
{...}
|
|
89
|
+
{
|
|
90
|
+
// render different components if current session is authenticated
|
|
91
|
+
authenticated && <MyPrivateComponent />
|
|
92
|
+
}
|
|
93
|
+
{
|
|
94
|
+
// render user's content
|
|
95
|
+
authenticated && <div>Hello ${user.name}</div>
|
|
96
|
+
}
|
|
97
|
+
{
|
|
98
|
+
// logout button
|
|
99
|
+
authenticated && <button onClick={logout}>Logout</div>
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Session token server validation (pass session token to server API)
|
|
106
|
+
|
|
107
|
+
When developing a full-stack application, it is common to have private server API which requires a valid session token:
|
|
108
|
+
|
|
109
|
+

|
|
110
|
+
|
|
111
|
+
Note: Descope also provides server-side SDKs in various languages (NodeJS, Go, Python, etc). Descope's server SDKs have out-of-the-box session validation API that supports the options described bellow. To read more about session validation, Read [this section](https://docs.descope.com/guides/gettingstarted/#session-validation) in Descope documentation.
|
|
112
|
+
|
|
113
|
+
The mechanism to pass session token depends on the Descope project's "Token response method" configuration.
|
|
114
|
+
|
|
115
|
+
##### 1. Manage in cookies
|
|
116
|
+
|
|
117
|
+
- Descope sets session token as cookie, which automatically sent each server api request. This option is more secure and is the recommended method for managing tokens, but for this option to work well with the browser - you must also configure a CNAME record for the custom domain listed, which will give a unified log in experience and securely restrict access to the session tokens that are stored in the cookies.
|
|
118
|
+
|
|
119
|
+
When this option is configured, the browser will automatically add the session token cookie to the server in every request.
|
|
120
|
+
|
|
121
|
+
##### 2. Manage in response body
|
|
122
|
+
|
|
123
|
+
- Descope API returns session token in body. In this option, The React application should pass session cookie (`const { sessionToken } = useAuth()`) as Authorization header. This option never requires a custom domain, and is recommended for testing or while working in a sandbox environment.
|
|
124
|
+
|
|
125
|
+
An example for using session token,
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
import { useAuth } from '@descope/react-sdk'
|
|
129
|
+
import { useCallback } from 'react'
|
|
130
|
+
|
|
131
|
+
const App = () => {
|
|
132
|
+
const { sessionToken } = useAuth()
|
|
133
|
+
|
|
134
|
+
const onClick = useCallback(() => {
|
|
135
|
+
fetch('https://localhost:3002/api/some-path' {
|
|
136
|
+
method: 'GET',
|
|
137
|
+
headers: { Authorization: `Bearer ${sessionToken}` }
|
|
138
|
+
})
|
|
139
|
+
},[sessionToken])
|
|
140
|
+
return (
|
|
141
|
+
{...}
|
|
142
|
+
{
|
|
143
|
+
// button that triggers an API that may use session token
|
|
144
|
+
<button onClick={onClick}>Click Me</div>
|
|
145
|
+
}
|
|
39
146
|
)
|
|
40
147
|
}
|
|
41
148
|
```
|
|
42
149
|
|
|
43
|
-
|
|
44
|
-
|
|
150
|
+
## Run a local example
|
|
151
|
+
|
|
152
|
+
There is a simple app that uses Descope React SDK, with two routes
|
|
153
|
+
|
|
154
|
+
- Home
|
|
155
|
+
- Login
|
|
156
|
+
|
|
157
|
+
In order to run this app locally, do the following steps:
|
|
158
|
+
|
|
159
|
+
- Clone this repository
|
|
160
|
+
- Navigate to repository directory
|
|
161
|
+
- Run `npm i`
|
|
162
|
+
- Create a `.env` file with the following variables (or alternatively export them manually):
|
|
163
|
+
|
|
164
|
+
```env
|
|
165
|
+
// .env
|
|
166
|
+
# Your project id
|
|
167
|
+
DESCOPE_PROJECT_ID=<project-id>
|
|
168
|
+
# Flow id to run, e.g. sign-up-or-in
|
|
169
|
+
DESCOPE_FLOW_ID=<flow-id>
|
|
170
|
+
# Optional - Descope base url, e.g. http://localhost:8000
|
|
171
|
+
DESCOPE_BASE_URL=<base-url>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
- Run `npm run start`
|
|
175
|
+
- Go to `http://localhost:3000/` and press the "Start Flow" button
|
|
176
|
+
|
|
177
|
+
Note: if you change env file (for example, change DESCOPE_PROJECT_ID), you need to rerun `npm run start`
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@descope/web-js-sdk"),t=require("react");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function o(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var s=r(e),n=r(t);const u=n.default.createContext(void 0),a=({projectId:e,baseUrl:r,children:o})=>{const[a,l]=t.useState({}),[c,i]=t.useState(""),d=t.useMemo((()=>{if(e)return s.default({projectId:e,baseUrl:r,hooks:{beforeRequest:e=>{const t=e;return t.headers={...t.headers,"x-descope-sdk-name":"react","x-descope-sdk-version":"0.0.52-alpha.21"},t}}})}),[e,r]);t.useEffect((()=>{if(!d)return;const e=d.onSessionTokenChange(i),t=d.onUserChange(l);return()=>{e?.(),t?.()}}),[d]);const f=t.useMemo((()=>({sdk:d,projectId:e,baseUrl:r,user:a,sessionToken:c,setUser:l,setSessionToken:i})),[c,a,e,r]);return n.default.createElement(u.Provider,{value:f},o)};a.defaultProps={baseUrl:"",children:void 0};const l=t.lazy((async()=>(await Promise.resolve().then((function(){return o(require("@descope/web-component"))})),{default:({projectId:e,flowId:t,baseUrl:r,innerRef:o,tenant:s,theme:u})=>n.default.createElement("descope-wc",{"project-id":e,"flow-id":t,"base-url":r,ref:o,tenant:s,theme:u})}))),c=n.default.forwardRef((({flowId:e,onSuccess:r,onError:o,tenant:s,theme:a},c)=>{const[i,d]=t.useState(null);t.useImperativeHandle(c,(()=>i));const{projectId:f,baseUrl:p,setUser:m,setSessionToken:b}=n.default.useContext(u),h=t.useCallback((e=>{m(e.detail?.user);const t=e.detail?.sessionJwt;b(t),r&&r(e)}),[m,b,r]);return t.useEffect((()=>{const e=i;return e?.addEventListener("success",h),o&&e?.addEventListener("error",o),()=>{o&&e?.removeEventListener("error",o),e?.removeEventListener("success",h)}}),[i,o,h]),n.default.createElement(t.Suspense,null,n.default.createElement(l,{projectId:f,flowId:e,baseUrl:p,innerRef:d,tenant:s,theme:a}))}));c.defaultProps={onError:void 0,onSuccess:void 0};const i=e=>(...t)=>{if(!e)throw Error("You can only use this function after sdk initialization. Make sure to supply 'projectId' to <AuthProvider /> component");return e(...t)};exports.AuthProvider=a,exports.Descope=c,exports.SignInFlow=e=>n.default.createElement(c,{...e,flowId:"sign-in"}),exports.SignUpFlow=e=>n.default.createElement(c,{...e,flowId:"sign-up"}),exports.SignUpOrInFlow=e=>n.default.createElement(c,{...e,flowId:"sign-up-or-in"}),exports.useAuth=()=>{const e=n.default.useContext(u);if(!e)throw Error("You can only use 'useAuth' in the context of <AuthProvider />");const{user:r,sessionToken:o,sdk:s}=e,a=t.useCallback(i(s?.logoutAll),[s]),l=t.useCallback(i(s?.logout),[s]),c=t.useCallback(i(s?.me),[s]),d=t.useCallback(i(s?.getJwtPermissions),[s]),f=t.useCallback(i(s?.getJwtRoles),[s]);return t.useMemo((()=>({authenticated:!!o,user:r,sessionToken:o,logoutAll:a,logout:l,me:c,getJwtPermissions:d,getJwtRoles:f})),[r,o,s])};
|
|
2
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../../src/lib/hooks/authContext.ts","../../src/lib/components/AuthProvider.tsx","../../src/lib/components/Descope.tsx","../../src/lib/hooks/useAuth.ts","../../src/lib/components/DefaultFlows.tsx"],"sourcesContent":["import React from 'react';\nimport { IAuthContext } from '../types';\n\nconst AuthContext = React.createContext<IAuthContext>(undefined);\n\nexport default AuthContext;\n","// '@descope/web-js-sdk' is a dependency of '@descope/web-component'\n// and we want to use the same version that is used there\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport createSdk from '@descope/web-js-sdk';\nimport React, { FC, useEffect, useMemo, useState } from 'react';\nimport AuthContext from '../hooks/authContext';\nimport { IAuthContext } from '../types';\n\ndeclare const BUILD_VERSION: string;\n\ninterface IAuthProviderProps {\n\tprojectId: string;\n\tbaseUrl?: string;\n\tchildren?: JSX.Element;\n}\n\nconst AuthProvider: FC<IAuthProviderProps> = ({\n\tprojectId,\n\tbaseUrl,\n\tchildren\n}) => {\n\tconst [user, setUser] = useState({});\n\tconst [sessionToken, setSessionToken] = useState('');\n\n\tconst sdk = useMemo(() => {\n\t\tif (!projectId) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn createSdk({\n\t\t\tprojectId,\n\t\t\tbaseUrl,\n\t\t\thooks: {\n\t\t\t\tbeforeRequest: (config) => {\n\t\t\t\t\tconst conf = config;\n\t\t\t\t\tconf.headers = {\n\t\t\t\t\t\t...conf.headers,\n\t\t\t\t\t\t'x-descope-sdk-name': 'react',\n\t\t\t\t\t\t'x-descope-sdk-version': BUILD_VERSION\n\t\t\t\t\t};\n\t\t\t\t\treturn conf;\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}, [projectId, baseUrl]);\n\n\tuseEffect(() => {\n\t\tif (!sdk) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst unsubscribeSessionToken = sdk.onSessionTokenChange(setSessionToken);\n\t\tconst unsubscribeUser = sdk.onUserChange(setUser);\n\t\treturn () => {\n\t\t\tunsubscribeSessionToken?.();\n\t\t\tunsubscribeUser?.();\n\t\t};\n\t}, [sdk]);\n\n\tconst value = useMemo<IAuthContext>(\n\t\t() => ({\n\t\t\tsdk,\n\t\t\tprojectId,\n\t\t\tbaseUrl,\n\t\t\tuser,\n\t\t\tsessionToken,\n\t\t\tsetUser,\n\t\t\tsetSessionToken\n\t\t}),\n\t\t[sessionToken, user, projectId, baseUrl]\n\t);\n\treturn <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n};\n\nAuthProvider.defaultProps = {\n\tbaseUrl: '',\n\tchildren: undefined\n};\n\nexport default AuthProvider;\n","import React, {\n\tlazy,\n\tSuspense,\n\tuseCallback,\n\tuseEffect,\n\tuseImperativeHandle,\n\tuseState\n} from 'react';\nimport AuthContext from '../hooks/authContext';\nimport { DescopeProps } from '../types';\n\n// web-component code uses browser API, but can be used in SSR apps, hence the lazy loading\nconst DescopeWC = lazy(async () => {\n\tawait import('@descope/web-component');\n\treturn {\n\t\tdefault: ({ projectId, flowId, baseUrl, innerRef, tenant, theme }) => (\n\t\t\t<descope-wc\n\t\t\t\tproject-id={projectId}\n\t\t\t\tflow-id={flowId}\n\t\t\t\tbase-url={baseUrl}\n\t\t\t\tref={innerRef}\n\t\t\t\ttenant={tenant}\n\t\t\t\ttheme={theme}\n\t\t\t/>\n\t\t)\n\t};\n});\n\nconst Descope = React.forwardRef<HTMLElement, DescopeProps>(\n\t({ flowId, onSuccess, onError, tenant, theme }, ref) => {\n\t\tconst [innerRef, setInnerRef] = useState(null);\n\n\t\tuseImperativeHandle(ref, () => innerRef);\n\n\t\tconst { projectId, baseUrl, setUser, setSessionToken } =\n\t\t\tReact.useContext(AuthContext);\n\n\t\tconst handleSuccess = useCallback(\n\t\t\t(e: CustomEvent) => {\n\t\t\t\tsetUser(e.detail?.user);\n\t\t\t\tconst sessionJwt = e.detail?.sessionJwt;\n\t\t\t\tsetSessionToken(sessionJwt);\n\t\t\t\tif (onSuccess) {\n\t\t\t\t\tonSuccess(e);\n\t\t\t\t}\n\t\t\t},\n\t\t\t[setUser, setSessionToken, onSuccess]\n\t\t);\n\n\t\tuseEffect(() => {\n\t\t\tconst ele = innerRef;\n\t\t\tele?.addEventListener('success', handleSuccess);\n\t\t\tif (onError) ele?.addEventListener('error', onError);\n\n\t\t\treturn () => {\n\t\t\t\tif (onError) ele?.removeEventListener('error', onError);\n\n\t\t\t\tele?.removeEventListener('success', handleSuccess);\n\t\t\t};\n\t\t}, [innerRef, onError, handleSuccess]);\n\n\t\treturn (\n\t\t\t<Suspense>\n\t\t\t\t<DescopeWC\n\t\t\t\t\tprojectId={projectId}\n\t\t\t\t\tflowId={flowId}\n\t\t\t\t\tbaseUrl={baseUrl}\n\t\t\t\t\tinnerRef={setInnerRef}\n\t\t\t\t\ttenant={tenant}\n\t\t\t\t\ttheme={theme}\n\t\t\t\t/>\n\t\t\t</Suspense>\n\t\t);\n\t}\n);\n\nDescope.defaultProps = {\n\tonError: undefined,\n\tonSuccess: undefined\n};\n\nexport default Descope;\n","import React, { useCallback, useMemo } from 'react';\nimport { IAuth } from '../types';\nimport AuthContext from './authContext';\n\n/**\n * Wrap a function with a validation that it exists\n * @param fn The function to wrap with the validation\n * @throws if function does not exist, an error with the relevant message will be thrown\n */\nconst withValidation =\n\t<T extends Array<any>, U>(fn: (...args: T) => U) =>\n\t(...args: T): U => {\n\t\tif (!fn) {\n\t\t\tthrow Error(\n\t\t\t\t`You can only use this function after sdk initialization. Make sure to supply 'projectId' to <AuthProvider /> component`\n\t\t\t);\n\t\t}\n\t\treturn fn(...args);\n\t};\n\nconst useAuth = (): IAuth => {\n\tconst ctx = React.useContext(AuthContext);\n\tif (!ctx) {\n\t\tthrow Error(\n\t\t\t`You can only use 'useAuth' in the context of <AuthProvider />`\n\t\t);\n\t}\n\tconst { user, sessionToken, sdk } = ctx;\n\n\tconst logoutAll = useCallback(withValidation(sdk?.logoutAll), [sdk]);\n\n\tconst logout = useCallback(withValidation(sdk?.logout), [sdk]);\n\n\tconst me = useCallback(withValidation(sdk?.me), [sdk]);\n\n\tconst getJwtPermissions = useCallback(\n\t\twithValidation(sdk?.getJwtPermissions),\n\t\t[sdk]\n\t);\n\n\tconst getJwtRoles = useCallback(withValidation(sdk?.getJwtRoles), [sdk]);\n\n\treturn useMemo(\n\t\t() => ({\n\t\t\tauthenticated: !!sessionToken,\n\t\t\tuser,\n\t\t\tsessionToken,\n\t\t\tlogoutAll,\n\t\t\tlogout,\n\t\t\tme,\n\t\t\tgetJwtPermissions,\n\t\t\tgetJwtRoles\n\t\t}),\n\t\t[user, sessionToken, sdk]\n\t);\n};\n\nexport default useAuth;\n","import React from 'react';\nimport { DefaultFlowProps } from '../types';\nimport Descope from './Descope';\n\nexport const SignInFlow = (props: DefaultFlowProps) => (\n\t<Descope {...props} flowId=\"sign-in\" />\n);\n\nexport const SignUpFlow = (props: DefaultFlowProps) => (\n\t<Descope {...props} flowId=\"sign-up\" />\n);\n\nexport const SignUpOrInFlow = (props: DefaultFlowProps) => (\n\t<Descope {...props} flowId=\"sign-up-or-in\" />\n);\n"],"names":["AuthContext","React","createContext","undefined","AuthProvider","projectId","baseUrl","children","user","setUser","useState","sessionToken","setSessionToken","sdk","useMemo","createSdk","hooks","beforeRequest","config","conf","headers","useEffect","unsubscribeSessionToken","onSessionTokenChange","unsubscribeUser","onUserChange","value","createElement","Provider","defaultProps","DescopeWC","lazy","async","Promise","resolve","then","_interopNamespace","require","default","flowId","innerRef","tenant","theme","ref","Descope","forwardRef","onSuccess","onError","setInnerRef","useImperativeHandle","useContext","handleSuccess","useCallback","e","detail","sessionJwt","ele","addEventListener","removeEventListener","Suspense","withValidation","fn","args","Error","props","ctx","logoutAll","logout","me","getJwtPermissions","getJwtRoles","authenticated"],"mappings":"qfAGA,MAAMA,EAAcC,EAAAA,QAAMC,mBAA4BC,GCahDC,EAAuC,EAC5CC,YACAC,UACAC,eAEA,MAAOC,EAAMC,GAAWC,EAAQA,SAAC,CAAE,IAC5BC,EAAcC,GAAmBF,EAAQA,SAAC,IAE3CG,EAAMC,EAAAA,SAAQ,KACnB,GAAKT,EAGL,OAAOU,UAAU,CAChBV,YACAC,UACAU,MAAO,CACNC,cAAgBC,IACf,MAAMC,EAAOD,EAMb,OALAC,EAAKC,QAAU,IACXD,EAAKC,QACR,qBAAsB,QACtB,wBAAyB,mBAEnBD,CAAI,IAGZ,GACA,CAACd,EAAWC,IAEfe,EAAAA,WAAU,KACT,IAAKR,EACJ,OAGD,MAAMS,EAA0BT,EAAIU,qBAAqBX,GACnDY,EAAkBX,EAAIY,aAAahB,GACzC,MAAO,KACNa,MACAE,KAAmB,CACnB,GACC,CAACX,IAEJ,MAAMa,EAAQZ,EAAAA,SACb,KAAO,CACND,MACAR,YACAC,UACAE,OACAG,eACAF,UACAG,qBAED,CAACD,EAAcH,EAAMH,EAAWC,IAEjC,OAAOL,EAAA,QAAA0B,cAAC3B,EAAY4B,SAAQ,CAACF,MAAOA,GAAQnB,EAAgC,EAG7EH,EAAayB,aAAe,CAC3BvB,QAAS,GACTC,cAAUJ,GC/DX,MAAM2B,EAAYC,EAAIA,MAACC,gBAChBC,QAAAC,UAAAC,MAAA,WAAA,OAAAC,EAAAC,QAAO,0BAAwB,IAC9B,CACNC,QAAS,EAAGjC,YAAWkC,SAA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IAAKoB,EAAOzB,OAAO,kCDOZ,KACf,MAAM0B,EAAMhE,EAAAA,QAAMiD,WAAWlD,GAC7B,IAAKiE,EACJ,MAAMF,MACL,iEAGF,MAAMvD,KAAEA,EAAIG,aAAEA,EAAYE,IAAEA,GAAQoD,EAE9BC,EAAYd,EAAWA,YAACQ,EAAe/C,GAAKqD,WAAY,CAACrD,IAEzDsD,EAASf,EAAWA,YAACQ,EAAe/C,GAAKsD,QAAS,CAACtD,IAEnDuD,EAAKhB,EAAWA,YAACQ,EAAe/C,GAAKuD,IAAK,CAACvD,IAE3CwD,EAAoBjB,EAAWA,YACpCQ,EAAe/C,GAAKwD,mBACpB,CAACxD,IAGIyD,EAAclB,EAAWA,YAACQ,EAAe/C,GAAKyD,aAAc,CAACzD,IAEnE,OAAOC,EAAOA,SACb,KAAO,CACNyD,gBAAiB5D,EACjBH,OACAG,eACAuD,YACAC,SACAC,KACAC,oBACAC,iBAED,CAAC9D,EAAMG,EAAcE,GACrB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import React, { FC, DOMAttributes } from 'react';
|
|
2
3
|
import DescopeWc from '@descope/web-component';
|
|
4
|
+
import createSdk from '@descope/web-js-sdk';
|
|
5
|
+
|
|
6
|
+
interface IAuthProviderProps {
|
|
7
|
+
projectId: string;
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
children?: JSX.Element;
|
|
10
|
+
}
|
|
11
|
+
declare const AuthProvider: FC<IAuthProviderProps>;
|
|
3
12
|
|
|
4
13
|
declare global {
|
|
5
14
|
namespace JSX {
|
|
@@ -8,6 +17,7 @@ declare global {
|
|
|
8
17
|
}
|
|
9
18
|
}
|
|
10
19
|
}
|
|
20
|
+
declare type Sdk = ReturnType<typeof createSdk>;
|
|
11
21
|
declare type CustomEvents<K extends string> = {
|
|
12
22
|
[key in K]: (event: CustomEvent) => void;
|
|
13
23
|
};
|
|
@@ -39,27 +49,32 @@ interface User {
|
|
|
39
49
|
verifiedPhone?: boolean;
|
|
40
50
|
tenants?: string[];
|
|
41
51
|
}
|
|
42
|
-
|
|
43
|
-
declare const useAuth: () => {
|
|
44
|
-
projectId: string;
|
|
45
|
-
baseUrl: string;
|
|
52
|
+
interface IAuth {
|
|
46
53
|
authenticated: boolean;
|
|
47
|
-
user
|
|
48
|
-
sessionToken
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
user?: User;
|
|
55
|
+
sessionToken?: string;
|
|
56
|
+
logoutAll: Sdk['logoutAll'];
|
|
57
|
+
logout: Sdk['logout'];
|
|
58
|
+
me: Sdk['me'];
|
|
59
|
+
getJwtRoles: Sdk['getJwtRoles'];
|
|
60
|
+
getJwtPermissions: Sdk['getJwtPermissions'];
|
|
61
|
+
}
|
|
62
|
+
declare type DescopeTheme = 'light' | 'dark';
|
|
63
|
+
interface DescopeProps {
|
|
52
64
|
flowId: string;
|
|
53
65
|
onSuccess?: DescopeCustomElement['onsuccess'];
|
|
54
66
|
onError?: DescopeCustomElement['onerror'];
|
|
67
|
+
tenant?: string;
|
|
68
|
+
theme?: DescopeTheme;
|
|
55
69
|
}
|
|
56
|
-
declare
|
|
70
|
+
declare type DefaultFlowProps = Omit<DescopeProps, 'flowId'>;
|
|
57
71
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
declare const SignInFlow: (props: DefaultFlowProps) => JSX.Element;
|
|
73
|
+
declare const SignUpFlow: (props: DefaultFlowProps) => JSX.Element;
|
|
74
|
+
declare const SignUpOrInFlow: (props: DefaultFlowProps) => JSX.Element;
|
|
75
|
+
|
|
76
|
+
declare const Descope: React.ForwardRefExoticComponent<DescopeProps & React.RefAttributes<HTMLElement>>;
|
|
77
|
+
|
|
78
|
+
declare const useAuth: () => IAuth;
|
|
64
79
|
|
|
65
|
-
export { AuthProvider, Descope, useAuth };
|
|
80
|
+
export { AuthProvider, Descope, SignInFlow, SignUpFlow, SignUpOrInFlow, useAuth };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import e from"@descope/web-js-sdk";import t,{useState as o,useMemo as r,useEffect as s,lazy as n,useImperativeHandle as c,useCallback as i,Suspense as a}from"react";const l=t.createContext(void 0),d=({projectId:n,baseUrl:c,children:i})=>{const[a,d]=o({}),[u,p]=o(""),f=r((()=>{if(n)return e({projectId:n,baseUrl:c,hooks:{beforeRequest:e=>{const t=e;return t.headers={...t.headers,"x-descope-sdk-name":"react","x-descope-sdk-version":"0.0.52-alpha.21"},t}}})}),[n,c]);s((()=>{if(!f)return;const e=f.onSessionTokenChange(p),t=f.onUserChange(d);return()=>{e?.(),t?.()}}),[f]);const m=r((()=>({sdk:f,projectId:n,baseUrl:c,user:a,sessionToken:u,setUser:d,setSessionToken:p})),[u,a,n,c]);return t.createElement(l.Provider,{value:m},i)};d.defaultProps={baseUrl:"",children:void 0};const u=n((async()=>(await import("@descope/web-component"),{default:({projectId:e,flowId:o,baseUrl:r,innerRef:s,tenant:n,theme:c})=>t.createElement("descope-wc",{"project-id":e,"flow-id":o,"base-url":r,ref:s,tenant:n,theme:c})}))),p=t.forwardRef((({flowId:e,onSuccess:r,onError:n,tenant:d,theme:p},f)=>{const[m,h]=o(null);c(f,(()=>m));const{projectId:w,baseUrl:v,setUser:E,setSessionToken:k}=t.useContext(l),g=i((e=>{E(e.detail?.user);const t=e.detail?.sessionJwt;k(t),r&&r(e)}),[E,k,r]);return s((()=>{const e=m;return e?.addEventListener("success",g),n&&e?.addEventListener("error",n),()=>{n&&e?.removeEventListener("error",n),e?.removeEventListener("success",g)}}),[m,n,g]),t.createElement(a,null,t.createElement(u,{projectId:w,flowId:e,baseUrl:v,innerRef:h,tenant:d,theme:p}))}));p.defaultProps={onError:void 0,onSuccess:void 0};const f=e=>t.createElement(p,{...e,flowId:"sign-in"}),m=e=>t.createElement(p,{...e,flowId:"sign-up"}),h=e=>t.createElement(p,{...e,flowId:"sign-up-or-in"}),w=e=>(...t)=>{if(!e)throw Error("You can only use this function after sdk initialization. Make sure to supply 'projectId' to <AuthProvider /> component");return e(...t)},v=()=>{const e=t.useContext(l);if(!e)throw Error("You can only use 'useAuth' in the context of <AuthProvider />");const{user:o,sessionToken:s,sdk:n}=e,c=i(w(n?.logoutAll),[n]),a=i(w(n?.logout),[n]),d=i(w(n?.me),[n]),u=i(w(n?.getJwtPermissions),[n]),p=i(w(n?.getJwtRoles),[n]);return r((()=>({authenticated:!!s,user:o,sessionToken:s,logoutAll:c,logout:a,me:d,getJwtPermissions:u,getJwtRoles:p})),[o,s,n])};export{d as AuthProvider,p as Descope,f as SignInFlow,m as SignUpFlow,h as SignUpOrInFlow,v as useAuth};
|
|
2
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/lib/hooks/authContext.ts","../src/lib/components/AuthProvider.tsx","../src/lib/components/Descope.tsx","../src/lib/components/DefaultFlows.tsx","../src/lib/hooks/useAuth.ts"],"sourcesContent":["import React from 'react';\nimport { IAuthContext } from '../types';\n\nconst AuthContext = React.createContext<IAuthContext>(undefined);\n\nexport default AuthContext;\n","// '@descope/web-js-sdk' is a dependency of '@descope/web-component'\n// and we want to use the same version that is used there\n// eslint-disable-next-line import/no-extraneous-dependencies\nimport createSdk from '@descope/web-js-sdk';\nimport React, { FC, useEffect, useMemo, useState } from 'react';\nimport AuthContext from '../hooks/authContext';\nimport { IAuthContext } from '../types';\n\ndeclare const BUILD_VERSION: string;\n\ninterface IAuthProviderProps {\n\tprojectId: string;\n\tbaseUrl?: string;\n\tchildren?: JSX.Element;\n}\n\nconst AuthProvider: FC<IAuthProviderProps> = ({\n\tprojectId,\n\tbaseUrl,\n\tchildren\n}) => {\n\tconst [user, setUser] = useState({});\n\tconst [sessionToken, setSessionToken] = useState('');\n\n\tconst sdk = useMemo(() => {\n\t\tif (!projectId) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn createSdk({\n\t\t\tprojectId,\n\t\t\tbaseUrl,\n\t\t\thooks: {\n\t\t\t\tbeforeRequest: (config) => {\n\t\t\t\t\tconst conf = config;\n\t\t\t\t\tconf.headers = {\n\t\t\t\t\t\t...conf.headers,\n\t\t\t\t\t\t'x-descope-sdk-name': 'react',\n\t\t\t\t\t\t'x-descope-sdk-version': BUILD_VERSION\n\t\t\t\t\t};\n\t\t\t\t\treturn conf;\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}, [projectId, baseUrl]);\n\n\tuseEffect(() => {\n\t\tif (!sdk) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst unsubscribeSessionToken = sdk.onSessionTokenChange(setSessionToken);\n\t\tconst unsubscribeUser = sdk.onUserChange(setUser);\n\t\treturn () => {\n\t\t\tunsubscribeSessionToken?.();\n\t\t\tunsubscribeUser?.();\n\t\t};\n\t}, [sdk]);\n\n\tconst value = useMemo<IAuthContext>(\n\t\t() => ({\n\t\t\tsdk,\n\t\t\tprojectId,\n\t\t\tbaseUrl,\n\t\t\tuser,\n\t\t\tsessionToken,\n\t\t\tsetUser,\n\t\t\tsetSessionToken\n\t\t}),\n\t\t[sessionToken, user, projectId, baseUrl]\n\t);\n\treturn <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n};\n\nAuthProvider.defaultProps = {\n\tbaseUrl: '',\n\tchildren: undefined\n};\n\nexport default AuthProvider;\n","import React, {\n\tlazy,\n\tSuspense,\n\tuseCallback,\n\tuseEffect,\n\tuseImperativeHandle,\n\tuseState\n} from 'react';\nimport AuthContext from '../hooks/authContext';\nimport { DescopeProps } from '../types';\n\n// web-component code uses browser API, but can be used in SSR apps, hence the lazy loading\nconst DescopeWC = lazy(async () => {\n\tawait import('@descope/web-component');\n\treturn {\n\t\tdefault: ({ projectId, flowId, baseUrl, innerRef, tenant, theme }) => (\n\t\t\t<descope-wc\n\t\t\t\tproject-id={projectId}\n\t\t\t\tflow-id={flowId}\n\t\t\t\tbase-url={baseUrl}\n\t\t\t\tref={innerRef}\n\t\t\t\ttenant={tenant}\n\t\t\t\ttheme={theme}\n\t\t\t/>\n\t\t)\n\t};\n});\n\nconst Descope = React.forwardRef<HTMLElement, DescopeProps>(\n\t({ flowId, onSuccess, onError, tenant, theme }, ref) => {\n\t\tconst [innerRef, setInnerRef] = useState(null);\n\n\t\tuseImperativeHandle(ref, () => innerRef);\n\n\t\tconst { projectId, baseUrl, setUser, setSessionToken } =\n\t\t\tReact.useContext(AuthContext);\n\n\t\tconst handleSuccess = useCallback(\n\t\t\t(e: CustomEvent) => {\n\t\t\t\tsetUser(e.detail?.user);\n\t\t\t\tconst sessionJwt = e.detail?.sessionJwt;\n\t\t\t\tsetSessionToken(sessionJwt);\n\t\t\t\tif (onSuccess) {\n\t\t\t\t\tonSuccess(e);\n\t\t\t\t}\n\t\t\t},\n\t\t\t[setUser, setSessionToken, onSuccess]\n\t\t);\n\n\t\tuseEffect(() => {\n\t\t\tconst ele = innerRef;\n\t\t\tele?.addEventListener('success', handleSuccess);\n\t\t\tif (onError) ele?.addEventListener('error', onError);\n\n\t\t\treturn () => {\n\t\t\t\tif (onError) ele?.removeEventListener('error', onError);\n\n\t\t\t\tele?.removeEventListener('success', handleSuccess);\n\t\t\t};\n\t\t}, [innerRef, onError, handleSuccess]);\n\n\t\treturn (\n\t\t\t<Suspense>\n\t\t\t\t<DescopeWC\n\t\t\t\t\tprojectId={projectId}\n\t\t\t\t\tflowId={flowId}\n\t\t\t\t\tbaseUrl={baseUrl}\n\t\t\t\t\tinnerRef={setInnerRef}\n\t\t\t\t\ttenant={tenant}\n\t\t\t\t\ttheme={theme}\n\t\t\t\t/>\n\t\t\t</Suspense>\n\t\t);\n\t}\n);\n\nDescope.defaultProps = {\n\tonError: undefined,\n\tonSuccess: undefined\n};\n\nexport default Descope;\n","import React from 'react';\nimport { DefaultFlowProps } from '../types';\nimport Descope from './Descope';\n\nexport const SignInFlow = (props: DefaultFlowProps) => (\n\t<Descope {...props} flowId=\"sign-in\" />\n);\n\nexport const SignUpFlow = (props: DefaultFlowProps) => (\n\t<Descope {...props} flowId=\"sign-up\" />\n);\n\nexport const SignUpOrInFlow = (props: DefaultFlowProps) => (\n\t<Descope {...props} flowId=\"sign-up-or-in\" />\n);\n","import React, { useCallback, useMemo } from 'react';\nimport { IAuth } from '../types';\nimport AuthContext from './authContext';\n\n/**\n * Wrap a function with a validation that it exists\n * @param fn The function to wrap with the validation\n * @throws if function does not exist, an error with the relevant message will be thrown\n */\nconst withValidation =\n\t<T extends Array<any>, U>(fn: (...args: T) => U) =>\n\t(...args: T): U => {\n\t\tif (!fn) {\n\t\t\tthrow Error(\n\t\t\t\t`You can only use this function after sdk initialization. Make sure to supply 'projectId' to <AuthProvider /> component`\n\t\t\t);\n\t\t}\n\t\treturn fn(...args);\n\t};\n\nconst useAuth = (): IAuth => {\n\tconst ctx = React.useContext(AuthContext);\n\tif (!ctx) {\n\t\tthrow Error(\n\t\t\t`You can only use 'useAuth' in the context of <AuthProvider />`\n\t\t);\n\t}\n\tconst { user, sessionToken, sdk } = ctx;\n\n\tconst logoutAll = useCallback(withValidation(sdk?.logoutAll), [sdk]);\n\n\tconst logout = useCallback(withValidation(sdk?.logout), [sdk]);\n\n\tconst me = useCallback(withValidation(sdk?.me), [sdk]);\n\n\tconst getJwtPermissions = useCallback(\n\t\twithValidation(sdk?.getJwtPermissions),\n\t\t[sdk]\n\t);\n\n\tconst getJwtRoles = useCallback(withValidation(sdk?.getJwtRoles), [sdk]);\n\n\treturn useMemo(\n\t\t() => ({\n\t\t\tauthenticated: !!sessionToken,\n\t\t\tuser,\n\t\t\tsessionToken,\n\t\t\tlogoutAll,\n\t\t\tlogout,\n\t\t\tme,\n\t\t\tgetJwtPermissions,\n\t\t\tgetJwtRoles\n\t\t}),\n\t\t[user, sessionToken, sdk]\n\t);\n};\n\nexport default useAuth;\n"],"names":["AuthContext","React","createContext","undefined","AuthProvider","projectId","baseUrl","children","user","setUser","useState","sessionToken","setSessionToken","sdk","useMemo","createSdk","hooks","beforeRequest","config","conf","headers","useEffect","unsubscribeSessionToken","onSessionTokenChange","unsubscribeUser","onUserChange","value","createElement","Provider","defaultProps","DescopeWC","lazy","async","import","default","flowId","innerRef","tenant","theme","ref","Descope","forwardRef","onSuccess","onError","setInnerRef","useImperativeHandle","useContext","handleSuccess","useCallback","e","detail","sessionJwt","ele","addEventListener","removeEventListener","Suspense","SignInFlow","props","SignUpFlow","SignUpOrInFlow","withValidation","fn","args","Error","useAuth","ctx","logoutAll","logout","me","getJwtPermissions","getJwtRoles","authenticated"],"mappings":"qKAGA,MAAMA,EAAcC,EAAMC,mBAA4BC,GCahDC,EAAuC,EAC5CC,YACAC,UACAC,eAEA,MAAOC,EAAMC,GAAWC,EAAS,CAAE,IAC5BC,EAAcC,GAAmBF,EAAS,IAE3CG,EAAMC,GAAQ,KACnB,GAAKT,EAGL,OAAOU,EAAU,CAChBV,YACAC,UACAU,MAAO,CACNC,cAAgBC,IACf,MAAMC,EAAOD,EAMb,OALAC,EAAKC,QAAU,IACXD,EAAKC,QACR,qBAAsB,QACtB,wBAAyB,mBAEnBD,CAAI,IAGZ,GACA,CAACd,EAAWC,IAEfe,GAAU,KACT,IAAKR,EACJ,OAGD,MAAMS,EAA0BT,EAAIU,qBAAqBX,GACnDY,EAAkBX,EAAIY,aAAahB,GACzC,MAAO,KACNa,MACAE,KAAmB,CACnB,GACC,CAACX,IAEJ,MAAMa,EAAQZ,GACb,KAAO,CACND,MACAR,YACAC,UACAE,OACAG,eACAF,UACAG,qBAED,CAACD,EAAcH,EAAMH,EAAWC,IAEjC,OAAOL,EAAA0B,cAAC3B,EAAY4B,SAAQ,CAACF,MAAOA,GAAQnB,EAAgC,EAG7EH,EAAayB,aAAe,CAC3BvB,QAAS,GACTC,cAAUJ,GC/DX,MAAM2B,EAAYC,GAAKC,gBAChBC,OAAO,0BAC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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
2
|
+
"name": "@descope/react-sdk",
|
|
3
|
+
"version": "0.0.52-alpha.21",
|
|
4
|
+
"main": "dist/cjs/index.cjs.js",
|
|
5
|
+
"module": "dist/index.esm.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"require": "./dist/cjs/index.cjs.js",
|
|
9
|
+
"import": "./dist/index.esm.js"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"description": "Descope React SDK",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/descope/react-sdk.git"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@descope/web-component": "0.1.0-alpha.16",
|
|
23
|
+
"react-router-dom": "6.4.3"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@babel/core": "7.20.2",
|
|
27
|
+
"@babel/preset-env": "7.20.2",
|
|
28
|
+
"@babel/preset-react": "7.18.6",
|
|
29
|
+
"@babel/preset-typescript": "7.18.6",
|
|
30
|
+
"@open-wc/rollup-plugin-html": "^1.2.5",
|
|
31
|
+
"@rollup/plugin-commonjs": "^21.1.0",
|
|
32
|
+
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
33
|
+
"@rollup/plugin-replace": "^3.0.0",
|
|
34
|
+
"@rollup/plugin-typescript": "^8.3.0",
|
|
35
|
+
"@testing-library/jest-dom": "5.16.5",
|
|
36
|
+
"@testing-library/react": "13.4.0",
|
|
37
|
+
"@testing-library/user-event": "14.4.3",
|
|
38
|
+
"@types/jest": "^27.0.2",
|
|
39
|
+
"@types/react": "18.0.25",
|
|
40
|
+
"@types/react-dom": "18.0.9",
|
|
41
|
+
"@types/react-router-dom": "^5.3.3",
|
|
42
|
+
"babel-jest": "27.5.1",
|
|
43
|
+
"eslint": "8.26.0",
|
|
44
|
+
"eslint-config-airbnb": "19.0.4",
|
|
45
|
+
"eslint-config-airbnb-typescript": "17.0.0",
|
|
46
|
+
"eslint-config-prettier": "8.5.0",
|
|
47
|
+
"eslint-config-standard": "17.0.0",
|
|
48
|
+
"eslint-import-resolver-typescript": "2.7.1",
|
|
49
|
+
"eslint-plugin-import": "2.26.0",
|
|
50
|
+
"eslint-plugin-jest": "26.9.0",
|
|
51
|
+
"eslint-plugin-jest-dom": "4.0.3",
|
|
52
|
+
"eslint-plugin-jest-formatting": "3.1.0",
|
|
53
|
+
"eslint-plugin-jsx-a11y": "6.6.1",
|
|
54
|
+
"eslint-plugin-n": "15.5.1",
|
|
55
|
+
"eslint-plugin-no-only-tests": "2.6.0",
|
|
56
|
+
"eslint-plugin-prefer-arrow": "1.2.3",
|
|
57
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
58
|
+
"eslint-plugin-promise": "6.1.1",
|
|
59
|
+
"eslint-plugin-react": "7.31.10",
|
|
60
|
+
"eslint-plugin-react-hooks": "4.6.0",
|
|
61
|
+
"eslint-plugin-testing-library": "5.9.1",
|
|
62
|
+
"husky": "^8.0.1",
|
|
63
|
+
"jest": "^27.3.1",
|
|
64
|
+
"lint-staged": "^13.0.3",
|
|
65
|
+
"pretty-quick": "^3.1.3",
|
|
66
|
+
"react": "18.2.0",
|
|
67
|
+
"react-dom": "18.2.0",
|
|
68
|
+
"rollup": "^2.62.0",
|
|
69
|
+
"rollup-plugin-auto-external": "^2.0.0",
|
|
70
|
+
"rollup-plugin-browsersync": "^1.3.3",
|
|
71
|
+
"rollup-plugin-define": "^1.0.1",
|
|
72
|
+
"rollup-plugin-delete": "^2.0.0",
|
|
73
|
+
"rollup-plugin-dotenv": "^0.4.1",
|
|
74
|
+
"rollup-plugin-dts": "^4.2.2",
|
|
75
|
+
"rollup-plugin-livereload": "^2.0.5",
|
|
76
|
+
"rollup-plugin-serve": "^1.1.0",
|
|
77
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
78
|
+
"ts-jest": "^27.0.7",
|
|
79
|
+
"ts-node": "10.9.1",
|
|
80
|
+
"typescript": "^4.5.3"
|
|
81
|
+
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"@types/react": ">=16",
|
|
84
|
+
"react": ">=16"
|
|
85
|
+
},
|
|
86
|
+
"scripts": {
|
|
87
|
+
"start": "npm run build && rollup -c rollup.config.app.js -w",
|
|
88
|
+
"build": "rollup -c",
|
|
89
|
+
"prepare": "husky install",
|
|
90
|
+
"test": "jest",
|
|
91
|
+
"lint": "eslint '+(src|test|testUtils)/**/*.ts' --fix",
|
|
92
|
+
"format": "prettier . -w --ignore-path .gitignore",
|
|
93
|
+
"format-check": "prettier . --check --ignore-path .gitignore",
|
|
94
|
+
"format-lint": "pretty-quick --staged --ignore-path .gitignore && lint-staged",
|
|
95
|
+
"prepublishOnly": "npm run build"
|
|
96
|
+
},
|
|
97
|
+
"lint-staged": {
|
|
98
|
+
"+(src|test|examples)/**/*.{js,ts,jsx,tsx}": [
|
|
99
|
+
"npm run lint"
|
|
100
|
+
]
|
|
101
|
+
}
|
|
80
102
|
}
|
package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import e,{useMemo as t,useRef as r,useImperativeHandle as s,useCallback as o,useEffect as n,useState as c}from"react";import"@descope/web-component";const d=e.createContext(void 0),i=()=>{const r=e.useContext(d);if(!r)throw Error("You can only use useAuth in the context of <AuthProvider />");const{projectId:s,baseUrl:o,authenticated:n,user:c,sessionToken:i}=r;return t((()=>({projectId:s,baseUrl:o,authenticated:n,user:c,sessionToken:i})),[s,o,n,c,i])},u=e.forwardRef((({flowId:t,onSuccess:c,onError:i},u)=>{const a=r();s(u,(()=>a.current));const{projectId:l,baseUrl:p,setAuthenticated:v,setUser:h,setSessionToken:f}=e.useContext(d),E=o((e=>{h(e.detail?.user),v(!0),f(e.detail?.sessionJwt),c&&c(e)}),[h,v,c]);return n((()=>{const e=a.current;return e?.addEventListener("success",E),i&&e?.addEventListener("error",i),()=>{i&&e?.removeEventListener("error",i),e?.removeEventListener("success",E)}}),[a,i,E]),e.createElement("descope-wc",{"project-id":l,"flow-id":t,"base-url":p,ref:a})}));u.defaultProps={onError:void 0,onSuccess:void 0};const a=({projectId:r,baseUrl:s,children:o})=>{const[n,i]=c(!1),[u,a]=c({}),[l,p]=c(""),v=t((()=>({projectId:r,baseUrl:s,user:u,authenticated:n,sessionToken:l,setUser:a,setAuthenticated:i,setSessionToken:p})),[n,u,r,s]);return e.createElement(d.Provider,{value:v},o)};a.defaultProps={baseUrl:"",children:void 0};export{a as AuthProvider,u as Descope,i as useAuth};
|