@logto/vue 0.2.2 → 0.2.3-alpha.0

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/lib/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { IdTokenClaims, LogtoConfig } from '@logto/browser';
2
- import { App, Ref } from 'vue';
1
+ import { IdTokenClaims, LogtoConfig } from "@logto/browser";
2
+ import { App, Ref } from "vue";
3
3
  export type { LogtoConfig, IdTokenClaims, LogtoErrorCode, LogtoClientErrorCode, } from '@logto/browser';
4
4
  export { LogtoError, LogtoClientError, OidcError, Prompt } from '@logto/browser';
5
- declare type LogtoVuePlugin = {
5
+ type LogtoVuePlugin = {
6
6
  install: (app: App, config: LogtoConfig) => void;
7
7
  };
8
- declare type Logto = {
8
+ type Logto = {
9
9
  isAuthenticated: Readonly<Ref<boolean>>;
10
10
  isLoading: Readonly<Ref<boolean>>;
11
11
  error: Readonly<Ref<Error | undefined>>;
@@ -32,7 +32,7 @@ declare type Logto = {
32
32
  *
33
33
  * Use this in your Vue root component to register the plugin
34
34
  */
35
- export declare const createLogto: LogtoVuePlugin;
35
+ export const createLogto: LogtoVuePlugin;
36
36
  /**
37
37
  * A Vue composable method that provides the Logto reactive refs and auth methods.
38
38
  *
@@ -55,7 +55,7 @@ export declare const createLogto: LogtoVuePlugin;
55
55
  *
56
56
  * Use this composable in the setup script of your Vue component to make sure the injection works
57
57
  */
58
- export declare const useLogto: () => Logto;
58
+ export const useLogto: () => Logto;
59
59
  /**
60
60
  * A Vue composable method that watches browser navigation and automatically handles the sign-in callback
61
61
  *
@@ -72,8 +72,10 @@ export declare const useLogto: () => Logto;
72
72
  *
73
73
  * Use this in the setup script of your Callback page to make sure the injection works
74
74
  */
75
- export declare const useHandleSignInCallback: (callback?: (() => void) | undefined) => {
75
+ export const useHandleSignInCallback: (callback?: (() => void) | undefined) => {
76
76
  isLoading: Readonly<Ref<boolean>>;
77
77
  isAuthenticated: Readonly<Ref<boolean>>;
78
78
  error: Readonly<Ref<Error | undefined>>;
79
79
  };
80
+
81
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":";;AGOA,YAAY,EACV,WAAW,EACX,aAAa,EACb,cAAc,EACd,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAEjF,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,cAAc,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACnE,gBAAgB,EAAE,MAAM,aAAa,GAAG,SAAS,CAAC;IAClD,MAAM,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,OAAO,EAAE,CAAC,qBAAqB,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3D,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,MAAM,aAAa,cAezB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,MAAM,gBAAe,KAQ3B,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,OAAO,MAAM,4CAA4C,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 } 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"],"names":[],"version":3,"file":"index.d.ts.map"}
package/lib/index.js CHANGED
@@ -1,115 +1,167 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
1
+ var $Kru5Y$logtobrowser = require("@logto/browser");
2
+ var $Kru5Y$vue = require("vue");
3
+
4
+ function $parcel$interopDefault(a) {
5
+ return a && a.__esModule ? a.default : a;
6
+ }
7
+ function $parcel$export(e, n, v, s) {
8
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
9
+ }
10
+
11
+ $parcel$export(module.exports, "createLogto", () => $b8cbe35fca4d52f8$export$d8f887f9089028d2);
12
+ $parcel$export(module.exports, "useLogto", () => $b8cbe35fca4d52f8$export$44fc9df4d2a1789a);
13
+ $parcel$export(module.exports, "useHandleSignInCallback", () => $b8cbe35fca4d52f8$export$84e88c4b3c082374);
14
+ $parcel$export(module.exports, "LogtoError", () => $b8cbe35fca4d52f8$re_export$LogtoError);
15
+ $parcel$export(module.exports, "LogtoClientError", () => $b8cbe35fca4d52f8$re_export$LogtoClientError);
16
+ $parcel$export(module.exports, "OidcError", () => $b8cbe35fca4d52f8$re_export$OidcError);
17
+ $parcel$export(module.exports, "Prompt", () => $b8cbe35fca4d52f8$re_export$Prompt);
18
+
19
+
20
+ const $bf8f9fe4f6be54e2$export$58f3af87e7a1a85a = "@logto/vue";
21
+ const $bf8f9fe4f6be54e2$export$951b969b1220de04 = "@logto/vue:context";
22
+
23
+
24
+
25
+ const $a89e3697ca8ff325$export$fd42f52fd3ae1109 = (client)=>{
26
+ const context = (0, $Kru5Y$vue.toRefs)((0, $Kru5Y$vue.reactive)({
27
+ logtoClient: client,
28
+ isAuthenticated: client.isAuthenticated,
29
+ loadingCount: 0,
30
+ error: undefined
31
+ }));
32
+ const { isAuthenticated: isAuthenticated , loadingCount: loadingCount , error: error } = context;
33
+ const isLoading1 = (0, $Kru5Y$vue.computed)(()=>loadingCount.value > 0);
34
+ /* eslint-disable @silverhand/fp/no-mutation */ const setError = (_error, fallbackErrorMessage)=>{
35
+ if (_error instanceof Error) error.value = _error;
36
+ else if (fallbackErrorMessage) error.value = new Error(fallbackErrorMessage);
37
+ console.error(error);
38
+ };
39
+ const setLoading = (isLoading)=>{
40
+ if (isLoading) loadingCount.value += 1;
41
+ else loadingCount.value = Math.max(0, loadingCount.value - 1);
42
+ };
43
+ const setIsAuthenticated = (_isAuthenticated)=>{
44
+ isAuthenticated.value = _isAuthenticated;
45
+ };
46
+ /* eslint-enable @silverhand/fp/no-mutation */ return {
47
+ ...context,
48
+ isLoading: isLoading1,
49
+ setError: setError,
50
+ setLoading: setLoading,
51
+ setIsAuthenticated: setIsAuthenticated
52
+ };
4
53
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useHandleSignInCallback = exports.useLogto = exports.createLogto = exports.Prompt = exports.OidcError = exports.LogtoClientError = exports.LogtoError = void 0;
7
- const browser_1 = __importDefault(require("@logto/browser"));
8
- const vue_1 = require("vue");
9
- const consts_1 = require("./consts");
10
- const context_1 = require("./context");
11
- const plugin_1 = require("./plugin");
12
- var browser_2 = require("@logto/browser");
13
- Object.defineProperty(exports, "LogtoError", { enumerable: true, get: function () { return browser_2.LogtoError; } });
14
- Object.defineProperty(exports, "LogtoClientError", { enumerable: true, get: function () { return browser_2.LogtoClientError; } });
15
- Object.defineProperty(exports, "OidcError", { enumerable: true, get: function () { return browser_2.OidcError; } });
16
- Object.defineProperty(exports, "Prompt", { enumerable: true, get: function () { return browser_2.Prompt; } });
17
- /**
18
- * Creates the Logto Vue plugin
19
- *
20
- * ```ts
21
- * import { createApp } from 'vue';
22
- * import { createLogto } from '@logto/vue';
23
- *
24
- * const app = createApp(App);
25
- * const app.use(createLogto, {
26
- * appId: '<your-app-id>',
27
- * endpoint: '<your-oidc-endpoint-domain>',
28
- * });
29
- *
30
- * app.mount('#app');
31
- * ```
32
- *
33
- * Use this in your Vue root component to register the plugin
34
- */
35
- exports.createLogto = {
36
- install(app, config) {
37
- const client = new browser_1.default(config);
38
- const context = (0, context_1.createContext)(client);
39
- const pluginMethods = (0, plugin_1.createPluginMethods)(context);
40
- const { isAuthenticated, isLoading, error } = context;
41
- app.provide(consts_1.contextInjectionKey, context);
42
- app.provide(consts_1.logtoInjectionKey, {
43
- isAuthenticated: (0, vue_1.readonly)(isAuthenticated),
44
- isLoading: (0, vue_1.readonly)(isLoading),
45
- error: (0, vue_1.readonly)(error),
46
- ...pluginMethods,
47
- });
48
- },
54
+ const $a89e3697ca8ff325$export$838ead842aa548e7 = ()=>{
55
+ throw new Error("Must install Logto plugin first.");
56
+ };
57
+
58
+
59
+
60
+ const $155c9da87af3d4e0$export$6ad57c8ed6bec4 = (context)=>{
61
+ const { logtoClient: logtoClient , setLoading: setLoading , setError: setError , setIsAuthenticated: setIsAuthenticated } = context;
62
+ const signIn = async (redirectUri)=>{
63
+ if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
64
+ try {
65
+ setLoading(true);
66
+ await logtoClient.value.signIn(redirectUri);
67
+ } catch (error) {
68
+ setError(error, "Unexpected error occurred while signing in.");
69
+ } finally{
70
+ setLoading(false);
71
+ }
72
+ };
73
+ const signOut = async (postLogoutRedirectUri)=>{
74
+ if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
75
+ try {
76
+ setLoading(true);
77
+ await logtoClient.value.signOut(postLogoutRedirectUri);
78
+ // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
79
+ // even before navigating to the oidc end session endpoint, which might cause rendering problems.
80
+ // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
81
+ } catch (error) {
82
+ setError(error, "Unexpected error occurred while signing out.");
83
+ } finally{
84
+ setLoading(false);
85
+ }
86
+ };
87
+ const getAccessToken = async (resource)=>{
88
+ if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
89
+ try {
90
+ setLoading(true);
91
+ return await logtoClient.value.getAccessToken(resource);
92
+ } catch (error) {
93
+ setError(error, "Unexpected error occurred while getting access token.");
94
+ } finally{
95
+ setLoading(false);
96
+ }
97
+ };
98
+ const getIdTokenClaims = ()=>{
99
+ if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
100
+ try {
101
+ return logtoClient.value.getIdTokenClaims();
102
+ } catch (error) {
103
+ setError(error, "Unexpected error occurred while getting id token claims.");
104
+ }
105
+ };
106
+ const handleSignInCallback = async (callbackUri, callbackFunction)=>{
107
+ if (!logtoClient.value) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
108
+ try {
109
+ setLoading(true);
110
+ await logtoClient.value.handleSignInCallback(callbackUri);
111
+ setIsAuthenticated(true);
112
+ callbackFunction?.();
113
+ } catch (error) {
114
+ setError(error, "Unexpected error occurred while handling sign in callback.");
115
+ } finally{
116
+ setLoading(false);
117
+ }
118
+ };
119
+ return {
120
+ signIn: signIn,
121
+ signOut: signOut,
122
+ getAccessToken: getAccessToken,
123
+ getIdTokenClaims: getIdTokenClaims,
124
+ handleSignInCallback: handleSignInCallback
125
+ };
49
126
  };
50
- /**
51
- * A Vue composable method that provides the Logto reactive refs and auth methods.
52
- *
53
- * ```ts
54
- * import { useLogto } from '@logto/vue';
55
- *
56
- * export default {
57
- * setup() {
58
- * const { isAuthenticated, signIn } = useLogto();
59
- *
60
- * return {
61
- * isAuthenticated,
62
- * onClickSignIn: () => {
63
- * signIn('<your-redirect-uri>');
64
- * },
65
- * }
66
- * }
67
- * }
68
- * ```
69
- *
70
- * Use this composable in the setup script of your Vue component to make sure the injection works
71
- */
72
- const useLogto = () => {
73
- const logto = (0, vue_1.inject)(consts_1.logtoInjectionKey);
74
- if (!logto) {
75
- return (0, context_1.throwContextError)();
127
+
128
+
129
+
130
+ const $b8cbe35fca4d52f8$export$d8f887f9089028d2 = {
131
+ install (app, config) {
132
+ const client = new (0, ($parcel$interopDefault($Kru5Y$logtobrowser)))(config);
133
+ const context = (0, $a89e3697ca8ff325$export$fd42f52fd3ae1109)(client);
134
+ const pluginMethods = (0, $155c9da87af3d4e0$export$6ad57c8ed6bec4)(context);
135
+ const { isAuthenticated: isAuthenticated , isLoading: isLoading , error: error } = context;
136
+ app.provide((0, $bf8f9fe4f6be54e2$export$951b969b1220de04), context);
137
+ app.provide((0, $bf8f9fe4f6be54e2$export$58f3af87e7a1a85a), {
138
+ isAuthenticated: (0, $Kru5Y$vue.readonly)(isAuthenticated),
139
+ isLoading: (0, $Kru5Y$vue.readonly)(isLoading),
140
+ error: (0, $Kru5Y$vue.readonly)(error),
141
+ ...pluginMethods
142
+ });
76
143
  }
144
+ };
145
+ const $b8cbe35fca4d52f8$export$44fc9df4d2a1789a = ()=>{
146
+ const logto = (0, $Kru5Y$vue.inject)((0, $bf8f9fe4f6be54e2$export$58f3af87e7a1a85a));
147
+ if (!logto) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
77
148
  return logto;
78
149
  };
79
- exports.useLogto = useLogto;
80
- /**
81
- * A Vue composable method that watches browser navigation and automatically handles the sign-in callback
82
- *
83
- * ```ts
84
- * import { useLogto } from '@logto/vue';
85
- * import { useHandleSignInCallback } from '@logto/vue';
86
- *
87
- * export default {
88
- * setup() {
89
- * useHandleSignInCallback();
90
- * }
91
- * }
92
- * ```
93
- *
94
- * Use this in the setup script of your Callback page to make sure the injection works
95
- */
96
- const useHandleSignInCallback = (callback) => {
97
- const context = (0, vue_1.inject)(consts_1.contextInjectionKey);
98
- if (!context) {
99
- return (0, context_1.throwContextError)();
100
- }
101
- const { isAuthenticated, isLoading, logtoClient, error } = context;
102
- const { handleSignInCallback } = (0, plugin_1.createPluginMethods)(context);
103
- (0, vue_1.watchEffect)(() => {
150
+ const $b8cbe35fca4d52f8$export$84e88c4b3c082374 = (callback)=>{
151
+ const context = (0, $Kru5Y$vue.inject)((0, $bf8f9fe4f6be54e2$export$951b969b1220de04));
152
+ if (!context) return (0, $a89e3697ca8ff325$export$838ead842aa548e7)();
153
+ const { isAuthenticated: isAuthenticated , isLoading: isLoading , logtoClient: logtoClient , error: error } = context;
154
+ const { handleSignInCallback: handleSignInCallback } = (0, $155c9da87af3d4e0$export$6ad57c8ed6bec4)(context);
155
+ (0, $Kru5Y$vue.watchEffect)(()=>{
104
156
  const currentPageUrl = window.location.href;
105
- if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {
106
- void handleSignInCallback(currentPageUrl, callback);
107
- }
157
+ if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) handleSignInCallback(currentPageUrl, callback);
108
158
  });
109
159
  return {
110
- isLoading: (0, vue_1.readonly)(isLoading),
111
- isAuthenticated: (0, vue_1.readonly)(isAuthenticated),
112
- error: (0, vue_1.readonly)(error),
160
+ isLoading: (0, $Kru5Y$vue.readonly)(isLoading),
161
+ isAuthenticated: (0, $Kru5Y$vue.readonly)(isAuthenticated),
162
+ error: (0, $Kru5Y$vue.readonly)(error)
113
163
  };
114
164
  };
115
- exports.useHandleSignInCallback = useHandleSignInCallback;
165
+
166
+
167
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,iBAAM,CAAA,CACpB,CAAA,GAAA,mBAAQ,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,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,YAAY,CAAC,KAAK,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,OAAO,KAAK,CAAC;CACd,AAAC;AAkBK,MAAM,yCAAuB,GAAG,CAAC,QAAqB,GAAK;IAChE,MAAM,OAAO,GAAG,CAAA,GAAA,iBAAM,CAAA,CAAU,CAAA,GAAA,yCAAmB,CAAA,CAAC,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"}
package/lib/module.js ADDED
@@ -0,0 +1,154 @@
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";
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
+
4
+
5
+
6
+ const $cda0d5851de767d1$export$58f3af87e7a1a85a = "@logto/vue";
7
+ const $cda0d5851de767d1$export$951b969b1220de04 = "@logto/vue:context";
8
+
9
+
10
+
11
+ const $f63beecb5f2c2ea1$export$fd42f52fd3ae1109 = (client)=>{
12
+ const context = (0, $jwuiT$toRefs)((0, $jwuiT$reactive)({
13
+ logtoClient: client,
14
+ isAuthenticated: client.isAuthenticated,
15
+ loadingCount: 0,
16
+ error: undefined
17
+ }));
18
+ const { isAuthenticated: isAuthenticated , loadingCount: loadingCount , error: error } = context;
19
+ const isLoading1 = (0, $jwuiT$computed)(()=>loadingCount.value > 0);
20
+ /* eslint-disable @silverhand/fp/no-mutation */ const setError = (_error, fallbackErrorMessage)=>{
21
+ if (_error instanceof Error) error.value = _error;
22
+ else if (fallbackErrorMessage) error.value = new Error(fallbackErrorMessage);
23
+ console.error(error);
24
+ };
25
+ const setLoading = (isLoading)=>{
26
+ if (isLoading) loadingCount.value += 1;
27
+ else loadingCount.value = Math.max(0, loadingCount.value - 1);
28
+ };
29
+ const setIsAuthenticated = (_isAuthenticated)=>{
30
+ isAuthenticated.value = _isAuthenticated;
31
+ };
32
+ /* eslint-enable @silverhand/fp/no-mutation */ return {
33
+ ...context,
34
+ isLoading: isLoading1,
35
+ setError: setError,
36
+ setLoading: setLoading,
37
+ setIsAuthenticated: setIsAuthenticated
38
+ };
39
+ };
40
+ const $f63beecb5f2c2ea1$export$838ead842aa548e7 = ()=>{
41
+ throw new Error("Must install Logto plugin first.");
42
+ };
43
+
44
+
45
+
46
+ const $eec30d751f04969b$export$6ad57c8ed6bec4 = (context)=>{
47
+ const { logtoClient: logtoClient , setLoading: setLoading , setError: setError , setIsAuthenticated: setIsAuthenticated } = context;
48
+ const signIn = async (redirectUri)=>{
49
+ if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
50
+ try {
51
+ setLoading(true);
52
+ await logtoClient.value.signIn(redirectUri);
53
+ } catch (error) {
54
+ setError(error, "Unexpected error occurred while signing in.");
55
+ } finally{
56
+ setLoading(false);
57
+ }
58
+ };
59
+ const signOut = async (postLogoutRedirectUri)=>{
60
+ if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
61
+ try {
62
+ setLoading(true);
63
+ await logtoClient.value.signOut(postLogoutRedirectUri);
64
+ // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
65
+ // even before navigating to the oidc end session endpoint, which might cause rendering problems.
66
+ // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
67
+ } catch (error) {
68
+ setError(error, "Unexpected error occurred while signing out.");
69
+ } finally{
70
+ setLoading(false);
71
+ }
72
+ };
73
+ const getAccessToken = async (resource)=>{
74
+ if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
75
+ try {
76
+ setLoading(true);
77
+ return await logtoClient.value.getAccessToken(resource);
78
+ } catch (error) {
79
+ setError(error, "Unexpected error occurred while getting access token.");
80
+ } finally{
81
+ setLoading(false);
82
+ }
83
+ };
84
+ const getIdTokenClaims = ()=>{
85
+ if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
86
+ try {
87
+ return logtoClient.value.getIdTokenClaims();
88
+ } catch (error) {
89
+ setError(error, "Unexpected error occurred while getting id token claims.");
90
+ }
91
+ };
92
+ const handleSignInCallback = async (callbackUri, callbackFunction)=>{
93
+ if (!logtoClient.value) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
94
+ try {
95
+ setLoading(true);
96
+ await logtoClient.value.handleSignInCallback(callbackUri);
97
+ setIsAuthenticated(true);
98
+ callbackFunction?.();
99
+ } catch (error) {
100
+ setError(error, "Unexpected error occurred while handling sign in callback.");
101
+ } finally{
102
+ setLoading(false);
103
+ }
104
+ };
105
+ return {
106
+ signIn: signIn,
107
+ signOut: signOut,
108
+ getAccessToken: getAccessToken,
109
+ getIdTokenClaims: getIdTokenClaims,
110
+ handleSignInCallback: handleSignInCallback
111
+ };
112
+ };
113
+
114
+
115
+
116
+ const $a52d863b14a9eb8e$export$d8f887f9089028d2 = {
117
+ install (app, config) {
118
+ const client = new (0, $jwuiT$logtobrowser)(config);
119
+ const context = (0, $f63beecb5f2c2ea1$export$fd42f52fd3ae1109)(client);
120
+ const pluginMethods = (0, $eec30d751f04969b$export$6ad57c8ed6bec4)(context);
121
+ const { isAuthenticated: isAuthenticated , isLoading: isLoading , error: error } = context;
122
+ app.provide((0, $cda0d5851de767d1$export$951b969b1220de04), context);
123
+ app.provide((0, $cda0d5851de767d1$export$58f3af87e7a1a85a), {
124
+ isAuthenticated: (0, $jwuiT$readonly)(isAuthenticated),
125
+ isLoading: (0, $jwuiT$readonly)(isLoading),
126
+ error: (0, $jwuiT$readonly)(error),
127
+ ...pluginMethods
128
+ });
129
+ }
130
+ };
131
+ const $a52d863b14a9eb8e$export$44fc9df4d2a1789a = ()=>{
132
+ const logto = (0, $jwuiT$inject)((0, $cda0d5851de767d1$export$58f3af87e7a1a85a));
133
+ if (!logto) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
134
+ return logto;
135
+ };
136
+ const $a52d863b14a9eb8e$export$84e88c4b3c082374 = (callback)=>{
137
+ const context = (0, $jwuiT$inject)((0, $cda0d5851de767d1$export$951b969b1220de04));
138
+ if (!context) return (0, $f63beecb5f2c2ea1$export$838ead842aa548e7)();
139
+ const { isAuthenticated: isAuthenticated , isLoading: isLoading , logtoClient: logtoClient , error: error } = context;
140
+ const { handleSignInCallback: handleSignInCallback } = (0, $eec30d751f04969b$export$6ad57c8ed6bec4)(context);
141
+ (0, $jwuiT$watchEffect)(()=>{
142
+ const currentPageUrl = window.location.href;
143
+ if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) handleSignInCallback(currentPageUrl, callback);
144
+ });
145
+ return {
146
+ isLoading: (0, $jwuiT$readonly)(isLoading),
147
+ isAuthenticated: (0, $jwuiT$readonly)(isAuthenticated),
148
+ error: (0, $jwuiT$readonly)(error)
149
+ };
150
+ };
151
+
152
+
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};
154
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;AAAA;;ACAO,MAAM,yCAAiB,GAAG,YAAY,AAAC;AACvC,MAAM,yCAAmB,GAAG,oBAAoB,AAAC;;;ACDxD;AAsBO,MAAM,yCAAa,GAAG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AAoB,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 } 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"}
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@logto/vue",
3
- "version": "0.2.2",
3
+ "version": "0.2.3-alpha.0",
4
+ "source": "./src/index.ts",
4
5
  "main": "./lib/index.js",
5
- "exports": "./lib/index.js",
6
- "typings": "./lib/index.d.ts",
6
+ "exports": {
7
+ "require": "./lib/index.js",
8
+ "import": "./lib/module.js"
9
+ },
10
+ "module": "./lib/module.js",
11
+ "types": "./lib/index.d.ts",
7
12
  "files": [
8
13
  "lib"
9
14
  ],
@@ -16,23 +21,28 @@
16
21
  "scripts": {
17
22
  "dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
18
23
  "precommit": "lint-staged",
19
- "build": "rm -rf lib/ && tsc -p tsconfig.build.json",
24
+ "check": "tsc --noEmit",
25
+ "build": "rm -rf lib/ && pnpm check && parcel build",
20
26
  "lint": "eslint --ext .ts src",
21
27
  "test": "jest",
22
28
  "test:coverage": "jest --silent --coverage",
23
29
  "prepack": "pnpm test"
24
30
  },
25
31
  "dependencies": {
26
- "@logto/browser": "^0.2.2"
32
+ "@logto/browser": "^0.2.3-alpha.0"
27
33
  },
28
34
  "devDependencies": {
29
35
  "@jest/types": "^27.5.1",
36
+ "@parcel/core": "^2.6.2",
37
+ "@parcel/packager-ts": "^2.6.2",
38
+ "@parcel/transformer-typescript-types": "^2.6.2",
30
39
  "@silverhand/eslint-config": "^0.14.0",
31
40
  "@silverhand/ts-config": "^0.14.0",
32
41
  "@types/jest": "^27.4.1",
33
42
  "eslint": "^8.9.0",
34
43
  "jest": "^27.5.1",
35
44
  "lint-staged": "^13.0.0",
45
+ "parcel": "^2.6.2",
36
46
  "postcss": "^8.4.6",
37
47
  "prettier": "^2.5.1",
38
48
  "stylelint": "^14.8.2",
@@ -60,5 +70,5 @@
60
70
  "publishConfig": {
61
71
  "access": "public"
62
72
  },
63
- "gitHead": "c8e2ad8ea656887884f9d32c119086d53554c178"
73
+ "gitHead": "e9e611773b0e73baa6c05a3f2d545fac64d03707"
64
74
  }
package/lib/consts.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare const logtoInjectionKey = "@logto/vue";
2
- export declare const contextInjectionKey = "@logto/vue:context";
package/lib/consts.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.contextInjectionKey = exports.logtoInjectionKey = void 0;
4
- exports.logtoInjectionKey = '@logto/vue';
5
- exports.contextInjectionKey = '@logto/vue:context';
package/lib/context.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import LogtoClient from '@logto/browser';
2
- import { ComputedRef, Ref, UnwrapRef } from 'vue';
3
- export declare type Context = {
4
- logtoClient: Ref<UnwrapRef<LogtoClient | undefined>>;
5
- isAuthenticated: Ref<boolean>;
6
- loadingCount: Ref<number>;
7
- error: Ref<Error | undefined>;
8
- isLoading: ComputedRef<boolean>;
9
- setError: (error: unknown, fallbackErrorMessage?: string | undefined) => void;
10
- setIsAuthenticated: (isAuthenticated: boolean) => void;
11
- setLoading: (isLoading: boolean) => void;
12
- };
13
- export declare const createContext: (client: LogtoClient) => Context;
14
- export declare const throwContextError: () => never;
package/lib/context.js DELETED
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.throwContextError = exports.createContext = void 0;
4
- const vue_1 = require("vue");
5
- const createContext = (client) => {
6
- const context = (0, vue_1.toRefs)((0, vue_1.reactive)({
7
- logtoClient: client,
8
- isAuthenticated: client.isAuthenticated,
9
- loadingCount: 0,
10
- error: undefined,
11
- }));
12
- const { isAuthenticated, loadingCount, error } = context;
13
- const isLoading = (0, vue_1.computed)(() => loadingCount.value > 0);
14
- /* eslint-disable @silverhand/fp/no-mutation */
15
- const setError = (_error, fallbackErrorMessage) => {
16
- if (_error instanceof Error) {
17
- error.value = _error;
18
- }
19
- else if (fallbackErrorMessage) {
20
- error.value = new Error(fallbackErrorMessage);
21
- }
22
- console.error(error);
23
- };
24
- const setLoading = (isLoading) => {
25
- if (isLoading) {
26
- loadingCount.value += 1;
27
- }
28
- else {
29
- loadingCount.value = Math.max(0, loadingCount.value - 1);
30
- }
31
- };
32
- const setIsAuthenticated = (_isAuthenticated) => {
33
- isAuthenticated.value = _isAuthenticated;
34
- };
35
- /* eslint-enable @silverhand/fp/no-mutation */
36
- return { ...context, isLoading, setError, setLoading, setIsAuthenticated };
37
- };
38
- exports.createContext = createContext;
39
- const throwContextError = () => {
40
- throw new Error('Must install Logto plugin first.');
41
- };
42
- exports.throwContextError = throwContextError;
package/lib/plugin.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import { Context } from './context';
2
- export declare const createPluginMethods: (context: Context) => {
3
- signIn: (redirectUri: string) => Promise<undefined>;
4
- signOut: (postLogoutRedirectUri: string) => Promise<undefined>;
5
- getAccessToken: (resource?: string | undefined) => Promise<string | undefined>;
6
- getIdTokenClaims: () => {
7
- iss: string;
8
- sub: string;
9
- aud: string;
10
- exp: number;
11
- iat: number;
12
- at_hash?: string | null | undefined;
13
- name?: string | null | undefined;
14
- username?: string | null | undefined;
15
- avatar?: string | null | undefined;
16
- role_names?: string[] | null | undefined;
17
- } | undefined;
18
- handleSignInCallback: (callbackUri: string, callbackFunction?: (() => void) | undefined) => Promise<undefined>;
19
- };
package/lib/plugin.js DELETED
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createPluginMethods = void 0;
4
- const context_1 = require("./context");
5
- const createPluginMethods = (context) => {
6
- const { logtoClient, setLoading, setError, setIsAuthenticated } = context;
7
- const signIn = async (redirectUri) => {
8
- if (!logtoClient.value) {
9
- return (0, context_1.throwContextError)();
10
- }
11
- try {
12
- setLoading(true);
13
- await logtoClient.value.signIn(redirectUri);
14
- }
15
- catch (error) {
16
- setError(error, 'Unexpected error occurred while signing in.');
17
- }
18
- finally {
19
- setLoading(false);
20
- }
21
- };
22
- const signOut = async (postLogoutRedirectUri) => {
23
- if (!logtoClient.value) {
24
- return (0, context_1.throwContextError)();
25
- }
26
- try {
27
- setLoading(true);
28
- await logtoClient.value.signOut(postLogoutRedirectUri);
29
- // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
30
- // even before navigating to the oidc end session endpoint, which might cause rendering problems.
31
- // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
32
- }
33
- catch (error) {
34
- setError(error, 'Unexpected error occurred while signing out.');
35
- }
36
- finally {
37
- setLoading(false);
38
- }
39
- };
40
- const getAccessToken = async (resource) => {
41
- if (!logtoClient.value) {
42
- return (0, context_1.throwContextError)();
43
- }
44
- try {
45
- setLoading(true);
46
- return await logtoClient.value.getAccessToken(resource);
47
- }
48
- catch (error) {
49
- setError(error, 'Unexpected error occurred while getting access token.');
50
- }
51
- finally {
52
- setLoading(false);
53
- }
54
- };
55
- const getIdTokenClaims = () => {
56
- if (!logtoClient.value) {
57
- return (0, context_1.throwContextError)();
58
- }
59
- try {
60
- return logtoClient.value.getIdTokenClaims();
61
- }
62
- catch (error) {
63
- setError(error, 'Unexpected error occurred while getting id token claims.');
64
- }
65
- };
66
- const handleSignInCallback = async (callbackUri, callbackFunction) => {
67
- if (!logtoClient.value) {
68
- return (0, context_1.throwContextError)();
69
- }
70
- try {
71
- setLoading(true);
72
- await logtoClient.value.handleSignInCallback(callbackUri);
73
- setIsAuthenticated(true);
74
- callbackFunction?.();
75
- }
76
- catch (error) {
77
- setError(error, 'Unexpected error occurred while handling sign in callback.');
78
- }
79
- finally {
80
- setLoading(false);
81
- }
82
- };
83
- return {
84
- signIn,
85
- signOut,
86
- getAccessToken,
87
- getIdTokenClaims,
88
- handleSignInCallback,
89
- };
90
- };
91
- exports.createPluginMethods = createPluginMethods;