@authup/client-web-nuxt 1.0.0-beta.20

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.
@@ -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,23 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ type AuthupRuntimeOptions = {
4
+ apiURL?: string;
5
+ apiURLRuntimeKey?: string;
6
+ /**
7
+ * Separate apiURL for server side rendering.
8
+ */
9
+ apiURLServer?: string;
10
+ /**
11
+ * Separate extraction of apiURL for server side rendering.
12
+ */
13
+ apiURLServerRuntimeKey?: string;
14
+ cookieDomain?: string;
15
+ cookieDomainRuntimeKey?: string;
16
+ homeRoute?: string;
17
+ loginRoute?: string;
18
+ logoutRoute?: string;
19
+ };
20
+
21
+ declare const _default: _nuxt_schema.NuxtModule<AuthupRuntimeOptions, AuthupRuntimeOptions, false>;
22
+
23
+ export { _default as default };
@@ -0,0 +1,23 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ type AuthupRuntimeOptions = {
4
+ apiURL?: string;
5
+ apiURLRuntimeKey?: string;
6
+ /**
7
+ * Separate apiURL for server side rendering.
8
+ */
9
+ apiURLServer?: string;
10
+ /**
11
+ * Separate extraction of apiURL for server side rendering.
12
+ */
13
+ apiURLServerRuntimeKey?: string;
14
+ cookieDomain?: string;
15
+ cookieDomainRuntimeKey?: string;
16
+ homeRoute?: string;
17
+ loginRoute?: string;
18
+ logoutRoute?: string;
19
+ };
20
+
21
+ declare const _default: _nuxt_schema.NuxtModule<AuthupRuntimeOptions, AuthupRuntimeOptions, false>;
22
+
23
+ export { _default as default };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@authup/client-web-nuxt",
3
+ "configKey": "authup",
4
+ "version": "1.0.0-beta.20",
5
+ "builder": {
6
+ "@nuxt/module-builder": "0.8.4",
7
+ "unbuild": "2.0.0"
8
+ }
9
+ }
@@ -0,0 +1,45 @@
1
+ import { defineNuxtModule, addPlugin, addRouteMiddleware } from '@nuxt/kit';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { merge } from 'smob';
5
+
6
+ const module = defineNuxtModule({
7
+ meta: {
8
+ name: "@authup/client-web-nuxt",
9
+ configKey: "authup"
10
+ },
11
+ defaults: {},
12
+ async setup(options, nuxt) {
13
+ const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
14
+ nuxt.options.build.transpile.push(runtimeDir);
15
+ const runtimeOptions = {
16
+ apiURL: options.apiURL,
17
+ apiURLRuntimeKey: options.apiURLRuntimeKey,
18
+ apiURLServer: options.apiURLServer,
19
+ apiURLServerRuntimeKey: options.apiURLServerRuntimeKey,
20
+ cookieDomain: options.cookieDomain,
21
+ cookieDomainRuntimeKey: options.cookieDomainRuntimeKey,
22
+ homeRoute: options.homeRoute,
23
+ loginRoute: options.loginRoute,
24
+ logoutRoute: options.logoutRoute
25
+ };
26
+ nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {};
27
+ if (nuxt.options.runtimeConfig.public.authup) {
28
+ nuxt.options.runtimeConfig.public.authup = merge(
29
+ nuxt.options.runtimeConfig.public.authup,
30
+ runtimeOptions
31
+ );
32
+ } else {
33
+ nuxt.options.runtimeConfig.public.authup = runtimeOptions;
34
+ }
35
+ addPlugin(path.resolve(runtimeDir, "plugins/00.pinia"));
36
+ addPlugin(path.resolve(runtimeDir, "plugins/01.kit"));
37
+ addRouteMiddleware({
38
+ name: "loggedIn",
39
+ path: path.resolve(runtimeDir, "middleware/00.session"),
40
+ global: true
41
+ });
42
+ }
43
+ });
44
+
45
+ export { module as default };
@@ -0,0 +1,5 @@
1
+ export declare enum RouteMetaKey {
2
+ REQUIRED_LOGGED_IN = "requireLoggedIn",
3
+ REQUIRED_LOGGED_OUT = "requireLoggedOut",
4
+ REQUIRED_PERMISSIONS = "requirePermissions"
5
+ }
@@ -0,0 +1,6 @@
1
+ export var RouteMetaKey = /* @__PURE__ */ ((RouteMetaKey2) => {
2
+ RouteMetaKey2["REQUIRED_LOGGED_IN"] = "requireLoggedIn";
3
+ RouteMetaKey2["REQUIRED_LOGGED_OUT"] = "requireLoggedOut";
4
+ RouteMetaKey2["REQUIRED_PERMISSIONS"] = "requirePermissions";
5
+ return RouteMetaKey2;
6
+ })(RouteMetaKey || {});
@@ -0,0 +1,2 @@
1
+ export * from './route-path.js';
2
+ export * from './route-permissions.js';
@@ -0,0 +1,2 @@
1
+ export * from "./route-path.js";
2
+ export * from "./route-permissions.js";
@@ -0,0 +1,7 @@
1
+ import type { RouteLocationNormalized } from 'vue-router';
2
+ type RoutePathBuildOptions = {
3
+ location?: RouteLocationNormalized;
4
+ excluded?: string[];
5
+ };
6
+ export declare function buildRoutePath(context?: RoutePathBuildOptions): string | undefined;
7
+ export {};
@@ -0,0 +1,13 @@
1
+ export function buildRoutePath(context = {}) {
2
+ if (!context.location || context.location.query.redirect) {
3
+ return void 0;
4
+ }
5
+ if (context.excluded) {
6
+ for (let i = 0; i < context.excluded.length; i++) {
7
+ if (context.location.fullPath.startsWith(context.excluded[i])) {
8
+ return void 0;
9
+ }
10
+ }
11
+ }
12
+ return context.location.fullPath;
13
+ }
@@ -0,0 +1,8 @@
1
+ import { type Store } from '@authup/client-web-kit';
2
+ import type { RouteLocationNormalized } from 'vue-router';
3
+ type RoutePermissionsCheckOptions = {
4
+ routeLocation: RouteLocationNormalized;
5
+ store: Store;
6
+ };
7
+ export declare function checkRoutePermissions(context: RoutePermissionsCheckOptions): Promise<boolean>;
8
+ export {};
@@ -0,0 +1,41 @@
1
+ import { storeToRefs } from "@authup/client-web-kit";
2
+ import { hasOwnProperty } from "@authup/kit";
3
+ import { RouteMetaKey } from "../constants.js";
4
+ export async function checkRoutePermissions(context) {
5
+ const storeRefs = storeToRefs(context.store);
6
+ let identity;
7
+ if (storeRefs.userId.value) {
8
+ identity = {
9
+ type: "user",
10
+ id: storeRefs.userId.value
11
+ };
12
+ }
13
+ for (let i = 0; i < context.routeLocation.matched.length; i++) {
14
+ const match = context.routeLocation.matched[i];
15
+ if (!match.meta || !hasOwnProperty(match.meta, RouteMetaKey.REQUIRED_PERMISSIONS)) {
16
+ continue;
17
+ }
18
+ let permissions = [];
19
+ if (match.meta[RouteMetaKey.REQUIRED_PERMISSIONS]) {
20
+ if (Array.isArray(match.meta[RouteMetaKey.REQUIRED_PERMISSIONS])) {
21
+ permissions = match.meta[RouteMetaKey.REQUIRED_PERMISSIONS];
22
+ } else if (typeof match.meta[RouteMetaKey.REQUIRED_PERMISSIONS] === "string") {
23
+ permissions = [match.meta[RouteMetaKey.REQUIRED_PERMISSIONS]];
24
+ }
25
+ }
26
+ if (permissions.length === 0) {
27
+ continue;
28
+ }
29
+ try {
30
+ await context.store.permissionChecker.preCheckOneOf({
31
+ name: permissions,
32
+ data: {
33
+ identity
34
+ }
35
+ });
36
+ } catch (e) {
37
+ return false;
38
+ }
39
+ }
40
+ return true;
41
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: import("nuxt/app").RouteMiddleware;
2
+ export default _default;
@@ -0,0 +1,78 @@
1
+ import { storeToRefs, useStore } from "@authup/client-web-kit";
2
+ import { defineNuxtRouteMiddleware, navigateTo, useRuntimeConfig } from "#imports";
3
+ import { RouteMetaKey } from "../constants.js";
4
+ import { buildRoutePath, checkRoutePermissions } from "../helpers/index.js";
5
+ export default defineNuxtRouteMiddleware(
6
+ async (to, from) => {
7
+ const store = useStore();
8
+ const storeRefs = storeToRefs(store);
9
+ const runtimeConfig = useRuntimeConfig();
10
+ const runtimeOptions = runtimeConfig.public.authup;
11
+ const homeRoute = runtimeOptions.homeRoute || "/";
12
+ const loginRoute = runtimeOptions.loginRoute || "/login";
13
+ const logoutRoute = runtimeOptions.logoutRoute || "/logout";
14
+ try {
15
+ await store.resolve();
16
+ } catch (e) {
17
+ store.logout();
18
+ const redirect = buildRoutePath({
19
+ location: to,
20
+ excluded: [
21
+ logoutRoute,
22
+ loginRoute
23
+ ]
24
+ });
25
+ if (redirect) {
26
+ return navigateTo({
27
+ path: loginRoute,
28
+ query: {
29
+ redirect
30
+ }
31
+ });
32
+ }
33
+ return void 0;
34
+ }
35
+ const hasLoggedInProtection = to.matched.some(
36
+ (matched) => !!matched.meta[RouteMetaKey.REQUIRED_LOGGED_IN]
37
+ );
38
+ if (hasLoggedInProtection && !storeRefs.loggedIn.value) {
39
+ const redirect = buildRoutePath({
40
+ location: to,
41
+ excluded: [
42
+ logoutRoute,
43
+ loginRoute
44
+ ]
45
+ });
46
+ return navigateTo({
47
+ path: loginRoute,
48
+ query: {
49
+ ...redirect ? { redirect } : {}
50
+ }
51
+ });
52
+ }
53
+ const hasPermissionProtection = to.matched.some(
54
+ (matched) => !!matched.meta[RouteMetaKey.REQUIRED_PERMISSIONS]
55
+ );
56
+ if (hasPermissionProtection) {
57
+ const hasPermissions = await checkRoutePermissions({
58
+ store,
59
+ routeLocation: to
60
+ });
61
+ if (!hasPermissions) {
62
+ return navigateTo({
63
+ path: from ? from.fullPath : homeRoute
64
+ });
65
+ }
66
+ }
67
+ const hasLoggedOutProtection = to.matched.some(
68
+ (matched) => matched.meta[RouteMetaKey.REQUIRED_LOGGED_OUT]
69
+ );
70
+ if (hasLoggedOutProtection && storeRefs.loggedIn.value) {
71
+ store.logout();
72
+ return navigateTo({
73
+ path: to.fullPath
74
+ });
75
+ }
76
+ return void 0;
77
+ }
78
+ );
@@ -0,0 +1,6 @@
1
+ declare const _default: import("nuxt/app").Plugin<{
2
+ pinia: import("pinia").Pinia;
3
+ }> & import("nuxt/app").ObjectPlugin<{
4
+ pinia: import("pinia").Pinia;
5
+ }>;
6
+ export default _default;
@@ -0,0 +1,24 @@
1
+ import { createPinia, setActivePinia } from "pinia";
2
+ import { defineNuxtPlugin } from "#imports";
3
+ export default defineNuxtPlugin({
4
+ name: "authupPinia",
5
+ enforce: "pre",
6
+ async setup(nuxt) {
7
+ if (nuxt.$pinia) {
8
+ return {};
9
+ }
10
+ const pinia = createPinia();
11
+ nuxt.vueApp.use(pinia);
12
+ setActivePinia(pinia);
13
+ if (import.meta.server) {
14
+ nuxt.payload.pinia = pinia.state.value;
15
+ } else if (nuxt.payload && nuxt.payload.pinia) {
16
+ pinia.state.value = nuxt.payload.pinia;
17
+ }
18
+ return {
19
+ provide: {
20
+ pinia
21
+ }
22
+ };
23
+ }
24
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
2
+ export default _default;
@@ -0,0 +1,90 @@
1
+ import { install } from "@authup/client-web-kit";
2
+ import { getPathValue } from "pathtrace";
3
+ import {
4
+ defineNuxtPlugin,
5
+ tryUseNuxtApp,
6
+ useCookie,
7
+ useRuntimeConfig
8
+ } from "#imports";
9
+ export default defineNuxtPlugin({
10
+ name: "authupKit",
11
+ enforce: "pre",
12
+ dependsOn: ["authupPinia"],
13
+ setup(ctx) {
14
+ const runtimeConfig = useRuntimeConfig();
15
+ let apiURL;
16
+ let apiURLServer;
17
+ let cookieDomain;
18
+ const runtimeOptions = runtimeConfig.public.authup;
19
+ if (import.meta.server && (runtimeOptions.apiURLServer || runtimeOptions.apiURLServerRuntimeKey)) {
20
+ if (runtimeOptions.apiURLServer) {
21
+ apiURLServer = runtimeOptions.apiURLServer;
22
+ }
23
+ if (runtimeOptions.apiURLServerRuntimeKey) {
24
+ let pathValue = getPathValue(runtimeConfig, runtimeOptions.apiURLServerRuntimeKey);
25
+ if (typeof pathValue === "string") {
26
+ apiURLServer = pathValue;
27
+ }
28
+ pathValue = getPathValue(runtimeConfig.public, runtimeOptions.apiURLServerRuntimeKey);
29
+ if (typeof pathValue === "string") {
30
+ apiURLServer = pathValue;
31
+ }
32
+ }
33
+ }
34
+ if (runtimeOptions.apiURL) {
35
+ apiURL = runtimeOptions.apiURL;
36
+ }
37
+ if (runtimeOptions.apiURLRuntimeKey) {
38
+ const pathValue = getPathValue(runtimeConfig.public, runtimeOptions.apiURLRuntimeKey);
39
+ if (typeof pathValue === "string") {
40
+ apiURL = pathValue;
41
+ }
42
+ }
43
+ if (runtimeOptions.cookieDomain) {
44
+ cookieDomain = runtimeOptions.cookieDomain;
45
+ }
46
+ if (runtimeOptions.cookieDomainRuntimeKey) {
47
+ const pathValue = getPathValue(runtimeConfig.public, runtimeOptions.cookieDomainRuntimeKey);
48
+ if (typeof pathValue === "string") {
49
+ cookieDomain = pathValue;
50
+ }
51
+ }
52
+ if (!cookieDomain) {
53
+ const { hostname } = new URL(apiURL || "http://localhost:3000");
54
+ cookieDomain = hostname;
55
+ }
56
+ if (cookieDomain === "127.0.0.1") {
57
+ cookieDomain = "localhost";
58
+ }
59
+ const cookieOptions = {
60
+ domain: cookieDomain || "localhost"
61
+ };
62
+ install(ctx.vueApp, {
63
+ pinia: ctx.$pinia,
64
+ baseURL: apiURLServer || apiURL || "http://localhost:3000",
65
+ cookieSet: (key, value) => {
66
+ const app = tryUseNuxtApp();
67
+ if (app) {
68
+ const cookie = useCookie(key, cookieOptions);
69
+ cookie.value = value;
70
+ }
71
+ },
72
+ cookieUnset: (key) => {
73
+ const app = tryUseNuxtApp();
74
+ if (app) {
75
+ const cookie = useCookie(key, cookieOptions);
76
+ cookie.value = null;
77
+ }
78
+ },
79
+ cookieGet: (key) => {
80
+ const app = tryUseNuxtApp();
81
+ if (app) {
82
+ const cookie = useCookie(key, cookieOptions);
83
+ return cookie.value;
84
+ }
85
+ return null;
86
+ },
87
+ isServer: import.meta.server
88
+ });
89
+ }
90
+ });
@@ -0,0 +1,17 @@
1
+ export type AuthupRuntimeOptions = {
2
+ apiURL?: string;
3
+ apiURLRuntimeKey?: string;
4
+ /**
5
+ * Separate apiURL for server side rendering.
6
+ */
7
+ apiURLServer?: string;
8
+ /**
9
+ * Separate extraction of apiURL for server side rendering.
10
+ */
11
+ apiURLServerRuntimeKey?: string;
12
+ cookieDomain?: string;
13
+ cookieDomainRuntimeKey?: string;
14
+ homeRoute?: string;
15
+ loginRoute?: string;
16
+ logoutRoute?: string;
17
+ };
File without changes
@@ -0,0 +1,7 @@
1
+ import type { NuxtModule } from '@nuxt/schema'
2
+
3
+ import type { default as Module } from './module.js'
4
+
5
+ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
+
7
+ export { default } from './module.js'
@@ -0,0 +1,7 @@
1
+ import type { NuxtModule } from '@nuxt/schema'
2
+
3
+ import type { default as Module } from './module'
4
+
5
+ export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
+
7
+ export { default } from './module'
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@authup/client-web-nuxt",
3
+ "type": "module",
4
+ "version": "1.0.0-beta.20",
5
+ "description": "Nuxt module for the authup ecosystem",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/authup/authup.git",
9
+ "directory": "packages/client-web-nuxt"
10
+ },
11
+ "license": "Apache-2.0",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/types.d.ts",
15
+ "import": "./dist/module.mjs",
16
+ "require": "./dist/module.cjs"
17
+ }
18
+ },
19
+ "main": "./dist/module.cjs",
20
+ "module": "./dist/module.mjs",
21
+ "types": "./dist/types.d.ts",
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "scripts": {
26
+ "build": "nuxt-module-build prepare && nuxt-module-build build",
27
+ "prepack": "nuxt-module-build build",
28
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
29
+ "lint": "eslint ."
30
+ },
31
+ "dependencies": {
32
+ "@authup/kit": "^1.0.0-beta.20",
33
+ "@authup/client-web-kit": "^1.0.0-beta.20",
34
+ "@nuxt/kit": "^3.13.2",
35
+ "pathtrace": "^1.1.0",
36
+ "smob": "^1.5.0"
37
+ },
38
+ "devDependencies": {
39
+ "@nuxt/module-builder": "^0.8.4",
40
+ "nuxt": "^3.13.2",
41
+ "pinia": "^2.2.4"
42
+ },
43
+ "peerDependencies": {
44
+ "pinia": "^2.2.4"
45
+ }
46
+ }