@logto/vue 1.0.0-beta.0 → 1.0.0-beta.10
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 +7 -72
- package/lib/index.d.ts +6 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +26 -9
- package/lib/index.js.map +1 -1
- package/lib/module.js +26 -11
- package/lib/module.js.map +1 -1
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
[](https://github.com/logto-io/js/actions/workflows/main.yml)
|
|
4
4
|
[](https://app.codecov.io/gh/logto-io/js?branch=master)
|
|
5
5
|
|
|
6
|
-
The Logto Vue SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/integrate-
|
|
6
|
+
The Logto Vue SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/docs/recipes/integrate-logto/vue) or [docs](https://docs.logto.io/sdk/JavaScript/vue/) for more information.
|
|
7
7
|
|
|
8
|
-
We also provide [集成指南](https://docs.logto.io/zh-cn/integrate-
|
|
8
|
+
We also provide [集成指南](https://docs.logto.io/zh-cn/docs/recipes/integrate-logto/vue/) and [文档](https://docs.logto.io/zh-cn/sdk/JavaScript/vue/) in Simplified Chinese.
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
@@ -24,86 +24,21 @@ yarn add @logto/vue
|
|
|
24
24
|
### Using pnpm
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
|
-
pnpm
|
|
27
|
+
pnpm add @logto/vue
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
## Get sample
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
<script src="https://logto.io/js/logto-sdk-vue/0.1.0/logto-sdk-vue.production.js" />
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## Get Started
|
|
37
|
-
|
|
38
|
-
A sample project with the following code snippets can be found at [Vue Sample](https://github.com/logto-io/js/tree/master/packages/vue-sample)
|
|
39
|
-
|
|
40
|
-
Check out the source code and try it yourself. (We use [pnpm](https://pnpm.io/) for package management)
|
|
32
|
+
A sample Vue project with the integration of @logto/vue SDK can be found at [Vue Sample](https://github.com/logto-io/js/tree/master/packages/vue-sample). Check out the source code and try it with ease.
|
|
41
33
|
|
|
42
34
|
```bash
|
|
43
35
|
pnpm i && pnpm start
|
|
44
36
|
```
|
|
45
37
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
```ts
|
|
49
|
-
import { createLogto, LogtoConfig } from '@logto/vue';
|
|
50
|
-
|
|
51
|
-
const config: LogtoConfig = {
|
|
52
|
-
appId: '<your-application-id>',
|
|
53
|
-
endpoint: '<your-logto-endpoint>'
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const app = createApp(App);
|
|
57
|
-
|
|
58
|
-
app.use(createLogto, config);
|
|
59
|
-
app.mount("#app");
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### Setup your sign-in
|
|
63
|
-
|
|
64
|
-
```ts
|
|
65
|
-
import { useLogto } from "@logto/vue";
|
|
66
|
-
|
|
67
|
-
const { signIn } = useLogto();
|
|
68
|
-
const onClickSignIn = () => signIn(redirectUrl);
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
```html
|
|
72
|
-
<button @click="onClickSignIn">Sign In</button>
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Retrieve Auth Status
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
import { useLogto } from '@logto/vue';
|
|
79
|
-
|
|
80
|
-
const { isAuthenticated } = useLogto();
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
```html
|
|
84
|
-
<div v-if="!isAuthenticated">
|
|
85
|
-
<!-- E.g. navigate to the sign in page -->
|
|
86
|
-
</div>
|
|
87
|
-
<div v-else>
|
|
88
|
-
<!-- Do things when user is authenticated -->
|
|
89
|
-
</div>
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Sign out
|
|
93
|
-
|
|
94
|
-
```ts
|
|
95
|
-
import { useLogto } from "@logto/vue";
|
|
96
|
-
|
|
97
|
-
const { signOut } = useLogto();
|
|
98
|
-
const onClickSignOut = () => signOut('http://localhost:1234');
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
```html
|
|
102
|
-
<button @click="onClickSignOut">Sign Out</button>
|
|
103
|
-
```
|
|
38
|
+
Note: Currently only supports Vue 3.x and its [composition API](https://vuejs.org/api/composition-api-setup.html#composition-api-setup). Please let us know if you would like Logto to support Vue 2.x or options API by filing a [feature request](https://github.com/logto-io/logto/issues/new?assignees=&labels=feature-request&template=feature_request.md&title=feature+request%3A+).
|
|
104
39
|
|
|
105
40
|
## Resources
|
|
106
41
|
|
|
107
42
|
[](https://logto.io/)
|
|
108
|
-
[](https://docs.logto.io/
|
|
43
|
+
[](https://docs.logto.io/sdk/JavaScript/vue/)
|
|
109
44
|
[](https://discord.gg/UEPaF3j5e6)
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { IdTokenClaims, LogtoConfig } from "@logto/browser";
|
|
1
|
+
import { IdTokenClaims, LogtoConfig, UserInfoResponse } from "@logto/browser";
|
|
2
2
|
import { App, Ref } from "vue";
|
|
3
|
-
export type { LogtoConfig, IdTokenClaims, LogtoErrorCode, LogtoClientErrorCode, } from '@logto/browser';
|
|
4
|
-
export { LogtoError, LogtoClientError, OidcError, Prompt } from '@logto/browser';
|
|
3
|
+
export type { LogtoConfig, IdTokenClaims, UserInfoResponse, LogtoErrorCode, LogtoClientErrorCode, } from '@logto/browser';
|
|
4
|
+
export { LogtoError, LogtoClientError, OidcError, Prompt, ReservedScope, UserScope, } from '@logto/browser';
|
|
5
5
|
type LogtoVuePlugin = {
|
|
6
6
|
install: (app: App, config: LogtoConfig) => void;
|
|
7
7
|
};
|
|
@@ -9,8 +9,9 @@ type Logto = {
|
|
|
9
9
|
isAuthenticated: Readonly<Ref<boolean>>;
|
|
10
10
|
isLoading: Readonly<Ref<boolean>>;
|
|
11
11
|
error: Readonly<Ref<Error | undefined>>;
|
|
12
|
+
fetchUserInfo: () => Promise<UserInfoResponse | undefined>;
|
|
12
13
|
getAccessToken: (resource?: string) => Promise<string | undefined>;
|
|
13
|
-
getIdTokenClaims: () => IdTokenClaims | undefined
|
|
14
|
+
getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;
|
|
14
15
|
signIn: (redirectUri: string) => Promise<void>;
|
|
15
16
|
signOut: (postLogoutRedirectUri?: string) => Promise<void>;
|
|
16
17
|
};
|
|
@@ -72,7 +73,7 @@ export const useLogto: () => Logto;
|
|
|
72
73
|
*
|
|
73
74
|
* Use this in the setup script of your Callback page to make sure the injection works
|
|
74
75
|
*/
|
|
75
|
-
export const useHandleSignInCallback: (callback?: (
|
|
76
|
+
export const useHandleSignInCallback: (callback?: () => void) => {
|
|
76
77
|
isLoading: Readonly<Ref<boolean>>;
|
|
77
78
|
isAuthenticated: Readonly<Ref<boolean>>;
|
|
78
79
|
error: Readonly<Ref<Error | undefined>>;
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AGOA,YAAY,EACV,WAAW,EACX,aAAa,EACb,cAAc,EACd,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,
|
|
1
|
+
{"mappings":";;AGOA,YAAY,EACV,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,SAAS,EACT,MAAM,EACN,aAAa,EACb,SAAS,GACV,MAAM,gBAAgB,CAAC;AAExB,sBAAsB;IACpB,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,KAAK,IAAI,CAAC;CAClD,CAAC;AAEF,aAAa;IACX,eAAe,EAAE,QAAQ,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC;IACxC,SAAS,EAAE,QAAQ,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC;IAClC,KAAK,EAAE,QAAQ,CAAC,IAAI,KAAK,GAAG,SAAS,CAAC,CAAC,CAAC;IACxC,aAAa,EAAE,MAAM,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC;IAC3D,cAAc,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACnE,gBAAgB,EAAE,MAAM,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CAAC;IAC3D,MAAM,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,OAAO,EAAE,CAAC,qBAAqB,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5D,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,MAAM,aAAa,cAezB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,MAAM,gBAAe,KAQ3B,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,OAAO,MAAM,qCAAsC,MAAM,IAAI;;;;CAuB5D,CAAC","sources":["packages/vue/src/src/consts.ts","packages/vue/src/src/context.ts","packages/vue/src/src/plugin.ts","packages/vue/src/src/index.ts","packages/vue/src/index.ts"],"sourcesContent":[null,null,null,null,"import LogtoClient, { IdTokenClaims, LogtoConfig, UserInfoResponse } from '@logto/browser';\nimport { App, inject, readonly, Ref, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport { Context, createContext, throwContextError } from './context';\nimport { createPluginMethods } from './plugin';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n UserInfoResponse,\n LogtoErrorCode,\n LogtoClientErrorCode,\n} from '@logto/browser';\n\nexport {\n LogtoError,\n LogtoClientError,\n OidcError,\n Prompt,\n ReservedScope,\n UserScope,\n} from '@logto/browser';\n\ntype LogtoVuePlugin = {\n install: (app: App, config: LogtoConfig) => void;\n};\n\ntype Logto = {\n isAuthenticated: Readonly<Ref<boolean>>;\n isLoading: Readonly<Ref<boolean>>;\n error: Readonly<Ref<Error | undefined>>;\n fetchUserInfo: () => Promise<UserInfoResponse | undefined>;\n getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;\n signIn: (redirectUri: string) => Promise<void>;\n signOut: (postLogoutRedirectUri?: string) => Promise<void>;\n};\n\n/**\n * Creates the Logto Vue plugin\n *\n * ```ts\n * import { createApp } from 'vue';\n * import { createLogto } from '@logto/vue';\n *\n * const app = createApp(App);\n * const app.use(createLogto, {\n * appId: '<your-app-id>',\n * endpoint: '<your-oidc-endpoint-domain>',\n * });\n *\n * app.mount('#app');\n * ```\n *\n * Use this in your Vue root component to register the plugin\n */\nexport const createLogto: LogtoVuePlugin = {\n install(app: App, config: LogtoConfig) {\n const client = new LogtoClient(config);\n const context = createContext(client);\n const pluginMethods = createPluginMethods(context);\n const { isAuthenticated, isLoading, error } = context;\n\n app.provide<Context>(contextInjectionKey, context);\n app.provide<Logto>(logtoInjectionKey, {\n isAuthenticated: readonly(isAuthenticated),\n isLoading: readonly(isLoading),\n error: readonly(error),\n ...pluginMethods,\n });\n },\n};\n\n/**\n * A Vue composable method that provides the Logto reactive refs and auth methods.\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n *\n * export default {\n * setup() {\n * const { isAuthenticated, signIn } = useLogto();\n *\n * return {\n * isAuthenticated,\n * onClickSignIn: () => {\n * signIn('<your-redirect-uri>');\n * },\n * }\n * }\n * }\n * ```\n *\n * Use this composable in the setup script of your Vue component to make sure the injection works\n */\nexport const useLogto = (): Logto => {\n const logto = inject<Logto>(logtoInjectionKey);\n\n if (!logto) {\n return throwContextError();\n }\n\n return logto;\n};\n\n/**\n * A Vue composable method that watches browser navigation and automatically handles the sign-in callback\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n * import { useHandleSignInCallback } from '@logto/vue';\n *\n * export default {\n * setup() {\n * useHandleSignInCallback();\n * }\n * }\n * ```\n *\n * Use this in the setup script of your Callback page to make sure the injection works\n */\nexport const useHandleSignInCallback = (callback?: () => void) => {\n const context = inject<Context>(contextInjectionKey);\n\n if (!context) {\n return throwContextError();\n }\n\n const { isAuthenticated, isLoading, logtoClient, error } = context;\n const { handleSignInCallback } = createPluginMethods(context);\n\n watchEffect(() => {\n const currentPageUrl = window.location.href;\n\n if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {\n void handleSignInCallback(currentPageUrl, callback);\n }\n });\n\n return {\n isLoading: readonly(isLoading),\n isAuthenticated: readonly(isAuthenticated),\n error: readonly(error),\n };\n};\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,8 @@ $parcel$export(module.exports, "LogtoError", () => $b8cbe35fca4d52f8$re_export$L
|
|
|
15
15
|
$parcel$export(module.exports, "LogtoClientError", () => $b8cbe35fca4d52f8$re_export$LogtoClientError);
|
|
16
16
|
$parcel$export(module.exports, "OidcError", () => $b8cbe35fca4d52f8$re_export$OidcError);
|
|
17
17
|
$parcel$export(module.exports, "Prompt", () => $b8cbe35fca4d52f8$re_export$Prompt);
|
|
18
|
+
$parcel$export(module.exports, "ReservedScope", () => $b8cbe35fca4d52f8$re_export$ReservedScope);
|
|
19
|
+
$parcel$export(module.exports, "UserScope", () => $b8cbe35fca4d52f8$re_export$UserScope);
|
|
18
20
|
|
|
19
21
|
|
|
20
22
|
const $bf8f9fe4f6be54e2$export$58f3af87e7a1a85a = "@logto/vue";
|
|
@@ -25,12 +27,12 @@ const $bf8f9fe4f6be54e2$export$951b969b1220de04 = "@logto/vue:context";
|
|
|
25
27
|
const $a89e3697ca8ff325$export$fd42f52fd3ae1109 = (client)=>{
|
|
26
28
|
const context = (0, $Kru5Y$vue.toRefs)((0, $Kru5Y$vue.reactive)({
|
|
27
29
|
logtoClient: client,
|
|
28
|
-
isAuthenticated:
|
|
29
|
-
loadingCount:
|
|
30
|
+
isAuthenticated: false,
|
|
31
|
+
loadingCount: 1,
|
|
30
32
|
error: undefined
|
|
31
33
|
}));
|
|
32
34
|
const { isAuthenticated: isAuthenticated , loadingCount: loadingCount , error: error } = context;
|
|
33
|
-
const
|
|
35
|
+
const isLoading = (0, $Kru5Y$vue.computed)(()=>loadingCount.value > 0);
|
|
34
36
|
/* eslint-disable @silverhand/fp/no-mutation */ const setError = (_error, fallbackErrorMessage)=>{
|
|
35
37
|
if (_error instanceof Error) error.value = _error;
|
|
36
38
|
else if (fallbackErrorMessage) error.value = new Error(fallbackErrorMessage);
|
|
@@ -43,9 +45,14 @@ const $a89e3697ca8ff325$export$fd42f52fd3ae1109 = (client)=>{
|
|
|
43
45
|
const setIsAuthenticated = (_isAuthenticated)=>{
|
|
44
46
|
isAuthenticated.value = _isAuthenticated;
|
|
45
47
|
};
|
|
46
|
-
/* eslint-enable @silverhand/fp/no-mutation */
|
|
48
|
+
/* eslint-enable @silverhand/fp/no-mutation */ (0, $Kru5Y$vue.watchEffect)(async ()=>{
|
|
49
|
+
const isAuthenticated = await client.isAuthenticated();
|
|
50
|
+
setIsAuthenticated(isAuthenticated);
|
|
51
|
+
setLoading(false);
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
47
54
|
...context,
|
|
48
|
-
isLoading:
|
|
55
|
+
isLoading: isLoading,
|
|
49
56
|
setError: setError,
|
|
50
57
|
setLoading: setLoading,
|
|
51
58
|
setIsAuthenticated: setIsAuthenticated
|
|
@@ -66,8 +73,6 @@ const $155c9da87af3d4e0$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
66
73
|
await logtoClient.value.signIn(redirectUri);
|
|
67
74
|
} catch (error) {
|
|
68
75
|
setError(error, "Unexpected error occurred while signing in.");
|
|
69
|
-
} finally{
|
|
70
|
-
setLoading(false);
|
|
71
76
|
}
|
|
72
77
|
};
|
|
73
78
|
const signOut = async (postLogoutRedirectUri)=>{
|
|
@@ -84,6 +89,17 @@ const $155c9da87af3d4e0$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
84
89
|
setLoading(false);
|
|
85
90
|
}
|
|
86
91
|
};
|
|
92
|
+
const fetchUserInfo = async ()=>{
|
|
93
|
+
if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
|
|
94
|
+
try {
|
|
95
|
+
setLoading(true);
|
|
96
|
+
return await logtoClient.value.fetchUserInfo();
|
|
97
|
+
} catch (error) {
|
|
98
|
+
setError(error, "Unexpected error occurred while fetching user info.");
|
|
99
|
+
} finally{
|
|
100
|
+
setLoading(false);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
87
103
|
const getAccessToken = async (resource)=>{
|
|
88
104
|
if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
|
|
89
105
|
try {
|
|
@@ -95,10 +111,10 @@ const $155c9da87af3d4e0$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
95
111
|
setLoading(false);
|
|
96
112
|
}
|
|
97
113
|
};
|
|
98
|
-
const getIdTokenClaims = ()=>{
|
|
114
|
+
const getIdTokenClaims = async ()=>{
|
|
99
115
|
if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
|
|
100
116
|
try {
|
|
101
|
-
return logtoClient.value.getIdTokenClaims();
|
|
117
|
+
return await logtoClient.value.getIdTokenClaims();
|
|
102
118
|
} catch (error) {
|
|
103
119
|
setError(error, "Unexpected error occurred while getting id token claims.");
|
|
104
120
|
}
|
|
@@ -119,6 +135,7 @@ const $155c9da87af3d4e0$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
119
135
|
return {
|
|
120
136
|
signIn: signIn,
|
|
121
137
|
signOut: signOut,
|
|
138
|
+
fetchUserInfo: fetchUserInfo,
|
|
122
139
|
getAccessToken: getAccessToken,
|
|
123
140
|
getIdTokenClaims: getIdTokenClaims,
|
|
124
141
|
handleSignInCallback: handleSignInC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AAC,AAAC;IAErD,IAAI,CAAC,OAAO,EACV,OAAO,CAAA,GAAA,yCAAiB,CAAA,EAAE,CAAC;IAG7B,MAAM,mBAAE,eAAe,CAAA,aAAE,SAAS,CAAA,eAAE,WAAW,CAAA,SAAE,KAAK,CAAA,EAAE,GAAG,OAAO,AAAC;IACnE,MAAM,wBAAE,oBAAoB,CAAA,EAAE,GAAG,CAAA,GAAA,uCAAmB,CAAA,CAAC,OAAO,CAAC,AAAC;IAE9D,CAAA,GAAA,sBAAW,CAAA,CAAC,IAAM;QAChB,MAAM,cAAc,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,AAAC;QAE5C,IAAI,CAAC,eAAe,CAAC,KAAK,IAAI,WAAW,CAAC,KAAK,EAAE,kBAAkB,CAAC,cAAc,CAAC,EAC5E,oBAAoB,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;KAEvD,CAAC,CAAC;IAEH,OAAO;QACL,SAAS,EAAE,CAAA,GAAA,mBAAQ,CAAA,CAAC,SAAS,CAAC;QAC9B,eAAe,EAAE,CAAA,GAAA,mBAAQ,CAAA,CAAC,eAAe,CAAC;QAC1C,KAAK,EAAE,CAAA,GAAA,mBAAQ,CAAA,CAAC,KAAK,CAAC;KACvB,CAAC;CACH,AAAC","sources":["packages/vue/src/index.ts","packages/vue/src/consts.ts","packages/vue/src/context.ts","packages/vue/src/plugin.ts"],"sourcesContent":["import LogtoClient, { IdTokenClaims, LogtoConfig } from '@logto/browser';\nimport { App, inject, readonly, Ref, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport { Context, createContext, throwContextError } from './context';\nimport { createPluginMethods } from './plugin';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n LogtoErrorCode,\n LogtoClientErrorCode,\n} from '@logto/browser';\n\nexport { LogtoError, LogtoClientError, OidcError, Prompt } from '@logto/browser';\n\ntype LogtoVuePlugin = {\n install: (app: App, config: LogtoConfig) => void;\n};\n\ntype Logto = {\n isAuthenticated: Readonly<Ref<boolean>>;\n isLoading: Readonly<Ref<boolean>>;\n error: Readonly<Ref<Error | undefined>>;\n getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => IdTokenClaims | undefined;\n signIn: (redirectUri: string) => Promise<void>;\n signOut: (postLogoutRedirectUri?: string) => Promise<void>;\n};\n\n/**\n * Creates the Logto Vue plugin\n *\n * ```ts\n * import { createApp } from 'vue';\n * import { createLogto } from '@logto/vue';\n *\n * const app = createApp(App);\n * const app.use(createLogto, {\n * appId: '<your-app-id>',\n * endpoint: '<your-oidc-endpoint-domain>',\n * });\n *\n * app.mount('#app');\n * ```\n *\n * Use this in your Vue root component to register the plugin\n */\nexport const createLogto: LogtoVuePlugin = {\n install(app: App, config: LogtoConfig) {\n const client = new LogtoClient(config);\n const context = createContext(client);\n const pluginMethods = createPluginMethods(context);\n const { isAuthenticated, isLoading, error } = context;\n\n app.provide<Context>(contextInjectionKey, context);\n app.provide<Logto>(logtoInjectionKey, {\n isAuthenticated: readonly(isAuthenticated),\n isLoading: readonly(isLoading),\n error: readonly(error),\n ...pluginMethods,\n });\n },\n};\n\n/**\n * A Vue composable method that provides the Logto reactive refs and auth methods.\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n *\n * export default {\n * setup() {\n * const { isAuthenticated, signIn } = useLogto();\n *\n * return {\n * isAuthenticated,\n * onClickSignIn: () => {\n * signIn('<your-redirect-uri>');\n * },\n * }\n * }\n * }\n * ```\n *\n * Use this composable in the setup script of your Vue component to make sure the injection works\n */\nexport const useLogto = (): Logto => {\n const logto = inject<Logto>(logtoInjectionKey);\n\n if (!logto) {\n return throwContextError();\n }\n\n return logto;\n};\n\n/**\n * A Vue composable method that watches browser navigation and automatically handles the sign-in callback\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n * import { useHandleSignInCallback } from '@logto/vue';\n *\n * export default {\n * setup() {\n * useHandleSignInCallback();\n * }\n * }\n * ```\n *\n * Use this in the setup script of your Callback page to make sure the injection works\n */\nexport const useHandleSignInCallback = (callback?: () => void) => {\n const context = inject<Context>(contextInjectionKey);\n\n if (!context) {\n return throwContextError();\n }\n\n const { isAuthenticated, isLoading, logtoClient, error } = context;\n const { handleSignInCallback } = createPluginMethods(context);\n\n watchEffect(() => {\n const currentPageUrl = window.location.href;\n\n if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {\n void handleSignInCallback(currentPageUrl, callback);\n }\n });\n\n return {\n isLoading: readonly(isLoading),\n isAuthenticated: readonly(isAuthenticated),\n error: readonly(error),\n };\n};\n","export const logtoInjectionKey = '@logto/vue';\nexport const contextInjectionKey = '@logto/vue:context';\n","import LogtoClient from '@logto/browser';\nimport { computed, ComputedRef, reactive, Ref, toRefs, UnwrapRef } from 'vue';\n\ntype LogtoContextProperties = {\n logtoClient: LogtoClient | undefined;\n isAuthenticated: boolean;\n loadingCount: number;\n error: Error | undefined;\n};\n\nexport type Context = {\n // Wrong type workaround. https://github.com/vuejs/core/issues/2981\n logtoClient: Ref<UnwrapRef<LogtoClient | undefined>>;\n isAuthenticated: Ref<boolean>;\n loadingCount: Ref<number>;\n error: Ref<Error | undefined>;\n isLoading: ComputedRef<boolean>;\n setError: (error: unknown, fallbackErrorMessage?: string | undefined) => void;\n setIsAuthenticated: (isAuthenticated: boolean) => void;\n setLoading: (isLoading: boolean) => void;\n};\n\nexport const createContext = (client: LogtoClient): Context => {\n const context = toRefs(\n reactive<LogtoContextProperties>({\n logtoClient: client,\n isAuthenticated: client.isAuthenticated,\n loadingCount: 0,\n error: undefined,\n })\n );\n\n const { isAuthenticated, loadingCount, error } = context;\n\n const isLoading = computed(() => loadingCount.value > 0);\n\n /* eslint-disable @silverhand/fp/no-mutation */\n const setError = (_error: unknown, fallbackErrorMessage?: string) => {\n if (_error instanceof Error) {\n error.value = _error;\n } else if (fallbackErrorMessage) {\n error.value = new Error(fallbackErrorMessage);\n }\n console.error(error);\n };\n\n const setLoading = (isLoading: boolean) => {\n if (isLoading) {\n loadingCount.value += 1;\n } else {\n loadingCount.value = Math.max(0, loadingCount.value - 1);\n }\n };\n\n const setIsAuthenticated = (_isAuthenticated: boolean) => {\n isAuthenticated.value = _isAuthenticated;\n };\n /* eslint-enable @silverhand/fp/no-mutation */\n\n return { ...context, isLoading, setError, setLoading, setIsAuthenticated };\n};\n\nexport const throwContextError = (): never => {\n throw new Error('Must install Logto plugin first.');\n};\n","import { Context, throwContextError } from './context';\n\nexport const createPluginMethods = (context: Context) => {\n const { logtoClient, setLoading, setError, setIsAuthenticated } = context;\n\n const signIn = async (redirectUri: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signIn(redirectUri);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing in.');\n } finally {\n setLoading(false);\n }\n };\n\n const signOut = async (postLogoutRedirectUri?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signOut(postLogoutRedirectUri);\n\n // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately\n // even before navigating to the oidc end session endpoint, which might cause rendering problems.\n // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing out.');\n } finally {\n setLoading(false);\n }\n };\n\n const getAccessToken = async (resource?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n return await logtoClient.value.getAccessToken(resource);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting access token.');\n } finally {\n setLoading(false);\n }\n };\n\n const getIdTokenClaims = () => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n return logtoClient.value.getIdTokenClaims();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting id token claims.');\n }\n };\n\n const handleSignInCallback = async (callbackUri: string, callbackFunction?: () => void) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n await logtoClient.value.handleSignInCallback(callbackUri);\n setIsAuthenticated(true);\n callbackFunction?.();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while handling sign in callback.');\n } finally {\n setLoading(false);\n }\n };\n\n return {\n signIn,\n signOut,\n getAccessToken,\n getIdTokenClaims,\n handleSignInCallback,\n };\n};\n"],"names":[],"version":3,"file":"index.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;AAAA;;ACAO,MAAM,yCAAiB,GAAG,YAAY,AAAC;AACvC,MAAM,yCAAmB,GAAG,oBAAoB,AAAC;;;ACDxD;AAsBO,MAAM,yCAAa,GAAG,CAAC,MAAmB,GAAc;IAC7D,MAAM,OAAO,GAAG,CAAA,GAAA,iBAAM,CAAA,CACpB,CAAA,GAAA,mBAAQ,CAAA,CAAyB;QAC/B,WAAW,EAAE,MAAM;QACnB,eAAe,EAAE,KAAK;QACtB,YAAY,EAAE,CAAC;QACf,KAAK,EAAE,SAAS;KACjB,CAAC,CACH,AAAC;IAEF,MAAM,mBAAE,eAAe,CAAA,gBAAE,YAAY,CAAA,SAAE,KAAK,CAAA,EAAE,GAAG,OAAO,AAAC;IAEzD,MAAM,SAAS,GAAG,CAAA,GAAA,mBAAQ,CAAA,CAAC,IAAM,YAAY,CAAC,KAAK,GAAG,CAAC,CAAC,AAAC;IAEzD,+CAA+C,CAC/C,MAAM,QAAQ,GAAG,CAAC,MAAe,EAAE,oBAA6B,GAAK;QACnE,IAAI,MAAM,YAAY,KAAK,EACzB,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC;aAChB,IAAI,oBAAoB,EAC7B,KAAK,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QAEhD,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;KACtB,AAAC;IAEF,MAAM,UAAU,GAAG,CAAC,SAAkB,GAAK;QACzC,IAAI,SAAS,EACX,YAAY,CAAC,KAAK,IAAI,CAAC,CAAC;aAExB,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AAQ,CAAC,CAAC;KAEvD,CAAC,CAAC;IAEH,OAAO;QACL,SAAS,EAAE,CAAA,GAAA,mBAAQ,CAAA,CAAC,SAAS,CAAC;QAC9B,eAAe,EAAE,CAAA,GAAA,mBAAQ,CAAA,CAAC,eAAe,CAAC;QAC1C,KAAK,EAAE,CAAA,GAAA,mBAAQ,CAAA,CAAC,KAAK,CAAC;KACvB,CAAC;CACH,AAAC","sources":["packages/vue/src/index.ts","packages/vue/src/consts.ts","packages/vue/src/context.ts","packages/vue/src/plugin.ts"],"sourcesContent":["import LogtoClient, { IdTokenClaims, LogtoConfig, UserInfoResponse } from '@logto/browser';\nimport { App, inject, readonly, Ref, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport { Context, createContext, throwContextError } from './context';\nimport { createPluginMethods } from './plugin';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n UserInfoResponse,\n LogtoErrorCode,\n LogtoClientErrorCode,\n} from '@logto/browser';\n\nexport {\n LogtoError,\n LogtoClientError,\n OidcError,\n Prompt,\n ReservedScope,\n UserScope,\n} from '@logto/browser';\n\ntype LogtoVuePlugin = {\n install: (app: App, config: LogtoConfig) => void;\n};\n\ntype Logto = {\n isAuthenticated: Readonly<Ref<boolean>>;\n isLoading: Readonly<Ref<boolean>>;\n error: Readonly<Ref<Error | undefined>>;\n fetchUserInfo: () => Promise<UserInfoResponse | undefined>;\n getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;\n signIn: (redirectUri: string) => Promise<void>;\n signOut: (postLogoutRedirectUri?: string) => Promise<void>;\n};\n\n/**\n * Creates the Logto Vue plugin\n *\n * ```ts\n * import { createApp } from 'vue';\n * import { createLogto } from '@logto/vue';\n *\n * const app = createApp(App);\n * const app.use(createLogto, {\n * appId: '<your-app-id>',\n * endpoint: '<your-oidc-endpoint-domain>',\n * });\n *\n * app.mount('#app');\n * ```\n *\n * Use this in your Vue root component to register the plugin\n */\nexport const createLogto: LogtoVuePlugin = {\n install(app: App, config: LogtoConfig) {\n const client = new LogtoClient(config);\n const context = createContext(client);\n const pluginMethods = createPluginMethods(context);\n const { isAuthenticated, isLoading, error } = context;\n\n app.provide<Context>(contextInjectionKey, context);\n app.provide<Logto>(logtoInjectionKey, {\n isAuthenticated: readonly(isAuthenticated),\n isLoading: readonly(isLoading),\n error: readonly(error),\n ...pluginMethods,\n });\n },\n};\n\n/**\n * A Vue composable method that provides the Logto reactive refs and auth methods.\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n *\n * export default {\n * setup() {\n * const { isAuthenticated, signIn } = useLogto();\n *\n * return {\n * isAuthenticated,\n * onClickSignIn: () => {\n * signIn('<your-redirect-uri>');\n * },\n * }\n * }\n * }\n * ```\n *\n * Use this composable in the setup script of your Vue component to make sure the injection works\n */\nexport const useLogto = (): Logto => {\n const logto = inject<Logto>(logtoInjectionKey);\n\n if (!logto) {\n return throwContextError();\n }\n\n return logto;\n};\n\n/**\n * A Vue composable method that watches browser navigation and automatically handles the sign-in callback\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n * import { useHandleSignInCallback } from '@logto/vue';\n *\n * export default {\n * setup() {\n * useHandleSignInCallback();\n * }\n * }\n * ```\n *\n * Use this in the setup script of your Callback page to make sure the injection works\n */\nexport const useHandleSignInCallback = (callback?: () => void) => {\n const context = inject<Context>(contextInjectionKey);\n\n if (!context) {\n return throwContextError();\n }\n\n const { isAuthenticated, isLoading, logtoClient, error } = context;\n const { handleSignInCallback } = createPluginMethods(context);\n\n watchEffect(() => {\n const currentPageUrl = window.location.href;\n\n if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {\n void handleSignInCallback(currentPageUrl, callback);\n }\n });\n\n return {\n isLoading: readonly(isLoading),\n isAuthenticated: readonly(isAuthenticated),\n error: readonly(error),\n };\n};\n","export const logtoInjectionKey = '@logto/vue';\nexport const contextInjectionKey = '@logto/vue:context';\n","import LogtoClient from '@logto/browser';\nimport { computed, ComputedRef, reactive, Ref, toRefs, UnwrapRef, watchEffect } from 'vue';\n\ntype LogtoContextProperties = {\n logtoClient: LogtoClient | undefined;\n isAuthenticated: boolean;\n loadingCount: number;\n error: Error | undefined;\n};\n\nexport type Context = {\n // Wrong type workaround. https://github.com/vuejs/core/issues/2981\n logtoClient: Ref<UnwrapRef<LogtoClient | undefined>>;\n isAuthenticated: Ref<boolean>;\n loadingCount: Ref<number>;\n error: Ref<Error | undefined>;\n isLoading: ComputedRef<boolean>;\n setError: (error: unknown, fallbackErrorMessage?: string | undefined) => void;\n setIsAuthenticated: (isAuthenticated: boolean) => void;\n setLoading: (isLoading: boolean) => void;\n};\n\nexport const createContext = (client: LogtoClient): Context => {\n const context = toRefs(\n reactive<LogtoContextProperties>({\n logtoClient: client,\n isAuthenticated: false,\n loadingCount: 1,\n error: undefined,\n })\n );\n\n const { isAuthenticated, loadingCount, error } = context;\n\n const isLoading = computed(() => loadingCount.value > 0);\n\n /* eslint-disable @silverhand/fp/no-mutation */\n const setError = (_error: unknown, fallbackErrorMessage?: string) => {\n if (_error instanceof Error) {\n error.value = _error;\n } else if (fallbackErrorMessage) {\n error.value = new Error(fallbackErrorMessage);\n }\n console.error(error);\n };\n\n const setLoading = (isLoading: boolean) => {\n if (isLoading) {\n loadingCount.value += 1;\n } else {\n loadingCount.value = Math.max(0, loadingCount.value - 1);\n }\n };\n\n const setIsAuthenticated = (_isAuthenticated: boolean) => {\n isAuthenticated.value = _isAuthenticated;\n };\n /* eslint-enable @silverhand/fp/no-mutation */\n\n watchEffect(async () => {\n const isAuthenticated = await client.isAuthenticated();\n\n setIsAuthenticated(isAuthenticated);\n setLoading(false);\n });\n\n return { ...context, isLoading, setError, setLoading, setIsAuthenticated };\n};\n\nexport const throwContextError = (): never => {\n throw new Error('Must install Logto plugin first.');\n};\n","import { Context, throwContextError } from './context';\n\nexport const createPluginMethods = (context: Context) => {\n const { logtoClient, setLoading, setError, setIsAuthenticated } = context;\n\n const signIn = async (redirectUri: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signIn(redirectUri);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing in.');\n }\n };\n\n const signOut = async (postLogoutRedirectUri?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signOut(postLogoutRedirectUri);\n\n // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately\n // even before navigating to the oidc end session endpoint, which might cause rendering problems.\n // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing out.');\n } finally {\n setLoading(false);\n }\n };\n\n const fetchUserInfo = async () => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n return await logtoClient.value.fetchUserInfo();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while fetching user info.');\n } finally {\n setLoading(false);\n }\n };\n\n const getAccessToken = async (resource?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n return await logtoClient.value.getAccessToken(resource);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting access token.');\n } finally {\n setLoading(false);\n }\n };\n\n const getIdTokenClaims = async () => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n return await logtoClient.value.getIdTokenClaims();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting id token claims.');\n }\n };\n\n const handleSignInCallback = async (callbackUri: string, callbackFunction?: () => void) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n await logtoClient.value.handleSignInCallback(callbackUri);\n setIsAuthenticated(true);\n callbackFunction?.();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while handling sign in callback.');\n } finally {\n setLoading(false);\n }\n };\n\n return {\n signIn,\n signOut,\n fetchUserInfo,\n getAccessToken,\n getIdTokenClaims,\n handleSignInCallback,\n };\n};\n"],"names":[],"version":3,"file":"index.js.map"}
|
package/lib/module.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import $jwuiT$logtobrowser, {LogtoError as $a52d863b14a9eb8e$re_export$LogtoError, LogtoClientError as $a52d863b14a9eb8e$re_export$LogtoClientError, OidcError as $a52d863b14a9eb8e$re_export$OidcError, Prompt as $a52d863b14a9eb8e$re_export$Prompt} from "@logto/browser";
|
|
1
|
+
import $jwuiT$logtobrowser, {LogtoError as $a52d863b14a9eb8e$re_export$LogtoError, LogtoClientError as $a52d863b14a9eb8e$re_export$LogtoClientError, OidcError as $a52d863b14a9eb8e$re_export$OidcError, Prompt as $a52d863b14a9eb8e$re_export$Prompt, ReservedScope as $a52d863b14a9eb8e$re_export$ReservedScope, UserScope as $a52d863b14a9eb8e$re_export$UserScope} from "@logto/browser";
|
|
2
2
|
import {readonly as $jwuiT$readonly, inject as $jwuiT$inject, watchEffect as $jwuiT$watchEffect, toRefs as $jwuiT$toRefs, reactive as $jwuiT$reactive, computed as $jwuiT$computed} from "vue";
|
|
3
3
|
|
|
4
4
|
|
|
@@ -11,12 +11,12 @@ const $cda0d5851de767d1$export$951b969b1220de04 = "@logto/vue:context";
|
|
|
11
11
|
const $f63beecb5f2c2ea1$export$fd42f52fd3ae1109 = (client)=>{
|
|
12
12
|
const context = (0, $jwuiT$toRefs)((0, $jwuiT$reactive)({
|
|
13
13
|
logtoClient: client,
|
|
14
|
-
isAuthenticated:
|
|
15
|
-
loadingCount:
|
|
14
|
+
isAuthenticated: false,
|
|
15
|
+
loadingCount: 1,
|
|
16
16
|
error: undefined
|
|
17
17
|
}));
|
|
18
18
|
const { isAuthenticated: isAuthenticated , loadingCount: loadingCount , error: error } = context;
|
|
19
|
-
const
|
|
19
|
+
const isLoading = (0, $jwuiT$computed)(()=>loadingCount.value > 0);
|
|
20
20
|
/* eslint-disable @silverhand/fp/no-mutation */ const setError = (_error, fallbackErrorMessage)=>{
|
|
21
21
|
if (_error instanceof Error) error.value = _error;
|
|
22
22
|
else if (fallbackErrorMessage) error.value = new Error(fallbackErrorMessage);
|
|
@@ -29,9 +29,14 @@ const $f63beecb5f2c2ea1$export$fd42f52fd3ae1109 = (client)=>{
|
|
|
29
29
|
const setIsAuthenticated = (_isAuthenticated)=>{
|
|
30
30
|
isAuthenticated.value = _isAuthenticated;
|
|
31
31
|
};
|
|
32
|
-
/* eslint-enable @silverhand/fp/no-mutation */
|
|
32
|
+
/* eslint-enable @silverhand/fp/no-mutation */ (0, $jwuiT$watchEffect)(async ()=>{
|
|
33
|
+
const isAuthenticated = await client.isAuthenticated();
|
|
34
|
+
setIsAuthenticated(isAuthenticated);
|
|
35
|
+
setLoading(false);
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
33
38
|
...context,
|
|
34
|
-
isLoading:
|
|
39
|
+
isLoading: isLoading,
|
|
35
40
|
setError: setError,
|
|
36
41
|
setLoading: setLoading,
|
|
37
42
|
setIsAuthenticated: setIsAuthenticated
|
|
@@ -52,8 +57,6 @@ const $eec30d751f04969b$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
52
57
|
await logtoClient.value.signIn(redirectUri);
|
|
53
58
|
} catch (error) {
|
|
54
59
|
setError(error, "Unexpected error occurred while signing in.");
|
|
55
|
-
} finally{
|
|
56
|
-
setLoading(false);
|
|
57
60
|
}
|
|
58
61
|
};
|
|
59
62
|
const signOut = async (postLogoutRedirectUri)=>{
|
|
@@ -70,6 +73,17 @@ const $eec30d751f04969b$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
70
73
|
setLoading(false);
|
|
71
74
|
}
|
|
72
75
|
};
|
|
76
|
+
const fetchUserInfo = async ()=>{
|
|
77
|
+
if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
|
|
78
|
+
try {
|
|
79
|
+
setLoading(true);
|
|
80
|
+
return await logtoClient.value.fetchUserInfo();
|
|
81
|
+
} catch (error) {
|
|
82
|
+
setError(error, "Unexpected error occurred while fetching user info.");
|
|
83
|
+
} finally{
|
|
84
|
+
setLoading(false);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
73
87
|
const getAccessToken = async (resource)=>{
|
|
74
88
|
if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
|
|
75
89
|
try {
|
|
@@ -81,10 +95,10 @@ const $eec30d751f04969b$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
81
95
|
setLoading(false);
|
|
82
96
|
}
|
|
83
97
|
};
|
|
84
|
-
const getIdTokenClaims = ()=>{
|
|
98
|
+
const getIdTokenClaims = async ()=>{
|
|
85
99
|
if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
|
|
86
100
|
try {
|
|
87
|
-
return logtoClient.value.getIdTokenClaims();
|
|
101
|
+
return await logtoClient.value.getIdTokenClaims();
|
|
88
102
|
} catch (error) {
|
|
89
103
|
setError(error, "Unexpected error occurred while getting id token claims.");
|
|
90
104
|
}
|
|
@@ -105,6 +119,7 @@ const $eec30d751f04969b$export$6ad57c8ed6bec4 = (context)=>{
|
|
|
105
119
|
return {
|
|
106
120
|
signIn: signIn,
|
|
107
121
|
signOut: signOut,
|
|
122
|
+
fetchUserInfo: fetchUserInfo,
|
|
108
123
|
getAccessToken: getAccessToken,
|
|
109
124
|
getIdTokenClaims: getIdTokenClaims,
|
|
110
125
|
handleSignInCallback: handleSignInCallback
|
|
@@ -150,5 +165,5 @@ const $a52d863b14a9eb8e$export$84e88c4b3c082374 = (callback)=>{
|
|
|
150
165
|
};
|
|
151
166
|
|
|
152
167
|
|
|
153
|
-
export {$a52d863b14a9eb8e$export$d8f887f9089028d2 as createLogto, $a52d863b14a9eb8e$export$44fc9df4d2a1789a as useLogto, $a52d863b14a9eb8e$export$84e88c4b3c082374 as useHandleSignInCallback, $a52d863b14a9eb8e$re_export$LogtoError as LogtoError, $a52d863b14a9eb8e$re_export$LogtoClientError as LogtoClientError, $a52d863b14a9eb8e$re_export$OidcError as OidcError, $a52d863b14a9eb8e$re_export$Prompt as Prompt};
|
|
168
|
+
export {$a52d863b14a9eb8e$export$d8f887f9089028d2 as createLogto, $a52d863b14a9eb8e$export$44fc9df4d2a1789a as useLogto, $a52d863b14a9eb8e$export$84e88c4b3c082374 as useHandleSignInCallback, $a52d863b14a9eb8e$re_export$LogtoError as LogtoError, $a52d863b14a9eb8e$re_export$LogtoClientError as LogtoClientError, $a52d863b14a9eb8e$re_export$OidcError as OidcError, $a52d863b14a9eb8e$re_export$Prompt as Prompt, $a52d863b14a9eb8e$re_export$ReservedScope as ReservedScope, $a52d863b14a9eb8e$re_export$UserScope as UserScope};
|
|
154
169
|
//# sourceMappingURL=module.js.map
|
package/lib/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;ACAO,MAAM,yCAAiB,GAAG,YAAY,AAAC;AACvC,MAAM,yCAAmB,GAAG,oBAAoB,AAAC;;;ACDxD;AAsBO,MAAM,yCAAa,GAAG,CAAC,MAAmB,GAAc;IAC7D,MAAM,OAAO,GAAG,CAAA,GAAA,aAAM,CAAA,CACpB,CAAA,GAAA,eAAQ,CAAA,CAAyB;QAC/B,WAAW,EAAE,MAAM;QACnB,eAAe,EAAE,MAAM,CAAC,eAAe;QACvC,YAAY,EAAE,CAAC;QACf,KAAK,EAAE,SAAS;KACjB,CAAC,CACH,AAAC;IAEF,MAAM,mBAAE,eAAe,CAAA,gBAAE,YAAY,CAAA,SAAE,KAAK,CAAA,EAAE,GAAG,OAAO,AAAC;IAEzD,MAAM,UAAS,GAAG,CAAA,GAAA,eAAQ,CAAA,CAAC,IAAM,YAAY,CAAC,KAAK,GAAG,CAAC,CAAC,AAAC;IAEzD,+CAA+C,CAC/C,MAAM,QAAQ,GAAG,CAAC,MAAe,EAAE,oBAA6B,GAAK;QACnE,IAAI,MAAM,YAAY,KAAK,EACzB,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC;aAChB,IAAI,o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consts.ts","packages/vue/src/context.ts","packages/vue/src/plugin.ts"],"sourcesContent":["import LogtoClient, { IdTokenClaims, LogtoConfig } from '@logto/browser';\nimport { App, inject, readonly, Ref, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport { Context, createContext, throwContextError } from './context';\nimport { createPluginMethods } from './plugin';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n LogtoErrorCode,\n LogtoClientErrorCode,\n} from '@logto/browser';\n\nexport { LogtoError, LogtoClientError, OidcError, Prompt } from '@logto/browser';\n\ntype LogtoVuePlugin = {\n install: (app: App, config: LogtoConfig) => void;\n};\n\ntype Logto = {\n isAuthenticated: Readonly<Ref<boolean>>;\n isLoading: Readonly<Ref<boolean>>;\n error: Readonly<Ref<Error | undefined>>;\n getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => IdTokenClaims | undefined;\n signIn: (redirectUri: string) => Promise<void>;\n signOut: (postLogoutRedirectUri?: string) => Promise<void>;\n};\n\n/**\n * Creates the Logto Vue plugin\n *\n * ```ts\n * import { createApp } from 'vue';\n * import { createLogto } from '@logto/vue';\n *\n * const app = createApp(App);\n * const app.use(createLogto, {\n * appId: '<your-app-id>',\n * endpoint: '<your-oidc-endpoint-domain>',\n * });\n *\n * app.mount('#app');\n * ```\n *\n * Use this in your Vue root component to register the plugin\n */\nexport const createLogto: LogtoVuePlugin = {\n install(app: App, config: LogtoConfig) {\n const client = new LogtoClient(config);\n const context = createContext(client);\n const pluginMethods = createPluginMethods(context);\n const { isAuthenticated, isLoading, error } = context;\n\n app.provide<Context>(contextInjectionKey, context);\n app.provide<Logto>(logtoInjectionKey, {\n isAuthenticated: readonly(isAuthenticated),\n isLoading: readonly(isLoading),\n error: readonly(error),\n ...pluginMethods,\n });\n },\n};\n\n/**\n * A Vue composable method that provides the Logto reactive refs and auth methods.\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n *\n * export default {\n * setup() {\n * const { isAuthenticated, signIn } = useLogto();\n *\n * return {\n * isAuthenticated,\n * onClickSignIn: () => {\n * signIn('<your-redirect-uri>');\n * },\n * }\n * }\n * }\n * ```\n *\n * Use this composable in the setup script of your Vue component to make sure the injection works\n */\nexport const useLogto = (): Logto => {\n const logto = inject<Logto>(logtoInjectionKey);\n\n if (!logto) {\n return throwContextError();\n }\n\n return logto;\n};\n\n/**\n * A Vue composable method that watches browser navigation and automatically handles the sign-in callback\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n * import { useHandleSignInCallback } from '@logto/vue';\n *\n * export default {\n * setup() {\n * useHandleSignInCallback();\n * }\n * }\n * ```\n *\n * Use this in the setup script of your Callback page to make sure the injection works\n */\nexport const useHandleSignInCallback = (callback?: () => void) => {\n const context = inject<Context>(contextInjectionKey);\n\n if (!context) {\n return throwContextError();\n }\n\n const { isAuthenticated, isLoading, logtoClient, error } = context;\n const { handleSignInCallback } = createPluginMethods(context);\n\n watchEffect(() => {\n const currentPageUrl = window.location.href;\n\n if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {\n void handleSignInCallback(currentPageUrl, callback);\n }\n });\n\n return {\n isLoading: readonly(isLoading),\n isAuthenticated: readonly(isAuthenticated),\n error: readonly(error),\n };\n};\n","export const logtoInjectionKey = '@logto/vue';\nexport const contextInjectionKey = '@logto/vue:context';\n","import LogtoClient from '@logto/browser';\nimport { computed, ComputedRef, reactive, Ref, toRefs, UnwrapRef } from 'vue';\n\ntype LogtoContextProperties = {\n logtoClient: LogtoClient | undefined;\n isAuthenticated: boolean;\n loadingCount: number;\n error: Error | undefined;\n};\n\nexport type Context = {\n // Wrong type workaround. https://github.com/vuejs/core/issues/2981\n logtoClient: Ref<UnwrapRef<LogtoClient | undefined>>;\n isAuthenticated: Ref<boolean>;\n loadingCount: Ref<number>;\n error: Ref<Error | undefined>;\n isLoading: ComputedRef<boolean>;\n setError: (error: unknown, fallbackErrorMessage?: string | undefined) => void;\n setIsAuthenticated: (isAuthenticated: boolean) => void;\n setLoading: (isLoading: boolean) => void;\n};\n\nexport const createContext = (client: LogtoClient): Context => {\n const context = toRefs(\n reactive<LogtoContextProperties>({\n logtoClient: client,\n isAuthenticated: client.isAuthenticated,\n loadingCount: 0,\n error: undefined,\n })\n );\n\n const { isAuthenticated, loadingCount, error } = context;\n\n const isLoading = computed(() => loadingCount.value > 0);\n\n /* eslint-disable @silverhand/fp/no-mutation */\n const setError = (_error: unknown, fallbackErrorMessage?: string) => {\n if (_error instanceof Error) {\n error.value = _error;\n } else if (fallbackErrorMessage) {\n error.value = new Error(fallbackErrorMessage);\n }\n console.error(error);\n };\n\n const setLoading = (isLoading: boolean) => {\n if (isLoading) {\n loadingCount.value += 1;\n } else {\n loadingCount.value = Math.max(0, loadingCount.value - 1);\n }\n };\n\n const setIsAuthenticated = (_isAuthenticated: boolean) => {\n isAuthenticated.value = _isAuthenticated;\n };\n /* eslint-enable @silverhand/fp/no-mutation */\n\n return { ...context, isLoading, setError, setLoading, setIsAuthenticated };\n};\n\nexport const throwContextError = (): never => {\n throw new Error('Must install Logto plugin first.');\n};\n","import { Context, throwContextError } from './context';\n\nexport const createPluginMethods = (context: Context) => {\n const { logtoClient, setLoading, setError, setIsAuthenticated } = context;\n\n const signIn = async (redirectUri: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signIn(redirectUri);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing in.');\n } finally {\n setLoading(false);\n }\n };\n\n const signOut = async (postLogoutRedirectUri?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signOut(postLogoutRedirectUri);\n\n // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately\n // even before navigating to the oidc end session endpoint, which might cause rendering problems.\n // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing out.');\n } finally {\n setLoading(false);\n }\n };\n\n const getAccessToken = async (resource?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n return await logtoClient.value.getAccessToken(resource);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting access token.');\n } finally {\n setLoading(false);\n }\n };\n\n const getIdTokenClaims = () => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n return logtoClient.value.getIdTokenClaims();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting id token claims.');\n }\n };\n\n const handleSignInCallback = async (callbackUri: string, callbackFunction?: () => void) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n await logtoClient.value.handleSignInCallback(callbackUri);\n setIsAuthenticated(true);\n callbackFunction?.();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while handling sign in callback.');\n } finally {\n setLoading(false);\n }\n };\n\n return {\n signIn,\n signOut,\n getAccessToken,\n getIdTokenClaims,\n handleSignInCallback,\n };\n};\n"],"names":[],"version":3,"file":"module.js.map"}
|
|
1
|
+
{"mappings":";;;AAAA;;ACAO,MAAM,yCAAiB,GAAG,YAAY,AAAC;AACvC,MAAM,yCAAmB,GAAG,oBAAoB,AAAC;;;ACDxD;AAsBO,MAAM,yCAAa,GAAG,CAAC,MAAmB,GAAc;IAC7D,MAAM,OAAO,GAAG,CAAA,GAAA,aAAM,CAAA,CACpB,CAAA,GAAA,eAAQ,CAAA,CAAyB;QAC/B,WAAW,EAAE,MAAM;QACnB,eAAe,EAAE,KAAK;QACtB,YAAY,EAAE,CAAC;QACf,KAAK,EAAE,SAAS;KACjB,CAAC,CACH,AAAC;IAEF,MAAM,mBAAE,eAAe,CAAA,gBAAE,YAAY,CAAA,SAAE,KAAK,CAAA,EAAE,GAAG,OAAO,AAAC;IAEzD,MAAM,SAAS,GAAG,CAAA,GAAA,eAAQ,CAAA,CAAC,IAAM,YAAY,CAAC,KAAK,GAAG,CAAC,CAAC,AAAC;IAEzD,+CAA+C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,WAAW,CAAA,SAAE,KAAK,CAAA,EAAE,GAAG,OAAO,AAAC;IACnE,MAAM,wBAAE,oBAAoB,CAAA,EAAE,GAAG,CAAA,GAAA,uCAAmB,CAAA,CAAC,OAAO,CAAC,AAAC;IAE9D,CAAA,GAAA,kBAAW,CAAA,CAAC,IAAM;QAChB,MAAM,cAAc,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,AAAC;QAE5C,IAAI,CAAC,eAAe,CAAC,KAAK,IAAI,WAAW,CAAC,KAAK,EAAE,kBAAkB,CAAC,cAAc,CAAC,EAC5E,oBAAoB,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;KAEvD,CAAC,CAAC;IAEH,OAAO;QACL,SAAS,EAAE,CAAA,GAAA,eAAQ,CAAA,CAAC,SAAS,CAAC;QAC9B,eAAe,EAAE,CAAA,GAAA,eAAQ,CAAA,CAAC,eAAe,CAAC;QAC1C,KAAK,EAAE,CAAA,GAAA,eAAQ,CAAA,CAAC,KAAK,CAAC;KACvB,CAAC;CACH,AAAC","sources":["packages/vue/src/index.ts","packages/vue/src/consts.ts","packages/vue/src/context.ts","packages/vue/src/plugin.ts"],"sourcesContent":["import LogtoClient, { IdTokenClaims, LogtoConfig, UserInfoResponse } from '@logto/browser';\nimport { App, inject, readonly, Ref, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport { Context, createContext, throwContextError } from './context';\nimport { createPluginMethods } from './plugin';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n UserInfoResponse,\n LogtoErrorCode,\n LogtoClientErrorCode,\n} from '@logto/browser';\n\nexport {\n LogtoError,\n LogtoClientError,\n OidcError,\n Prompt,\n ReservedScope,\n UserScope,\n} from '@logto/browser';\n\ntype LogtoVuePlugin = {\n install: (app: App, config: LogtoConfig) => void;\n};\n\ntype Logto = {\n isAuthenticated: Readonly<Ref<boolean>>;\n isLoading: Readonly<Ref<boolean>>;\n error: Readonly<Ref<Error | undefined>>;\n fetchUserInfo: () => Promise<UserInfoResponse | undefined>;\n getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;\n signIn: (redirectUri: string) => Promise<void>;\n signOut: (postLogoutRedirectUri?: string) => Promise<void>;\n};\n\n/**\n * Creates the Logto Vue plugin\n *\n * ```ts\n * import { createApp } from 'vue';\n * import { createLogto } from '@logto/vue';\n *\n * const app = createApp(App);\n * const app.use(createLogto, {\n * appId: '<your-app-id>',\n * endpoint: '<your-oidc-endpoint-domain>',\n * });\n *\n * app.mount('#app');\n * ```\n *\n * Use this in your Vue root component to register the plugin\n */\nexport const createLogto: LogtoVuePlugin = {\n install(app: App, config: LogtoConfig) {\n const client = new LogtoClient(config);\n const context = createContext(client);\n const pluginMethods = createPluginMethods(context);\n const { isAuthenticated, isLoading, error } = context;\n\n app.provide<Context>(contextInjectionKey, context);\n app.provide<Logto>(logtoInjectionKey, {\n isAuthenticated: readonly(isAuthenticated),\n isLoading: readonly(isLoading),\n error: readonly(error),\n ...pluginMethods,\n });\n },\n};\n\n/**\n * A Vue composable method that provides the Logto reactive refs and auth methods.\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n *\n * export default {\n * setup() {\n * const { isAuthenticated, signIn } = useLogto();\n *\n * return {\n * isAuthenticated,\n * onClickSignIn: () => {\n * signIn('<your-redirect-uri>');\n * },\n * }\n * }\n * }\n * ```\n *\n * Use this composable in the setup script of your Vue component to make sure the injection works\n */\nexport const useLogto = (): Logto => {\n const logto = inject<Logto>(logtoInjectionKey);\n\n if (!logto) {\n return throwContextError();\n }\n\n return logto;\n};\n\n/**\n * A Vue composable method that watches browser navigation and automatically handles the sign-in callback\n *\n * ```ts\n * import { useLogto } from '@logto/vue';\n * import { useHandleSignInCallback } from '@logto/vue';\n *\n * export default {\n * setup() {\n * useHandleSignInCallback();\n * }\n * }\n * ```\n *\n * Use this in the setup script of your Callback page to make sure the injection works\n */\nexport const useHandleSignInCallback = (callback?: () => void) => {\n const context = inject<Context>(contextInjectionKey);\n\n if (!context) {\n return throwContextError();\n }\n\n const { isAuthenticated, isLoading, logtoClient, error } = context;\n const { handleSignInCallback } = createPluginMethods(context);\n\n watchEffect(() => {\n const currentPageUrl = window.location.href;\n\n if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {\n void handleSignInCallback(currentPageUrl, callback);\n }\n });\n\n return {\n isLoading: readonly(isLoading),\n isAuthenticated: readonly(isAuthenticated),\n error: readonly(error),\n };\n};\n","export const logtoInjectionKey = '@logto/vue';\nexport const contextInjectionKey = '@logto/vue:context';\n","import LogtoClient from '@logto/browser';\nimport { computed, ComputedRef, reactive, Ref, toRefs, UnwrapRef, watchEffect } from 'vue';\n\ntype LogtoContextProperties = {\n logtoClient: LogtoClient | undefined;\n isAuthenticated: boolean;\n loadingCount: number;\n error: Error | undefined;\n};\n\nexport type Context = {\n // Wrong type workaround. https://github.com/vuejs/core/issues/2981\n logtoClient: Ref<UnwrapRef<LogtoClient | undefined>>;\n isAuthenticated: Ref<boolean>;\n loadingCount: Ref<number>;\n error: Ref<Error | undefined>;\n isLoading: ComputedRef<boolean>;\n setError: (error: unknown, fallbackErrorMessage?: string | undefined) => void;\n setIsAuthenticated: (isAuthenticated: boolean) => void;\n setLoading: (isLoading: boolean) => void;\n};\n\nexport const createContext = (client: LogtoClient): Context => {\n const context = toRefs(\n reactive<LogtoContextProperties>({\n logtoClient: client,\n isAuthenticated: false,\n loadingCount: 1,\n error: undefined,\n })\n );\n\n const { isAuthenticated, loadingCount, error } = context;\n\n const isLoading = computed(() => loadingCount.value > 0);\n\n /* eslint-disable @silverhand/fp/no-mutation */\n const setError = (_error: unknown, fallbackErrorMessage?: string) => {\n if (_error instanceof Error) {\n error.value = _error;\n } else if (fallbackErrorMessage) {\n error.value = new Error(fallbackErrorMessage);\n }\n console.error(error);\n };\n\n const setLoading = (isLoading: boolean) => {\n if (isLoading) {\n loadingCount.value += 1;\n } else {\n loadingCount.value = Math.max(0, loadingCount.value - 1);\n }\n };\n\n const setIsAuthenticated = (_isAuthenticated: boolean) => {\n isAuthenticated.value = _isAuthenticated;\n };\n /* eslint-enable @silverhand/fp/no-mutation */\n\n watchEffect(async () => {\n const isAuthenticated = await client.isAuthenticated();\n\n setIsAuthenticated(isAuthenticated);\n setLoading(false);\n });\n\n return { ...context, isLoading, setError, setLoading, setIsAuthenticated };\n};\n\nexport const throwContextError = (): never => {\n throw new Error('Must install Logto plugin first.');\n};\n","import { Context, throwContextError } from './context';\n\nexport const createPluginMethods = (context: Context) => {\n const { logtoClient, setLoading, setError, setIsAuthenticated } = context;\n\n const signIn = async (redirectUri: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signIn(redirectUri);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing in.');\n }\n };\n\n const signOut = async (postLogoutRedirectUri?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n await logtoClient.value.signOut(postLogoutRedirectUri);\n\n // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately\n // even before navigating to the oidc end session endpoint, which might cause rendering problems.\n // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while signing out.');\n } finally {\n setLoading(false);\n }\n };\n\n const fetchUserInfo = async () => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n return await logtoClient.value.fetchUserInfo();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while fetching user info.');\n } finally {\n setLoading(false);\n }\n };\n\n const getAccessToken = async (resource?: string) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n\n return await logtoClient.value.getAccessToken(resource);\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting access token.');\n } finally {\n setLoading(false);\n }\n };\n\n const getIdTokenClaims = async () => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n return await logtoClient.value.getIdTokenClaims();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while getting id token claims.');\n }\n };\n\n const handleSignInCallback = async (callbackUri: string, callbackFunction?: () => void) => {\n if (!logtoClient.value) {\n return throwContextError();\n }\n\n try {\n setLoading(true);\n await logtoClient.value.handleSignInCallback(callbackUri);\n setIsAuthenticated(true);\n callbackFunction?.();\n } catch (error: unknown) {\n setError(error, 'Unexpected error occurred while handling sign in callback.');\n } finally {\n setLoading(false);\n }\n };\n\n return {\n signIn,\n signOut,\n fetchUserInfo,\n getAccessToken,\n getIdTokenClaims,\n handleSignInCallback,\n };\n};\n"],"names":[],"version":3,"file":"module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/vue",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -29,25 +29,25 @@
|
|
|
29
29
|
"prepack": "pnpm test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@logto/browser": "^1.0.0-beta.
|
|
32
|
+
"@logto/browser": "^1.0.0-beta.10"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@jest/types": "^27.5.1",
|
|
36
|
-
"@parcel/core": "^2.
|
|
37
|
-
"@parcel/packager-ts": "^2.
|
|
38
|
-
"@parcel/transformer-typescript-types": "^2.
|
|
39
|
-
"@silverhand/eslint-config": "^0.
|
|
40
|
-
"@silverhand/ts-config": "^0.
|
|
36
|
+
"@parcel/core": "^2.7.0",
|
|
37
|
+
"@parcel/packager-ts": "^2.7.0",
|
|
38
|
+
"@parcel/transformer-typescript-types": "^2.7.0",
|
|
39
|
+
"@silverhand/eslint-config": "^1.0.0",
|
|
40
|
+
"@silverhand/ts-config": "^1.0.0",
|
|
41
41
|
"@types/jest": "^27.4.1",
|
|
42
|
-
"eslint": "^8.
|
|
42
|
+
"eslint": "^8.23.0",
|
|
43
43
|
"jest": "^27.5.1",
|
|
44
44
|
"lint-staged": "^13.0.0",
|
|
45
|
-
"parcel": "^2.
|
|
45
|
+
"parcel": "^2.7.0",
|
|
46
46
|
"postcss": "^8.4.6",
|
|
47
|
-
"prettier": "^2.
|
|
48
|
-
"stylelint": "^14.
|
|
47
|
+
"prettier": "^2.7.1",
|
|
48
|
+
"stylelint": "^14.11.0",
|
|
49
49
|
"ts-jest": "^27.0.4",
|
|
50
|
-
"typescript": "
|
|
50
|
+
"typescript": "4.7.4",
|
|
51
51
|
"vue": "^3.2.35"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
@@ -70,5 +70,5 @@
|
|
|
70
70
|
"publishConfig": {
|
|
71
71
|
"access": "public"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "c79c3759b7f15c44bf5b476dcebd82e9c96e9dd8"
|
|
74
74
|
}
|