@logto/nuxt 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Silverhand
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Logto Nuxt 3 SDK
2
+
3
+ [![Version](https://img.shields.io/npm/v/@logto/nuxt)](https://www.npmjs.com/package/@logto/nuxt)
4
+ [![Build Status](https://github.com/logto-io/js/actions/workflows/main.yml/badge.svg)](https://github.com/logto-io/js/actions/workflows/main.yml)
5
+
6
+ The Logto Nuxt 3 SDK written in TypeScript.
7
+
8
+ Check out our [docs](https://docs.logto.io/sdk/nuxt/) for more information.
9
+
10
+ ## Installation
11
+
12
+ ### Using npm
13
+
14
+ ```bash
15
+ npm install @logto/nuxt
16
+ ```
17
+
18
+ ### Using yarn
19
+
20
+ ```bash
21
+ yarn add @logto/nuxt
22
+ ```
23
+
24
+ ### Using pnpm
25
+
26
+ ```bash
27
+ pnpm add @logto/nuxt
28
+ ```
29
+
30
+ ## Get sample
31
+
32
+ A sample project can be found at [playground](./playground/).
33
+
34
+ Check out the full JS repo and try it with pnpm.
35
+
36
+ ```bash
37
+ pnpm i && pnpm dev
38
+ ```
39
+
40
+ ## Resources
41
+
42
+ [![Website](https://img.shields.io/badge/website-logto.io-8262F8.svg)](https://logto.io/)
43
+ [![Docs](https://img.shields.io/badge/docs-logto.io-green.svg)](https://docs.logto.io/)
44
+ [![Discord](https://img.shields.io/discord/965845662535147551?logo=discord&logoColor=ffffff&color=7389D8&cacheSeconds=600)](https://discord.gg/UEPaF3j5e6)
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,38 @@
1
+ import { NuxtModule } from 'nuxt/schema';
2
+ import { LogtoConfig } from '@logto/node';
3
+ export * from '@logto/node';
4
+ export { default as LogtoNodeClient } from '@logto/node';
5
+
6
+ type DeepPartial<T> = T extends Record<string, unknown> ? {
7
+ [P in keyof T]?: DeepPartial<T[P]>;
8
+ } : T;
9
+ type LogtoModuleOptions = {
10
+ /**
11
+ * The name to use when storing the Logto cookie.
12
+ *
13
+ * @see {@link CookieConfig.cookieKey} for the default value.
14
+ */
15
+ cookieName?: string;
16
+ /**
17
+ * If Logto should fetch from the [userinfo endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo)
18
+ * when the user is signed in. This is useful when you need to fetch additional claims like `custom_data`.
19
+ */
20
+ fetchUserInfo: boolean;
21
+ postCallbackRedirectUri: string;
22
+ postLogoutRedirectUri: string;
23
+ pathnames: {
24
+ signIn: string;
25
+ signOut: string;
26
+ callback: string;
27
+ };
28
+ };
29
+ type LogtoRuntimeConfig = LogtoModuleOptions & {
30
+ cookieEncryptionKey: string;
31
+ } & Omit<LogtoConfig, 'appSecret'> & {
32
+ appSecret: NonNullable<LogtoConfig['appSecret']>;
33
+ };
34
+ type LogtoRuntimeConfigInput = DeepPartial<LogtoRuntimeConfig>;
35
+
36
+ declare const logtoModule: NuxtModule<LogtoRuntimeConfigInput>;
37
+
38
+ export { logtoModule as default };
@@ -0,0 +1,38 @@
1
+ import { NuxtModule } from 'nuxt/schema';
2
+ import { LogtoConfig } from '@logto/node';
3
+ export * from '@logto/node';
4
+ export { default as LogtoNodeClient } from '@logto/node';
5
+
6
+ type DeepPartial<T> = T extends Record<string, unknown> ? {
7
+ [P in keyof T]?: DeepPartial<T[P]>;
8
+ } : T;
9
+ type LogtoModuleOptions = {
10
+ /**
11
+ * The name to use when storing the Logto cookie.
12
+ *
13
+ * @see {@link CookieConfig.cookieKey} for the default value.
14
+ */
15
+ cookieName?: string;
16
+ /**
17
+ * If Logto should fetch from the [userinfo endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo)
18
+ * when the user is signed in. This is useful when you need to fetch additional claims like `custom_data`.
19
+ */
20
+ fetchUserInfo: boolean;
21
+ postCallbackRedirectUri: string;
22
+ postLogoutRedirectUri: string;
23
+ pathnames: {
24
+ signIn: string;
25
+ signOut: string;
26
+ callback: string;
27
+ };
28
+ };
29
+ type LogtoRuntimeConfig = LogtoModuleOptions & {
30
+ cookieEncryptionKey: string;
31
+ } & Omit<LogtoConfig, 'appSecret'> & {
32
+ appSecret: NonNullable<LogtoConfig['appSecret']>;
33
+ };
34
+ type LogtoRuntimeConfigInput = DeepPartial<LogtoRuntimeConfig>;
35
+
36
+ declare const logtoModule: NuxtModule<LogtoRuntimeConfigInput>;
37
+
38
+ export { logtoModule as default };
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "@logto/nuxt",
3
+ "configKey": "logto",
4
+ "version": "0.0.0"
5
+ }
@@ -0,0 +1,43 @@
1
+ import { defineNuxtModule, createResolver, addServerHandler, addImportsDir } from '@nuxt/kit';
2
+ import { defu } from 'defu';
3
+ export * from '@logto/node';
4
+ export { default as LogtoNodeClient } from '@logto/node';
5
+
6
+ const defaults = Object.freeze({
7
+ endpoint: "<replace-with-logto-endpoint>",
8
+ appId: "<replace-with-logto-app-id>",
9
+ appSecret: "<replace-with-logto-app-secret>",
10
+ cookieEncryptionKey: "<replace-with-random-string>"
11
+ });
12
+
13
+ const logtoModule = defineNuxtModule({
14
+ meta: {
15
+ name: "@logto/nuxt",
16
+ configKey: "logto"
17
+ },
18
+ defaults,
19
+ setup(options, nuxt) {
20
+ const runtimeConfig = defu(
21
+ nuxt.options.runtimeConfig.logto,
22
+ options,
23
+ {
24
+ fetchUserInfo: false,
25
+ postCallbackRedirectUri: "/",
26
+ postLogoutRedirectUri: "/",
27
+ pathnames: {
28
+ signIn: "/sign-in",
29
+ signOut: "/sign-out",
30
+ callback: "/callback"
31
+ }
32
+ }
33
+ );
34
+ nuxt.options.runtimeConfig.logto = runtimeConfig;
35
+ const { resolve } = createResolver(import.meta.url);
36
+ addServerHandler({
37
+ handler: resolve("./runtime/server/event-handler")
38
+ });
39
+ addImportsDir(resolve("./runtime/composables"));
40
+ }
41
+ });
42
+
43
+ export { logtoModule as default };
@@ -0,0 +1,2 @@
1
+ import LogtoClient from '@logto/node';
2
+ export default function useLogtoClient(): LogtoClient | undefined;
@@ -0,0 +1,7 @@
1
+ import LogtoClient from "@logto/node";
2
+ import { useNuxtApp } from "#app";
3
+ export default function useLogtoClient() {
4
+ const nuxtApp = useNuxtApp();
5
+ const client = nuxtApp.ssrContext?.event.context.logtoClient;
6
+ return client instanceof LogtoClient ? client : void 0;
7
+ }
@@ -0,0 +1 @@
1
+ export default function useLogtoUser(): any;
@@ -0,0 +1,10 @@
1
+ import { LogtoStateKey } from "../utils/constants.mjs";
2
+ import { shallowRef as shallowReference, useNuxtApp, useState } from "#imports";
3
+ export default function useLogtoUser() {
4
+ const nuxtApp = useNuxtApp();
5
+ const user = useState(
6
+ LogtoStateKey.User,
7
+ () => shallowReference(nuxtApp.ssrContext?.event.context.logtoUser)
8
+ );
9
+ return user.value;
10
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,57 @@
1
+ import LogtoClient, { CookieStorage } from "@logto/node";
2
+ import { defaults } from "../utils/constants.mjs";
3
+ export default defineEventHandler(async (event) => {
4
+ const config = useRuntimeConfig(event);
5
+ const logtoConfig = config.logto;
6
+ const {
7
+ cookieName,
8
+ cookieEncryptionKey,
9
+ fetchUserInfo,
10
+ pathnames,
11
+ postCallbackRedirectUri,
12
+ postLogoutRedirectUri,
13
+ ...clientConfig
14
+ } = logtoConfig;
15
+ const defaultValueKeys = Object.entries(defaults).filter(([key, value]) => logtoConfig[key] === value).map(([key]) => key);
16
+ if (defaultValueKeys.length > 0) {
17
+ throw new TypeError(
18
+ `The following Logto configuration keys have default values: ${defaultValueKeys.join(
19
+ ", "
20
+ )}. Please replace them with your own values.`
21
+ );
22
+ }
23
+ const url = getRequestURL(event);
24
+ const storage = new CookieStorage(
25
+ {
26
+ cookieKey: cookieName,
27
+ encryptionKey: cookieEncryptionKey,
28
+ getCookie: (name) => getCookie(event, name),
29
+ setCookie: (name, value, options) => {
30
+ setCookie(event, name, value, options);
31
+ }
32
+ },
33
+ { headers: event.headers, url: url.href }
34
+ );
35
+ await storage.init();
36
+ const logto = new LogtoClient(clientConfig, {
37
+ navigate: async (url2) => {
38
+ await sendRedirect(event, url2, 302);
39
+ },
40
+ storage
41
+ });
42
+ if (url.pathname === pathnames.signIn) {
43
+ await logto.signIn(new URL(pathnames.callback, url).href);
44
+ return;
45
+ }
46
+ if (url.pathname === pathnames.signOut) {
47
+ await logto.signOut(new URL(postLogoutRedirectUri, url).href);
48
+ return;
49
+ }
50
+ if (url.pathname === pathnames.callback) {
51
+ await logto.handleSignInCallback(url.href);
52
+ await sendRedirect(event, postCallbackRedirectUri, 302);
53
+ return;
54
+ }
55
+ event.context.logtoClient = logto;
56
+ event.context.logtoUser = await logto.isAuthenticated() ? await (fetchUserInfo ? logto.fetchUserInfo() : logto.getIdTokenClaims()) : void 0;
57
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server",
3
+ }
@@ -0,0 +1,9 @@
1
+ export declare enum LogtoStateKey {
2
+ User = "logto.user"
3
+ }
4
+ export declare const defaults: Readonly<{
5
+ readonly endpoint: "<replace-with-logto-endpoint>";
6
+ readonly appId: "<replace-with-logto-app-id>";
7
+ readonly appSecret: "<replace-with-logto-app-secret>";
8
+ readonly cookieEncryptionKey: "<replace-with-random-string>";
9
+ }>;
@@ -0,0 +1,10 @@
1
+ export var LogtoStateKey = /* @__PURE__ */ ((LogtoStateKey2) => {
2
+ LogtoStateKey2["User"] = "logto.user";
3
+ return LogtoStateKey2;
4
+ })(LogtoStateKey || {});
5
+ export const defaults = Object.freeze({
6
+ endpoint: "<replace-with-logto-endpoint>",
7
+ appId: "<replace-with-logto-app-id>",
8
+ appSecret: "<replace-with-logto-app-secret>",
9
+ cookieEncryptionKey: "<replace-with-random-string>"
10
+ });
@@ -0,0 +1,31 @@
1
+ import type { LogtoConfig } from '@logto/node';
2
+ export type DeepPartial<T> = T extends Record<string, unknown> ? {
3
+ [P in keyof T]?: DeepPartial<T[P]>;
4
+ } : T;
5
+ type LogtoModuleOptions = {
6
+ /**
7
+ * The name to use when storing the Logto cookie.
8
+ *
9
+ * @see {@link CookieConfig.cookieKey} for the default value.
10
+ */
11
+ cookieName?: string;
12
+ /**
13
+ * If Logto should fetch from the [userinfo endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo)
14
+ * when the user is signed in. This is useful when you need to fetch additional claims like `custom_data`.
15
+ */
16
+ fetchUserInfo: boolean;
17
+ postCallbackRedirectUri: string;
18
+ postLogoutRedirectUri: string;
19
+ pathnames: {
20
+ signIn: string;
21
+ signOut: string;
22
+ callback: string;
23
+ };
24
+ };
25
+ export type LogtoRuntimeConfig = LogtoModuleOptions & {
26
+ cookieEncryptionKey: string;
27
+ } & Omit<LogtoConfig, 'appSecret'> & {
28
+ appSecret: NonNullable<LogtoConfig['appSecret']>;
29
+ };
30
+ export type LogtoRuntimeConfigInput = DeepPartial<LogtoRuntimeConfig>;
31
+ export {};
File without changes
@@ -0,0 +1,25 @@
1
+
2
+ import type { ModuleOptions, ModuleHooks, RuntimeModuleHooks, ModuleRuntimeHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module.js'
3
+
4
+ declare module '#app' {
5
+ interface RuntimeNuxtHooks extends RuntimeModuleHooks, ModuleRuntimeHooks {}
6
+ }
7
+
8
+ declare module '@nuxt/schema' {
9
+ interface NuxtConfig { ['logto']?: Partial<ModuleOptions> }
10
+ interface NuxtOptions { ['logto']?: ModuleOptions }
11
+ interface NuxtHooks extends ModuleHooks {}
12
+ interface RuntimeConfig extends ModuleRuntimeConfig {}
13
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
14
+ }
15
+
16
+ declare module 'nuxt/schema' {
17
+ interface NuxtConfig { ['logto']?: Partial<ModuleOptions> }
18
+ interface NuxtOptions { ['logto']?: ModuleOptions }
19
+ interface NuxtHooks extends ModuleHooks {}
20
+ interface RuntimeConfig extends ModuleRuntimeConfig {}
21
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
22
+ }
23
+
24
+
25
+ export type { LogtoNodeClient } from './module.js'
@@ -0,0 +1,25 @@
1
+
2
+ import type { ModuleOptions, ModuleHooks, RuntimeModuleHooks, ModuleRuntimeHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module'
3
+
4
+ declare module '#app' {
5
+ interface RuntimeNuxtHooks extends RuntimeModuleHooks, ModuleRuntimeHooks {}
6
+ }
7
+
8
+ declare module '@nuxt/schema' {
9
+ interface NuxtConfig { ['logto']?: Partial<ModuleOptions> }
10
+ interface NuxtOptions { ['logto']?: ModuleOptions }
11
+ interface NuxtHooks extends ModuleHooks {}
12
+ interface RuntimeConfig extends ModuleRuntimeConfig {}
13
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
14
+ }
15
+
16
+ declare module 'nuxt/schema' {
17
+ interface NuxtConfig { ['logto']?: Partial<ModuleOptions> }
18
+ interface NuxtOptions { ['logto']?: ModuleOptions }
19
+ interface NuxtHooks extends ModuleHooks {}
20
+ interface RuntimeConfig extends ModuleRuntimeConfig {}
21
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
22
+ }
23
+
24
+
25
+ export type { LogtoNodeClient } from './module'
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@logto/nuxt",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "import": "./dist/module.mjs",
8
+ "types": "./dist/module.d.ts",
9
+ "require": "./dist/module.cjs"
10
+ }
11
+ },
12
+ "main": "./dist/module.cjs",
13
+ "types": "./dist/module.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/logto-io/js.git",
21
+ "directory": "packages/nuxt"
22
+ },
23
+ "devDependencies": {
24
+ "@nuxt/module-builder": "^0.5.5",
25
+ "@silverhand/eslint-config": "^5.0.0",
26
+ "eslint": "^8.56.0",
27
+ "h3": "^1.10.2",
28
+ "lint-staged": "^15.0.0",
29
+ "nuxt": "^3.10.2",
30
+ "prettier": "^3.0.0",
31
+ "typescript": "^5.3.3",
32
+ "vue": "^3.4.19"
33
+ },
34
+ "eslintConfig": {
35
+ "extends": "@silverhand"
36
+ },
37
+ "prettier": "@silverhand/eslint-config/.prettierrc",
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "dependencies": {
42
+ "@nuxt/kit": "^3.10.2",
43
+ "defu": "^6.1.4",
44
+ "@logto/node": "^2.4.0"
45
+ },
46
+ "scripts": {
47
+ "precommit": "lint-staged",
48
+ "dev": "nuxi dev playground",
49
+ "dev:prepare": "nuxt-module-build prepare && nuxi prepare playground",
50
+ "build": "nuxt-module-build prepare && nuxt-module-build build",
51
+ "check": "tsc --noEmit",
52
+ "lint": "eslint --ext .ts src"
53
+ }
54
+ }