@descope/vue-sdk 0.0.0-next-fe60cdb6-20230614

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Descope <help@descope.com>
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 ADDED
@@ -0,0 +1,160 @@
1
+ # Descope SDK for Vue
2
+
3
+ The Descope SDK for Vue provides convenient access to the Descope for an application written on top of Vue. You can read more on the [Descope Website](https://descope.com).
4
+
5
+ ## Requirements
6
+
7
+ - The SDK supports Vue version 3 and above.
8
+ - A Descope `Project ID` is required for using the SDK. Find it on the [project page in the Descope Console](https://app.descope.com/settings/project).
9
+
10
+ ## Installing the SDK
11
+
12
+ Install the package with:
13
+
14
+ ```bash
15
+ npm i --save @descope/vue-sdk
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ### Render it in your application
21
+
22
+ #### Add Descope plugin to your application
23
+
24
+ ```js
25
+ import { createApp } from 'vue';
26
+ import App from './App.vue';
27
+ import descope from '@descope/vue-sdk';
28
+
29
+ const app = createApp(App);
30
+ app.use(descope, {
31
+ projectId: 'my-project-id'
32
+ // If the Descope project manages the token response in cookies, a custom domain
33
+ // must be configured (e.g., https://auth.app.example.com)
34
+ // and should be set as the baseUrl property.
35
+ // baseUrl: https://auth.app.example.com'
36
+ });
37
+ app.mount('#app');
38
+ ```
39
+
40
+ #### Use Descope to render specific flow
41
+
42
+ ```js
43
+ <template>
44
+ <Descope
45
+ flowId="sign-up-or-in"
46
+ @error="handleError"
47
+ @success="handleSuccess"
48
+ />
49
+ </template>
50
+
51
+ <script setup>
52
+ import { Descope } from '@descope/vue-sdk';
53
+
54
+ const handleError = (e) => {
55
+ console.log('Got error', e);
56
+ };
57
+
58
+ const handleSuccess = (e) => {
59
+ console.log('Logged in', e);
60
+ };
61
+ </script>
62
+ ```
63
+
64
+ #### Use the `useDescope`, `useSession` and `useUser` functions in your components in order to get authentication state, user details and utilities
65
+
66
+ This can be helpful to implement application-specific logic. Examples:
67
+
68
+ - Render different components if current session is authenticated
69
+ - Render user's content
70
+ - Logout button
71
+
72
+ ```js
73
+ <template>
74
+ <div>
75
+ <div v-if="isSessionLoading || isUserLoading">Loading ...</div>
76
+ <div v-else-if="isAuthenticated">
77
+ <div>Hello {{ user?.name }}</div>
78
+ <button @click="logout">Logout</button>
79
+ </div>
80
+ <div v-else>You are not logged in</div>
81
+ </div>
82
+ </template>
83
+
84
+ <script setup>
85
+ import { useDescope, useSession, useUser } from '../../src';
86
+
87
+ const { isAuthenticated, isSessionLoading } = useSession();
88
+ const { user, isUserLoading } = useUser();
89
+ const { logout } = useDescope();
90
+ </script>
91
+ ```
92
+
93
+ Note: `useSession` triggers a single request to the Descope backend to attempt to refresh the session. If you **don't** `useSession` on your app, the session will not be refreshed automatically. If your app does not require `useSession`, you can trigger the refresh manually by calling `refresh` from `useDescope` hook.
94
+
95
+ **For more SDK usage examples refer to [docs](https://docs.descope.com/build/guides/client_sdks/)**
96
+
97
+ #### Refresh token lifecycle
98
+
99
+ Descope SDK is automatically refreshes the session token when it is about to expire. This is done in the background using the refresh token, without any additional configuration.
100
+
101
+ If the Descope project settings are configured to manage tokens in cookies.
102
+ you must also configure a custom domain, and set it as the `baseUrl` to the `descope` plugin. See the above [`plugin` usage](#add-descope-plugin-to-your-application) for usage example.
103
+
104
+ ## Code Example
105
+
106
+ You can find an example Vue app in the [examples folder](./example).
107
+
108
+ ### Setup
109
+
110
+ To run the examples, set your `Project ID` by setting the `DESCOPE_PROJECT_ID` env var or directly
111
+ in the sample code.
112
+ Find your Project ID in the [Descope console](https://app.descope.com/settings/project).
113
+
114
+ ```bash
115
+ export VUE_APP_DESCOPE_PROJECT_ID=<Project-ID>
116
+ ```
117
+
118
+ Alternatively, put the environment variable in `.env.local` file in the project root directory.
119
+ See bellow for an `.env.local` file template with more information.
120
+
121
+ ### Run Example
122
+
123
+ Run the following command in the root of the project to build and run the example:
124
+
125
+ ```bash
126
+ npm i && npm start
127
+ ```
128
+
129
+ ### Example Optional Env Variables
130
+
131
+ See the following table for customization environment variables for the example app:
132
+
133
+ | Env Variable | Description | Default value |
134
+ | ------------ | ----------- | ------------- |
135
+
136
+ | VUE_APP_DESCOPE_FLOW_ID | Which flow ID to use in the login page | **sign-up-or-in** |
137
+ | VUE_APP_DESCOPE_BASE_URL | Custom Descope base URL | None |
138
+
139
+ Example for `.env.local` file template:
140
+
141
+ ```
142
+ # Your project ID
143
+ VUE_APP_DESCOPE_PROJECT_ID="<Project-ID>"
144
+ # Login flow ID
145
+ VUE_APP_DESCOPE_FLOW_ID=""
146
+ # Descope base URL
147
+ VUE_APP_DESCOPE_BASE_URL=""
148
+ ```
149
+
150
+ ## Learn More
151
+
152
+ To learn more please see the [Descope Documentation and API reference page](https://docs.descope.com/).
153
+
154
+ ## Contact Us
155
+
156
+ If you need help you can email [Descope Support](mailto:support@descope.com)
157
+
158
+ ## License
159
+
160
+ The Descope SDK for React is licensed for use under the terms and conditions of the [MIT license Agreement](./LICENSE).
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@descope/web-component"),s=require("vue"),o=require("@descope/web-js-sdk");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=r(e),n=r(o);const u=Symbol("$descope"),a={"x-descope-sdk-name":"vue","x-descope-sdk-version":"0.0.0-next-fe60cdb6-20230614"},i=()=>{const e=s.inject(u);if(!e)throw Error("Missing Descope context, make sure you are using the Descope plugin");return e},l=()=>i().sdk;t.default.sdkConfigOverrides={baseHeaders:a};var c={name:"Descope",props:{flowId:{type:String,required:!0},tenant:{type:String},theme:{type:String},debug:{type:Boolean},telemetryKey:{type:String},redirectUrl:{type:String},autoFocus:{type:Boolean}},emits:["success","error"],setup(e,{emit:s}){const{projectId:o,baseUrl:r,sessionTokenViaCookie:t}=i().options,n=l();return{projectId:o,baseUrl:r,sessionTokenViaCookie:t,onSuccess:async e=>{s("success",e),await(n.httpClient.hooks?.afterRequest?.({},new Response(JSON.stringify(e.detail))))},onError:e=>s("error",e)}}};const d=["project-id","base-url","flow-id","^theme","^tenant","^debug","^telemetryKey","redirect-url","auto-focus"];c.render=function(e,o,r,t,n,u){return s.openBlock(),s.createElementBlock("div",null,[s.createElementVNode("descope-wc",{"project-id":t.projectId,"base-url":t.baseUrl,"flow-id":r.flowId,"^theme":r.theme,"^tenant":r.tenant,"^debug":r.debug,"^telemetryKey":r.telemetryKey,"redirect-url":r.redirectUrl,"auto-focus":r.autoFocus,onSuccess:o[0]||(o[0]=(...e)=>t.onSuccess&&t.onSuccess(...e)),onError:o[1]||(o[1]=(...e)=>t.onError&&t.onError(...e))},null,40,d)])},c.__file="src/Descope.vue";const p=s.ref(null);var v={install:function(e,o){const r=n.default({persistTokens:!0,autoRefresh:!0,baseHeaders:a,...o}),t=s.ref(null),i=s.ref(""),l=s.ref(null),c=s.ref(null);r.onSessionTokenChange((e=>{i.value=e})),r.onUserChange((e=>{c.value=e}));const d=async()=>{t.value=!0,await r.refresh(),t.value=!1},v=s.computed((()=>null===t.value)),f=s.computed((()=>null===l.value));p.value=async()=>(!i.value&&v.value&&await d(),!!s.unref(i)),e.provide(u,{session:{fetchSession:d,isLoading:s.readonly(t),session:s.readonly(i),isFetchSessionWasNeverCalled:v},user:{fetchUser:async()=>{l.value=!0,await r.me(),l.value=!1},isLoading:s.readonly(l),user:s.readonly(c),isFetchUserWasNeverCalled:f},sdk:r,options:o})}};exports.Descope=c,exports.default=v,exports.routeGuard=()=>s.unref(p)?.(),exports.useDescope=l,exports.useSession=()=>{const{session:e}=i();return e.isFetchSessionWasNeverCalled.value&&e.fetchSession(),{isLoading:s.computed((()=>e.isLoading.value||e.isFetchSessionWasNeverCalled.value)),sessionToken:e.session,isAuthenticated:s.computed((()=>!!e.session.value))}},exports.useUser=()=>{const{user:e,session:o}=i(),r=()=>{!e.user.value&&o.session.value&&e.fetchUser()};return r(),s.watch(o.session,r),{isLoading:s.computed((()=>e.isLoading.value||e.isFetchUserWasNeverCalled.value)),user:e.user}};
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/Descope.vue?vue&type=template&id=8a031dec&lang.js"],"sourcesContent":["<template>\n\t<div>\n\t\t<descope-wc\n\t\t\t:project-id=\"projectId\"\n\t\t\t:base-url=\"baseUrl\"\n\t\t\t:flow-id=\"flowId\"\n\t\t\t:theme.attr=\"theme\"\n\t\t\t:tenant.attr=\"tenant\"\n\t\t\t:debug.attr=\"debug\"\n\t\t\t:telemetryKey.attr=\"telemetryKey\"\n\t\t\t:redirect-url=\"redirectUrl\"\n\t\t\t:auto-focus=\"autoFocus\"\n\t\t\t@success=\"onSuccess\"\n\t\t\t@error=\"onError\"\n\t\t/>\n\t</div>\n</template>\n\n<script lang=\"ts\">\nimport DescopeWc from '@descope/web-component';\nimport { useOptions, useDescope } from './hooks';\nimport { baseHeaders } from './constants';\nimport { RequestConfig } from '@descope/core-js-sdk';\nimport { SetupContext } from 'vue';\n\nDescopeWc.sdkConfigOverrides = { baseHeaders };\n\nexport default {\n\t// eslint-disable-next-line vue/multi-word-component-names\n\tname: 'Descope',\n\tprops: {\n\t\tflowId: {\n\t\t\ttype: String,\n\t\t\trequired: true\n\t\t},\n\t\ttenant: {\n\t\t\ttype: String\n\t\t},\n\t\ttheme: {\n\t\t\ttype: String\n\t\t},\n\t\tdebug: {\n\t\t\ttype: Boolean\n\t\t},\n\t\ttelemetryKey: {\n\t\t\ttype: String\n\t\t},\n\t\tredirectUrl: {\n\t\t\ttype: String\n\t\t},\n\t\tautoFocus: {\n\t\t\ttype: Boolean\n\t\t}\n\t},\n\temits: ['success', 'error'],\n\tsetup(_: unknown, { emit }: SetupContext) {\n\t\tconst { projectId, baseUrl, sessionTokenViaCookie } = useOptions();\n\t\tconst sdk = useDescope();\n\n\t\tconst onSuccess = async (e: CustomEvent) => {\n\t\t\t// Note: We need to emit AFTER the afterRequest hook has been called, but for\n\t\t\t// an unknown reason, the emit is not called if we await the hook.\n\t\t\temit('success', e);\n\t\t\tawait sdk.httpClient.hooks?.afterRequest?.(\n\t\t\t\t{} as RequestConfig,\n\t\t\t\tnew Response(JSON.stringify(e.detail))\n\t\t\t);\n\t\t};\n\t\tconst onError = (e: Event) => emit('error', e);\n\n\t\treturn {\n\t\t\tprojectId,\n\t\t\tbaseUrl,\n\t\t\tsessionTokenViaCookie,\n\t\t\tonSuccess,\n\t\t\tonError\n\t\t};\n\t}\n};\n</script>\n"],"names":["_createElementBlock","_createElementVNode","$setup","projectId","baseUrl","$props","flowId","theme","tenant","debug","telemetryKey","redirectUrl","autoFocus","onSuccess","args","onError"],"mappings":"itCACCA,qBAcK,MAAA,KAAA,CAbJC,EAAAA,mBAYC,aAAA,CAXC,aAAYC,EAASC,UACrB,WAAUD,EAAOE,QACjB,UAASC,EAAMC,OACf,SAAYD,EAAKE,MACjB,UAAaF,EAAMG,OACnB,SAAYH,EAAKI,MACjB,gBAAmBJ,EAAYK,aAC/B,eAAcL,EAAWM,YACzB,aAAYN,EAASO,UACrBC,8BAASX,EAASW,WAAAX,EAAAW,aAAAC,IAClBC,4BAAOb,EAAOa,SAAAb,EAAAa,WAAAD"}