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 +2 -2
- package/dist/module.mjs +50 -25
- package/dist/runtime/composables/useAuth.d.ts +1 -1
- package/dist/runtime/composables/useAuth.js +18 -11
- package/dist/runtime/plugins/auth.js +19 -14
- package/dist/runtime/stores/auth.d.ts +1 -1
- package/dist/runtime/stores/auth.js +48 -44
- package/dist/runtime/types/index.d.ts +1 -1
- package/package.json +6 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addPlugin, addImports,
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
{
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
{
|
|
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
|
|
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
|
|
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
|
-
|
|
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(
|
|
169
|
-
|
|
170
|
-
|
|
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(
|
|
183
|
-
|
|
184
|
-
|
|
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(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
|
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(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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?:
|
|
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.
|
|
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": {
|