nuxt-auth-kit 1.0.2 → 1.0.4

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/dist/module.json CHANGED
@@ -2,9 +2,9 @@
2
2
  "name": "nuxt-auth-kit",
3
3
  "configKey": "nuxtAuthKit",
4
4
  "compatibility": {
5
- "nuxt": "^3.0.0"
5
+ "nuxt": "^3.0.0 || ^4.0.0"
6
6
  },
7
- "version": "1.0.2",
7
+ "version": "1.0.4",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "0.8.4",
10
10
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -1,11 +1,12 @@
1
- import { defineNuxtModule, createResolver, addPlugin, addImports, addRouteMiddleware, addComponent } from '@nuxt/kit';
1
+ import { defineNuxtModule, createResolver, addPlugin, addImports, addComponent } from '@nuxt/kit';
2
+ import { join } from 'pathe';
2
3
 
3
4
  const module = defineNuxtModule({
4
5
  meta: {
5
6
  name: "nuxt-auth-kit",
6
7
  configKey: "nuxtAuthKit",
7
8
  compatibility: {
8
- nuxt: "^3.0.0"
9
+ nuxt: "^3.0.0 || ^4.0.0"
9
10
  }
10
11
  },
11
12
  defaults: {
@@ -57,31 +58,55 @@ const module = defineNuxtModule({
57
58
  from: resolver.resolve("./runtime/stores/auth")
58
59
  }
59
60
  ]);
60
- addRouteMiddleware({
61
- name: "auth",
62
- path: resolver.resolve("./runtime/middleware/auth"),
63
- global: false
64
- });
65
- addRouteMiddleware({
66
- name: "guest",
67
- path: resolver.resolve("./runtime/middleware/guest"),
68
- global: false
69
- });
70
- addRouteMiddleware({
71
- name: "role",
72
- path: resolver.resolve("./runtime/middleware/role"),
73
- global: false
61
+ nuxt.hook("app:resolve", (app) => {
62
+ const middlewareDir = resolver.resolve("./runtime/middleware");
63
+ for (const name of ["auth", "guest", "role"]) {
64
+ const path = join(middlewareDir, name);
65
+ const exists = app.middleware.find((m) => m.name === name);
66
+ if (!exists) {
67
+ app.middleware.push({ name, path, global: false });
68
+ }
69
+ }
74
70
  });
75
71
  const components = [
76
- // Auth
77
- { name: "AuthLayout", filePath: resolver.resolve("./runtime/components/auth/AuthLayout.vue") },
78
- { name: "AuthLoginForm", filePath: resolver.resolve("./runtime/components/auth/LoginForm.vue") },
79
- { name: "AuthRegisterForm", filePath: resolver.resolve("./runtime/components/auth/RegisterForm.vue") },
80
- { name: "AuthForgotPasswordForm", filePath: resolver.resolve("./runtime/components/auth/ForgotPasswordForm.vue") },
81
- { name: "AuthResetPasswordForm", filePath: resolver.resolve("./runtime/components/auth/ResetPasswordForm.vue") },
82
- // Profile
83
- { name: "ProfileUpdateForm", filePath: resolver.resolve("./runtime/components/profile/UpdateProfileForm.vue") },
84
- { name: "ProfileUpdatePasswordForm", filePath: resolver.resolve("./runtime/components/profile/UpdatePasswordForm.vue") }
72
+ {
73
+ name: "AuthLayout",
74
+ filePath: resolver.resolve("./runtime/components/auth/AuthLayout.vue")
75
+ },
76
+ {
77
+ name: "AuthLoginForm",
78
+ filePath: resolver.resolve("./runtime/components/auth/LoginForm.vue")
79
+ },
80
+ {
81
+ name: "AuthRegisterForm",
82
+ filePath: resolver.resolve(
83
+ "./runtime/components/auth/RegisterForm.vue"
84
+ )
85
+ },
86
+ {
87
+ name: "AuthForgotPasswordForm",
88
+ filePath: resolver.resolve(
89
+ "./runtime/components/auth/ForgotPasswordForm.vue"
90
+ )
91
+ },
92
+ {
93
+ name: "AuthResetPasswordForm",
94
+ filePath: resolver.resolve(
95
+ "./runtime/components/auth/ResetPasswordForm.vue"
96
+ )
97
+ },
98
+ {
99
+ name: "ProfileUpdateForm",
100
+ filePath: resolver.resolve(
101
+ "./runtime/components/profile/UpdateProfileForm.vue"
102
+ )
103
+ },
104
+ {
105
+ name: "ProfileUpdatePasswordForm",
106
+ filePath: resolver.resolve(
107
+ "./runtime/components/profile/UpdatePasswordForm.vue"
108
+ )
109
+ }
85
110
  ];
86
111
  for (const component of components) {
87
112
  addComponent(component);
@@ -1,4 +1,4 @@
1
- import type { LoginCredentials, RegisterData, UpdateProfileData, UpdatePasswordData, ForgotPasswordData, ResetPasswordData, ApiError } from '../types/index.js';
1
+ import type { LoginCredentials, RegisterData, UpdateProfileData, UpdatePasswordData, ForgotPasswordData, ResetPasswordData, ApiError } from "../types/index.js";
2
2
  export declare function useAuth(): {
3
3
  user: any;
4
4
  token: any;
@@ -1,7 +1,8 @@
1
- import { useRuntimeConfig, navigateTo, useCookie } from "#app";
1
+ import { useNuxtApp, useRuntimeConfig, navigateTo, useCookie } from "#app";
2
2
  import { useAuthStore } from "../stores/auth.js";
3
3
  export function useAuth() {
4
- const store = useAuthStore();
4
+ const nuxtApp = useNuxtApp();
5
+ const store = useAuthStore(nuxtApp.$pinia);
5
6
  const config = useRuntimeConfig();
6
7
  const opts = config.public.nuxtAuthKit;
7
8
  const apiBase = opts?.apiBase || "";
@@ -33,7 +34,7 @@ export function useAuth() {
33
34
  async function apiFetch(path, options = {}) {
34
35
  const headers = {
35
36
  "Content-Type": "application/json",
36
- "Accept": "application/json",
37
+ Accept: "application/json",
37
38
  ...options.headers || {}
38
39
  };
39
40
  if (store.token) {
@@ -165,10 +166,13 @@ export function useAuth() {
165
166
  async function forgotPassword(data) {
166
167
  store.setLoading(true);
167
168
  try {
168
- const response = await apiFetch(endpoints.forgotPassword, {
169
- method: "POST",
170
- body: JSON.stringify(data)
171
- });
169
+ const response = await apiFetch(
170
+ endpoints.forgotPassword,
171
+ {
172
+ method: "POST",
173
+ body: JSON.stringify(data)
174
+ }
175
+ );
172
176
  return { success: true, message: response.message };
173
177
  } catch (error) {
174
178
  return { success: false, error };
@@ -179,10 +183,13 @@ export function useAuth() {
179
183
  async function resetPassword(data) {
180
184
  store.setLoading(true);
181
185
  try {
182
- const response = await apiFetch(endpoints.resetPassword, {
183
- method: "POST",
184
- body: JSON.stringify(data)
185
- });
186
+ const response = await apiFetch(
187
+ endpoints.resetPassword,
188
+ {
189
+ method: "POST",
190
+ body: JSON.stringify(data)
191
+ }
192
+ );
186
193
  return { success: true, message: response.message };
187
194
  } catch (error) {
188
195
  return { success: false, error };
@@ -1,20 +1,25 @@
1
1
  import { defineNuxtPlugin, useCookie, useRuntimeConfig } from "#app";
2
2
  import { useAuthStore } from "../stores/auth.js";
3
3
  import { useAuth } from "../composables/useAuth.js";
4
- export default defineNuxtPlugin(async (nuxtApp) => {
5
- const store = useAuthStore();
6
- const config = useRuntimeConfig();
7
- const opts = config.public.nuxtAuthKit;
8
- const tokenCookieName = opts?.tokenCookieName || "auth_token";
9
- const tokenCookie = useCookie(tokenCookieName);
10
- if (tokenCookie.value && !store.token) {
11
- store.setToken(tokenCookie.value);
12
- try {
13
- const { fetchUser } = useAuth();
14
- await fetchUser();
15
- } catch (_) {
16
- store.clearAuth();
17
- tokenCookie.value = null;
4
+ export default defineNuxtPlugin({
5
+ name: "nuxt-auth-kit",
6
+ // Run AFTER Pinia plugin so $pinia is available
7
+ dependsOn: ["pinia"],
8
+ async setup(nuxtApp) {
9
+ const store = useAuthStore(nuxtApp.$pinia);
10
+ const config = useRuntimeConfig();
11
+ const opts = config.public.nuxtAuthKit;
12
+ const tokenCookieName = opts?.tokenCookieName || "auth_token";
13
+ const tokenCookie = useCookie(tokenCookieName);
14
+ if (tokenCookie.value && !store.token) {
15
+ store.setToken(tokenCookie.value);
16
+ try {
17
+ const { fetchUser } = useAuth();
18
+ await fetchUser();
19
+ } catch (_) {
20
+ store.clearAuth();
21
+ tokenCookie.value = null;
22
+ }
18
23
  }
19
24
  }
20
25
  });
@@ -1,4 +1,4 @@
1
- import type { AuthUser } from '../types/index.js';
1
+ import type { AuthUser } from "../types/index.js";
2
2
  export declare const useAuthStore: import("pinia").StoreDefinition<"nuxt-auth-kit", Pick<{
3
3
  user: import("vue").Ref<any, any>;
4
4
  token: import("vue").Ref<string | null, string | null>;
@@ -1,47 +1,51 @@
1
1
  import { defineStore } from "pinia";
2
2
  import { ref, computed } from "vue";
3
- export const useAuthStore = defineStore("nuxt-auth-kit", () => {
4
- const user = ref(null);
5
- const token = ref(null);
6
- const loading = ref(false);
7
- const isAuthenticated = computed(() => !!token.value && !!user.value);
8
- const isGuest = computed(() => !isAuthenticated.value);
9
- function hasRole(role) {
10
- if (!user.value?.roles) return false;
11
- const roles = Array.isArray(role) ? role : [role];
12
- return roles.some((r) => user.value.roles.includes(r));
3
+ export const useAuthStore = defineStore(
4
+ "nuxt-auth-kit",
5
+ () => {
6
+ const user = ref(null);
7
+ const token = ref(null);
8
+ const loading = ref(false);
9
+ const isAuthenticated = computed(() => !!token.value && !!user.value);
10
+ const isGuest = computed(() => !isAuthenticated.value);
11
+ function hasRole(role) {
12
+ if (!user.value?.roles) return false;
13
+ const roles = Array.isArray(role) ? role : [role];
14
+ return roles.some((r) => user.value.roles.includes(r));
15
+ }
16
+ function hasPermission(permission) {
17
+ if (!user.value?.permissions) return false;
18
+ const perms = Array.isArray(permission) ? permission : [permission];
19
+ return perms.some((p) => user.value.permissions.includes(p));
20
+ }
21
+ function setUser(userData) {
22
+ user.value = userData;
23
+ }
24
+ function setToken(tokenValue) {
25
+ token.value = tokenValue;
26
+ }
27
+ function clearAuth() {
28
+ user.value = null;
29
+ token.value = null;
30
+ }
31
+ function setLoading(state) {
32
+ loading.value = state;
33
+ }
34
+ return {
35
+ user,
36
+ token,
37
+ loading,
38
+ isAuthenticated,
39
+ isGuest,
40
+ hasRole,
41
+ hasPermission,
42
+ setUser,
43
+ setToken,
44
+ clearAuth,
45
+ setLoading
46
+ };
47
+ },
48
+ {
49
+ persist: false
13
50
  }
14
- function hasPermission(permission) {
15
- if (!user.value?.permissions) return false;
16
- const perms = Array.isArray(permission) ? permission : [permission];
17
- return perms.some((p) => user.value.permissions.includes(p));
18
- }
19
- function setUser(userData) {
20
- user.value = userData;
21
- }
22
- function setToken(tokenValue) {
23
- token.value = tokenValue;
24
- }
25
- function clearAuth() {
26
- user.value = null;
27
- token.value = null;
28
- }
29
- function setLoading(state) {
30
- loading.value = state;
31
- }
32
- return {
33
- user,
34
- token,
35
- loading,
36
- isAuthenticated,
37
- isGuest,
38
- hasRole,
39
- hasPermission,
40
- setUser,
41
- setToken,
42
- clearAuth,
43
- setLoading
44
- };
45
- }, {
46
- persist: false
47
- });
51
+ );
@@ -68,7 +68,7 @@ export interface ModuleOptions {
68
68
  resetPassword?: string;
69
69
  };
70
70
  /** Token storage strategy */
71
- tokenStorage?: 'cookie' | 'localStorage';
71
+ tokenStorage?: "cookie" | "localStorage";
72
72
  /** Cookie name for token */
73
73
  tokenCookieName?: string;
74
74
  /** Redirect routes */
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "nuxt-auth-kit",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Nuxt module for authentication with Laravel API — login, register, profile, password management, roles & permissions",
5
5
  "keywords": ["nuxt", "nuxt-module", "authentication", "laravel", "roles", "permissions"],
6
+ "author": {
7
+ "name": "3S Tech Group",
8
+ "url": "https://github.com/sssadgroup/"
9
+ },
10
+ "homepage": "https://sssadgroup.github.io/nuxt-auth-kit",
6
11
  "license": "MIT",
7
12
  "type": "module",
8
13
  "exports": {