@logto/vue 1.0.0-rc.0 → 1.0.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.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/module.mjs.map +1 -1
- package/package.json +6 -6
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;
|
|
1
|
+
{"mappings":";;AGUA,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 type { IdTokenClaims, LogtoConfig, UserInfoResponse } from '@logto/browser';\nimport LogtoClient from '@logto/browser';\nimport type { App, Ref } from 'vue';\nimport { inject, readonly, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport type { Context } from './context';\nimport { 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"}
|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AK,EAAE,GAAG;IAC3D,MAAM,wBAAE,qBAAoB,EAAE,GAAG,CAAA,GAAA,uCAAmB,AAAD,EAAE;IAErD,CAAA,GAAA,sBAAW,AAAD,EAAE,IAAM;QAChB,MAAM,iBAAiB,OAAO,QAAQ,CAAC,IAAI;QAE3C,IAAI,CAAC,gBAAgB,KAAK,IAAI,YAAY,KAAK,EAAE,mBAAmB,iBAC7D,qBAAqB,gBAAgB;IAE9C;IAEA,OAAO;QACL,WAAW,CAAA,GAAA,mBAAO,EAAE;QACpB,iBAAiB,CAAA,GAAA,mBAAO,EAAE;QAC1B,OAAO,CAAA,GAAA,mBAAO,EAAE;IAClB;AACF","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"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;AAAA;;ACAO,MAAM,4CAAoB;AAC1B,MAAM,4CAAsB;;;ACDnC;AAuBO,MAAM,4CAAgB,CAAC,SAAiC;IAC7D,MAAM,UAAU,CAAA,GAAA,iBAAK,EACnB,CAAA,GAAA,mBAAQ,AAAD,EAA0B;QAC/B,aAAa;QACb,iBAAiB,KAAK;QACtB,cAAc;QACd,OAAO;IACT;IAGF,MAAM,mBAAE,gBAAe,gBAAE,aAAY,SAAE,MAAK,EAAE,GAAG;IAEjD,MAAM,YAAY,CAAA,GAAA,mBAAO,EAAE,IAAM,aAAa,KAAK,GAAG;IAEtD,6CAA6C,GAC7C,MAAM,WAAW,CAAC,QAAiB,uBAAkC;QACnE,IAAI,kBAAkB,OACpB,MAAM,KAAK,GAAG;aACT,IAAI,sBACT,MAAM,KAAK,GAAG,IAAI,MAAM;QAE1B,QAAQ,KAAK,CAAC;IAChB;IAEA,MAAM,aAAa,CAAC,YAAuB;QACzC,IAAI,WACF,aAAa,KAAK,IAAI;aAEtB,aAAa,KAAK,GAAG,KAAK,GAAG,CAAC,GAAG,aAAa,KAAK,GAAG;IAE1D;IAEA,MAAM,qBAAqB,CAAC,mBAA8B;QACxD,gBAAgB,KAAK,GAAG;IAC1B;IACA,4CAA4C,GAE5C,CAAA,GAAA,sBAAU,EAAE,UAAY;QACtB,MAAM,kBAAkB,MAAM,OAAO,eAAe;QAEpD,mBAAmB;QACnB,WAAW,KAAK;IAClB;IAEA,OAAO;QAAE,GAAG,OAAO;mBAAE;kBAAW;oBAAU;4BAAY;IAAmB;AAC3E;AAEO,MAAM,4CAAoB,I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M,UAAU,CAAA,GAAA,iBAAK,EAAW,CAAA,GAAA,yCAAkB;IAElD,IAAI,CAAC,SACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;IAGzB,MAAM,mBAAE,gBAAe,aAAE,UAAS,eAAE,YAAW,SAAE,MAAK,EAAE,GAAG;IAC3D,MAAM,wBAAE,qBAAoB,EAAE,GAAG,CAAA,GAAA,uCAAmB,AAAD,EAAE;IAErD,CAAA,GAAA,sBAAW,AAAD,EAAE,IAAM;QAChB,MAAM,iBAAiB,OAAO,QAAQ,CAAC,IAAI;QAE3C,IAAI,CAAC,gBAAgB,KAAK,IAAI,YAAY,KAAK,EAAE,mBAAmB,iBAC7D,qBAAqB,gBAAgB;IAE9C;IAEA,OAAO;QACL,WAAW,CAAA,GAAA,mBAAO,EAAE;QACpB,iBAAiB,CAAA,GAAA,mBAAO,EAAE;QAC1B,OAAO,CAAA,GAAA,mBAAO,EAAE;IAClB;AACF","sources":["packages/vue/src/index.ts","packages/vue/src/consts.ts","packages/vue/src/context.ts","packages/vue/src/plugin.ts"],"sourcesContent":["import type { IdTokenClaims, LogtoConfig, UserInfoResponse } from '@logto/browser';\nimport LogtoClient from '@logto/browser';\nimport type { App, Ref } from 'vue';\nimport { inject, readonly, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport type { Context } from './context';\nimport { 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 type LogtoClient from '@logto/browser';\nimport type { ComputedRef, Ref, UnwrapRef } from 'vue';\nimport { computed, reactive, toRefs, 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 type { Context } from './context';\nimport { 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.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;ACAO,MAAM,4CAAoB;AAC1B,MAAM,4CAAsB;;;ACDnC;AAsBO,MAAM,4CAAgB,CAAC,SAAiC;IAC7D,MAAM,UAAU,CAAA,GAAA,aAAK,EACnB,CAAA,GAAA,eAAQ,AAAD,EAA0B;QAC/B,aAAa;QACb,iBAAiB,KAAK;QACtB,cAAc;QACd,OAAO;IACT;IAGF,MAAM,mBAAE,gBAAe,gBAAE,aAAY,SAAE,MAAK,EAAE,GAAG;IAEjD,MAAM,YAAY,CAAA,GAAA,eAAO,EAAE,IAAM,aAAa,KAAK,GAAG;IAEtD,6CAA6C,GAC7C,MAAM,WAAW,CAAC,QAAiB,uBAAkC;QACnE,IAAI,kBAAkB,OACpB,MAAM,KAAK,GAAG;aACT,IAAI,sBACT,MAAM,KAAK,GAAG,IAAI,MAAM;QAE1B,QAAQ,KAAK,CAAC;IAChB;IAEA,MAAM,aAAa,CAAC,YAAuB;QACzC,IAAI,WACF,aAAa,KAAK,IAAI;aAEtB,aAAa,KAAK,GAAG,KAAK,GAAG,CAAC,GAAG,aAAa,KAAK,GAAG;IAE1D;IAEA,MAAM,qBAAqB,CAAC,mBAA8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,eAAO,EAAE;YAChB,GAAG,aAAa;QAClB;IACF;AACF;AAwBO,MAAM,4CAAW,IAAa;IACnC,MAAM,QAAQ,CAAA,GAAA,aAAK,EAAS,CAAA,GAAA,yCAAgB;IAE5C,IAAI,CAAC,OACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;IAGzB,OAAO;AACT;AAkBO,MAAM,4CAA0B,CAAC,WAA0B;IAChE,MAAM,UAAU,CAAA,GAAA,aAAK,EAAW,CAAA,GAAA,yCAAkB;IAElD,IAAI,CAAC,SACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;IAGzB,MAAM,mBAAE,gBAAe,aAAE,UAAS,eAAE,YAAW,SAAE,MAAK,EAAE,GAAG;IAC3D,MAAM,wBAAE,qBAAoB,EAAE,GAAG,CAAA,GAAA,uCAAmB,AAAD,EAAE;IAErD,CAAA,GAAA,kBAAW,AAAD,EAAE,IAAM;QAChB,MAAM,iBAAiB,OAAO,QAAQ,CAAC,IAAI;QAE3C,IAAI,CAAC,gBAAgB,KAAK,IAAI,YAAY,KAAK,EAAE,mBAAmB,iBAC7D,qBAAqB,gBAAgB;IAE9C;IAEA,OAAO;QACL,WAAW,CAAA,GAAA,eAAO,EAAE;QACpB,iBAAiB,CAAA,GAAA,eAAO,EAAE;QAC1B,OAAO,CAAA,GAAA,eAAO,EAAE;IAClB;AACF","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.mjs.map"}
|
|
1
|
+
{"mappings":";;;AAAA;;ACAO,MAAM,4CAAoB;AAC1B,MAAM,4CAAsB;;;ACDnC;AAuBO,MAAM,4CAAgB,CAAC,SAAiC;IAC7D,MAAM,UAAU,CAAA,GAAA,aAAK,EACnB,CAAA,GAAA,eAAQ,AAAD,EAA0B;QAC/B,aAAa;QACb,iBAAiB,KAAK;QACtB,cAAc;QACd,OAAO;IACT;IAGF,MAAM,mBAAE,gBAAe,gBAAE,aAAY,SAAE,MAAK,EAAE,GAAG;IAEjD,MAAM,YAAY,CAAA,GAAA,eAAO,EAAE,IAAM,aAAa,KAAK,GAAG;IAEtD,6CAA6C,GAC7C,MAAM,WAAW,CAAC,QAAiB,uBAAkC;QACnE,IAAI,kBAAkB,OACpB,MAAM,KAAK,GAAG;aACT,IAAI,sBACT,MAAM,KAAK,GAAG,IAAI,MAAM;QAE1B,QAAQ,KAAK,CAAC;IAChB;IAEA,MAAM,aAAa,CAAC,YAAuB;QAC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,OAAO,CAAQ,CAAA,GAAA,yCAAgB,GAAG;YACpC,iBAAiB,CAAA,GAAA,eAAO,EAAE;YAC1B,WAAW,CAAA,GAAA,eAAO,EAAE;YACpB,OAAO,CAAA,GAAA,eAAO,EAAE;YAChB,GAAG,aAAa;QAClB;IACF;AACF;AAwBO,MAAM,4CAAW,IAAa;IACnC,MAAM,QAAQ,CAAA,GAAA,aAAK,EAAS,CAAA,GAAA,yCAAgB;IAE5C,IAAI,CAAC,OACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;IAGzB,OAAO;AACT;AAkBO,MAAM,4CAA0B,CAAC,WAA0B;IAChE,MAAM,UAAU,CAAA,GAAA,aAAK,EAAW,CAAA,GAAA,yCAAkB;IAElD,IAAI,CAAC,SACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;IAGzB,MAAM,mBAAE,gBAAe,aAAE,UAAS,eAAE,YAAW,SAAE,MAAK,EAAE,GAAG;IAC3D,MAAM,wBAAE,qBAAoB,EAAE,GAAG,CAAA,GAAA,uCAAmB,AAAD,EAAE;IAErD,CAAA,GAAA,kBAAW,AAAD,EAAE,IAAM;QAChB,MAAM,iBAAiB,OAAO,QAAQ,CAAC,IAAI;QAE3C,IAAI,CAAC,gBAAgB,KAAK,IAAI,YAAY,KAAK,EAAE,mBAAmB,iBAC7D,qBAAqB,gBAAgB;IAE9C;IAEA,OAAO;QACL,WAAW,CAAA,GAAA,eAAO,EAAE;QACpB,iBAAiB,CAAA,GAAA,eAAO,EAAE;QAC1B,OAAO,CAAA,GAAA,eAAO,EAAE;IAClB;AACF","sources":["packages/vue/src/index.ts","packages/vue/src/consts.ts","packages/vue/src/context.ts","packages/vue/src/plugin.ts"],"sourcesContent":["import type { IdTokenClaims, LogtoConfig, UserInfoResponse } from '@logto/browser';\nimport LogtoClient from '@logto/browser';\nimport type { App, Ref } from 'vue';\nimport { inject, readonly, watchEffect } from 'vue';\n\nimport { logtoInjectionKey, contextInjectionKey } from './consts';\nimport type { Context } from './context';\nimport { 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 type LogtoClient from '@logto/browser';\nimport type { ComputedRef, Ref, UnwrapRef } from 'vue';\nimport { computed, reactive, toRefs, 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 type { Context } from './context';\nimport { 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.mjs.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/vue",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -29,14 +29,14 @@
|
|
|
29
29
|
"prepack": "pnpm test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@logto/browser": "^1.0.0
|
|
32
|
+
"@logto/browser": "^1.0.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@jest/types": "^27.5.1",
|
|
36
36
|
"@parcel/core": "^2.8.3",
|
|
37
37
|
"@parcel/packager-ts": "^2.8.3",
|
|
38
38
|
"@parcel/transformer-typescript-types": "^2.8.3",
|
|
39
|
-
"@silverhand/eslint-config": "^
|
|
39
|
+
"@silverhand/eslint-config": "^2.0.0",
|
|
40
40
|
"@silverhand/ts-config": "^1.0.0",
|
|
41
41
|
"@types/jest": "^27.4.1",
|
|
42
42
|
"eslint": "^8.23.0",
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
"parcel": "^2.8.3",
|
|
46
46
|
"postcss": "^8.4.6",
|
|
47
47
|
"prettier": "^2.7.1",
|
|
48
|
-
"stylelint": "^
|
|
48
|
+
"stylelint": "^15.0.0",
|
|
49
49
|
"ts-jest": "^27.0.4",
|
|
50
|
-
"typescript": "4.
|
|
50
|
+
"typescript": "4.9.5",
|
|
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": "fff27b23a74d5bd3a46fc83fbbc2901b6e054c72"
|
|
74
74
|
}
|