nuxt-users 1.2.0 → 1.4.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/CHANGELOG.md CHANGED
@@ -1,6 +1,54 @@
1
1
  # Changelog
2
2
 
3
3
 
4
+ ## v1.4.0
5
+
6
+ [compare changes](https://github.com/rrd108/nuxt-users/compare/v1.3.0...v1.4.0)
7
+
8
+ ### 🚀 Enhancements
9
+
10
+ - Implement forgot password closes #57 ([#57](https://github.com/rrd108/nuxt-users/issues/57))
11
+
12
+ ### 🩹 Fixes
13
+
14
+ - Css color ([1d72336](https://github.com/rrd108/nuxt-users/commit/1d72336))
15
+ - Auth middleware ([113b165](https://github.com/rrd108/nuxt-users/commit/113b165))
16
+ - Enhances auth middleware and improves logging ([f519bbc](https://github.com/rrd108/nuxt-users/commit/f519bbc))
17
+ - Playground colors ([f87ade1](https://github.com/rrd108/nuxt-users/commit/f87ade1))
18
+
19
+ ### 💅 Refactors
20
+
21
+ - Have a dedicated playground login page ([487d754](https://github.com/rrd108/nuxt-users/commit/487d754))
22
+
23
+ ### 🏡 Chore
24
+
25
+ - **release:** V1.3.0 ([b127043](https://github.com/rrd108/nuxt-users/commit/b127043))
26
+
27
+ ### ❤️ Contributors
28
+
29
+ - Rrd108 ([@rrd108](https://github.com/rrd108))
30
+
31
+ ## v1.3.0
32
+
33
+ [compare changes](https://github.com/rrd108/nuxt-users/compare/v1.2.0...v1.3.0)
34
+
35
+ ### 🚀 Enhancements
36
+
37
+ - Adds theme support with light/dark mode ([005ea87](https://github.com/rrd108/nuxt-users/commit/005ea87))
38
+ - Adds logout functionality ([a61b4e8](https://github.com/rrd108/nuxt-users/commit/a61b4e8))
39
+
40
+ ### 💅 Refactors
41
+
42
+ - Restyles login form for better UX closes #51 ([#51](https://github.com/rrd108/nuxt-users/issues/51))
43
+
44
+ ### 🏡 Chore
45
+
46
+ - **release:** V1.2.0 ([54fd5c8](https://github.com/rrd108/nuxt-users/commit/54fd5c8))
47
+
48
+ ### ❤️ Contributors
49
+
50
+ - Rrd108 ([@rrd108](https://github.com/rrd108))
51
+
4
52
  ## v1.2.0
5
53
 
6
54
  [compare changes](https://github.com/rrd108/nuxt-users/compare/v1.1.6...v1.2.0)
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-users",
3
3
  "configKey": "nuxtUsers",
4
- "version": "1.2.0",
4
+ "version": "1.4.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.1",
7
7
  "unbuild": "3.6.0"
package/dist/module.mjs CHANGED
@@ -52,7 +52,7 @@ const module = defineNuxtModule({
52
52
  passwordResetTokens: options.tables?.passwordResetTokens || defaultOptions.tables.passwordResetTokens
53
53
  },
54
54
  auth: {
55
- whitelist: options.auth?.whitelist || defaultOptions.auth?.whitelist || ["/login"]
55
+ whitelist: [...defaultOptions.auth?.whitelist || [], ...options.auth?.whitelist || []]
56
56
  }
57
57
  };
58
58
  addPlugin({
@@ -75,6 +75,11 @@ const module = defineNuxtModule({
75
75
  method: "post",
76
76
  handler: resolver.resolve("./runtime/server/api/auth/reset-password.post")
77
77
  });
78
+ addServerHandler({
79
+ route: "/api/logout",
80
+ method: "get",
81
+ handler: resolver.resolve("./runtime/server/api/logout.get")
82
+ });
78
83
  addComponent({
79
84
  name: "LoginForm",
80
85
  filePath: resolver.resolve("./runtime/components/LoginForm.vue")
@@ -87,6 +92,10 @@ const module = defineNuxtModule({
87
92
  name: "ResetPasswordForm",
88
93
  filePath: resolver.resolve("./runtime/components/ResetPasswordForm.vue")
89
94
  });
95
+ addComponent({
96
+ name: "LogoutLink",
97
+ filePath: resolver.resolve("./runtime/components/LogoutLink.vue")
98
+ });
90
99
  nuxt.options.css = nuxt.options.css || [];
91
100
  nuxt.options.css.push(resolver.resolve("./runtime/assets/colors.css"));
92
101
  }
@@ -1 +1 @@
1
- :root{--color-primary:#3b82f6;--color-primary-dark:#2563eb;--color-primary-light:#60a5fa;--color-secondary:#059669;--color-secondary-dark:#047857;--color-secondary-light:#10b981;--color-white:#fff;--color-black:#000;--color-gray-50:#f9fafb;--color-gray-100:#f3f4f6;--color-gray-200:#e5e7eb;--color-gray-300:#d1d5db;--color-gray-400:#9ca3af;--color-gray-500:#6b7280;--color-gray-600:#4b5563;--color-gray-700:#374151;--color-gray-800:#1f2937;--color-gray-900:#111827;--color-success:#059669;--color-success-light:#f0fdf4;--color-warning:#f59e0b;--color-warning-light:#fef3c7;--color-warning-text:#92400e;--color-error:#dc2626;--color-error-light:#fef2f2;--color-error-border:#fecaca;--color-error-alt:#db5050;--color-border:#e5e7eb;--color-border-light:#d1d5db;--color-border-dark:#ccc;--color-bg-primary:#fff;--color-bg-secondary:#f9fafb;--color-bg-tertiary:#f3f4f6}
1
+ :root{--color-primary:#60a5fa;--color-primary-dark:#3b82f6;--color-primary-light:#93c5fd;--color-secondary:#10b981;--color-secondary-dark:#059669;--color-secondary-light:#34d399;--color-white:#000;--color-black:#fff;--color-gray-50:#111827;--color-gray-100:#1f2937;--color-gray-200:#374151;--color-gray-300:#4b5563;--color-gray-400:#6b7280;--color-gray-500:#9ca3af;--color-gray-600:#d1d5db;--color-gray-700:#e5e7eb;--color-gray-800:#f3f4f6;--color-gray-900:#f9fafb;--color-success:#10b981;--color-success-light:#064e3b;--color-warning:#fbbf24;--color-warning-light:#451a03;--color-warning-text:#fef3c7;--color-error:#ef4444;--color-error-light:#450a0a;--color-error-border:#7f1d1d;--color-error-alt:#f87171;--color-border:#374151;--color-border-light:#4b5563;--color-border-dark:#6b7280;--color-bg-primary:#111827;--color-bg-secondary:#1f2937;--color-bg-tertiary:#374151}@media (prefers-color-scheme:light){.light,:root{--color-primary:#3b82f6;--color-primary-dark:#2563eb;--color-primary-light:#60a5fa;--color-secondary:#059669;--color-secondary-dark:#047857;--color-secondary-light:#10b981;--color-white:#fff;--color-black:#000;--color-gray-50:#f9fafb;--color-gray-100:#f3f4f6;--color-gray-200:#e5e7eb;--color-gray-300:#d1d5db;--color-gray-400:#9ca3af;--color-gray-500:#6b7280;--color-gray-600:#4b5563;--color-gray-700:#374151;--color-gray-800:#1f2937;--color-gray-900:#111827;--color-success:#059669;--color-success-light:#f0fdf4;--color-warning:#f59e0b;--color-warning-light:#fef3c7;--color-warning-text:#92400e;--color-error:#dc2626;--color-error-light:#fef2f2;--color-error-border:#fecaca;--color-error-alt:#db5050;--color-border:#e5e7eb;--color-border-light:#d1d5db;--color-border-dark:#ccc;--color-bg-primary:#fff;--color-bg-secondary:#f9fafb;--color-bg-tertiary:#f3f4f6}}
@@ -3,11 +3,14 @@ import { ref } from "vue";
3
3
  import { navigateTo } from "#app";
4
4
  const props = defineProps({
5
5
  apiEndpoint: { type: String, required: false, default: "/api/login" },
6
- redirectTo: { type: String, required: false, default: "/" }
6
+ redirectTo: { type: String, required: false, default: "/" },
7
+ forgotPasswordEndpoint: { type: String, required: false, default: "/api/auth/forgot-password" }
7
8
  });
8
- const emit = defineEmits(["success", "error", "submit"]);
9
+ const emit = defineEmits(["success", "error", "forgot-password-error", "submit", "forgot-password-success"]);
9
10
  const isLoading = ref(false);
11
+ const isForgotPasswordLoading = ref(false);
10
12
  const error = ref("");
13
+ const forgotPasswordMessage = ref("");
11
14
  const formData = ref({
12
15
  email: "",
13
16
  password: "",
@@ -37,10 +40,35 @@ const handleSubmit = async () => {
37
40
  isLoading.value = false;
38
41
  }
39
42
  };
43
+ const handleForgotPassword = async () => {
44
+ if (!formData.value.email) {
45
+ error.value = "Please enter your email address first";
46
+ return;
47
+ }
48
+ isForgotPasswordLoading.value = true;
49
+ forgotPasswordMessage.value = "";
50
+ error.value = "";
51
+ try {
52
+ const response = await $fetch(props.forgotPasswordEndpoint, {
53
+ method: "POST",
54
+ body: {
55
+ email: formData.value.email
56
+ }
57
+ });
58
+ forgotPasswordMessage.value = response.message;
59
+ emit("forgot-password-success");
60
+ } catch (err) {
61
+ const errorMessage = err && typeof err === "object" && "data" in err && err.data && typeof err.data === "object" && "statusMessage" in err.data ? String(err.data.statusMessage) : err instanceof Error ? err.message : "Failed to send password reset email";
62
+ error.value = errorMessage;
63
+ emit("forgot-password-error", errorMessage);
64
+ } finally {
65
+ isForgotPasswordLoading.value = false;
66
+ }
67
+ };
40
68
  </script>
41
69
 
42
70
  <template>
43
- <div class="login-form-container">
71
+ <section>
44
72
  <form @submit.prevent="handleSubmit">
45
73
  <!-- Header slot -->
46
74
  <slot name="header">
@@ -119,11 +147,32 @@ const handleSubmit = async () => {
119
147
  <a
120
148
  href="#"
121
149
  class="forgot-link"
122
- >Forgot your password?</a>
150
+ :class="{ loading: isForgotPasswordLoading }"
151
+ @click.prevent="handleForgotPassword"
152
+ >
153
+ <span
154
+ v-if="isForgotPasswordLoading"
155
+ class="loading-spinner"
156
+ />
157
+ {{ isForgotPasswordLoading ? "Sending..." : "Forgot your password?" }}
158
+ </a>
123
159
  </p>
124
160
  </div>
125
161
  </slot>
126
162
 
163
+ <!-- Success message slot -->
164
+ <slot
165
+ name="success-message"
166
+ :message="forgotPasswordMessage"
167
+ >
168
+ <div
169
+ v-if="forgotPasswordMessage"
170
+ class="success-message"
171
+ >
172
+ {{ forgotPasswordMessage }}
173
+ </div>
174
+ </slot>
175
+
127
176
  <!-- Error message slot -->
128
177
  <slot
129
178
  name="error-message"
@@ -137,9 +186,9 @@ const handleSubmit = async () => {
137
186
  </div>
138
187
  </slot>
139
188
  </form>
140
- </div>
189
+ </section>
141
190
  </template>
142
191
 
143
192
  <style scoped>
144
- .login-form-container{background:var(--color-bg-primary);border:1px solid var(--color-border);border-radius:12px;box-shadow:0 4px 6px -1px rgba(0,0,0,.1),0 2px 4px -1px rgba(0,0,0,.06);margin:0 auto;max-width:400px;padding:2em;width:100%}form{gap:1.5rem}.form-group,form{display:flex;flex-direction:column}label{display:block;font-weight:500;margin-bottom:.5em}input[type=email],input[type=password],label{color:var(--color-gray-700);font-size:.875rem}input[type=email],input[type=password]{background-color:var(--color-bg-secondary);border:1px solid var(--color-border-light);border-radius:8px;box-sizing:border-box;padding:.75em 1em;transition:all .2s ease-in-out;width:100%}input[type=email]:focus,input[type=password]:focus{background-color:var(--color-bg-primary);border-color:var(--color-primary);box-shadow:0 0 0 3px rgba(59,130,246,.1);outline:none}.login-header{margin-bottom:1em;text-align:center}.login-title{color:var(--color-gray-800);font-size:1.875rem;font-weight:700;line-height:1.2;margin:0 0 .5em}.login-subtitle{color:var(--color-gray-500);font-size:.875rem;line-height:1.5;margin:0}.remember-me{align-items:center;display:flex;gap:.5rem}input[type=checkbox]{margin:0;width:auto}.remember-me label{color:var(--color-gray-700);font-size:.875rem;margin:0}button[type=submit]{align-items:center;background-color:var(--color-primary);border:1px solid var(--color-primary);border-radius:8px;color:var(--color-white);cursor:pointer;display:flex;font-size:.875rem;font-weight:500;gap:.5rem;justify-content:center;min-height:44px;padding:.75em 1em;transition:all .2s ease-in-out;width:100%}button[type=submit]:hover:not(:disabled){background-color:var(--color-primary-dark);border-color:var(--color-primary-dark)}button[type=submit]:disabled{cursor:not-allowed;opacity:.6}.loading-spinner{animation:spin 1s linear infinite;border:2px solid var(--color-white);border-radius:50%;border-top:2px solid transparent;height:16px;width:16px}@keyframes spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}.login-footer{margin-top:1em;text-align:center}.forgot-password{margin:0}.forgot-link{color:var(--color-primary);font-size:.875rem;text-decoration:none;transition:color .2s ease-in-out}.forgot-link:hover{color:var(--color-primary-dark);text-decoration:underline}.error-message{background-color:var(--color-error-light);border:1px solid var(--color-error-border);border-radius:8px;color:var(--color-error);font-size:.875rem;margin-top:1em;padding:.75em 1em;text-align:center}@media (max-width:480px){.login-form-container{margin:1em;padding:1.5em}.login-title{font-size:1.5rem}}
193
+ section{background:linear-gradient(135deg,var(--color-bg-primary) 0,var(--color-bg-secondary) 100%);border:1px solid var(--color-border);border-radius:.75em;box-shadow:0 4px 6px -1px rgba(0,0,0,.1),0 2px 4px -1px rgba(0,0,0,.06);margin:0 auto;max-width:25rem;padding:1em}form{gap:1.5rem}.form-group,form{display:flex;flex-direction:column}label{display:block;font-weight:500;margin-bottom:.5em}input[type=email],input[type=password],label{color:var(--color-gray-700);font-size:.875rem}input[type=email],input[type=password]{background-color:var(--color-bg-secondary);border:1px solid var(--color-border-light);border-radius:8px;box-sizing:border-box;padding:.75em 1em;transition:all .2s ease-in-out;width:100%}input[type=email]:focus,input[type=password]:focus{background-color:var(--color-bg-primary);border-color:var(--color-primary);box-shadow:0 0 0 3px rgba(59,130,246,.1);outline:none}.login-header{margin-bottom:1em;text-align:center}.login-title{color:var(--color-gray-800);font-size:1.875rem;font-weight:700;line-height:1.2;margin:0 0 .5em}.login-subtitle{color:var(--color-gray-500);font-size:.875rem;line-height:1.5;margin:0}.remember-me{align-items:center;display:flex;gap:.5rem}input[type=checkbox]{margin:0;width:auto}.remember-me label{color:var(--color-gray-700);font-size:.875rem;margin:0}button[type=submit]{align-items:center;background-color:var(--color-primary);border:1px solid var(--color-primary);border-radius:8px;color:var(--color-white);cursor:pointer;display:flex;font-size:.875rem;font-weight:500;gap:.5rem;justify-content:center;min-height:44px;padding:.75em 1em;transition:all .2s ease-in-out;width:100%}button[type=submit]:hover:not(:disabled){background-color:var(--color-primary-dark);border-color:var(--color-primary-dark)}button[type=submit]:disabled{cursor:not-allowed;opacity:.6}.loading-spinner{animation:spin 1s linear infinite;border:2px solid var(--color-white);border-radius:50%;border-top:2px solid transparent;height:16px;width:16px}@keyframes spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}.login-footer{margin-top:1em;text-align:center}.forgot-password{margin:0}.forgot-link{align-items:center;color:var(--color-primary);display:inline-flex;font-size:.875rem;gap:.5rem;text-decoration:none;transition:color .2s ease-in-out}.forgot-link:hover:not(.loading){color:var(--color-primary-dark);text-decoration:underline}.forgot-link.loading{cursor:not-allowed;opacity:.6;pointer-events:none}.error-message{background-color:var(--color-error-light);border:1px solid var(--color-error-border);border-radius:8px;color:var(--color-error)}.error-message,.success-message{font-size:.875rem;margin-top:1em;padding:.75em 1em;text-align:center}.success-message{background-color:var(--color-success-light);border:1px solid var(--color-success-border);border-radius:8px;color:var(--color-success)}@media (max-width:480px){.login-form-container{margin:1em;padding:1.5em}.login-title{font-size:1.5rem}}
145
194
  </style>
@@ -1,5 +1,7 @@
1
1
  import type { LoginFormData, LoginFormProps, User } from '../../types.js';
2
2
  declare var __VLS_1: {}, __VLS_3: {}, __VLS_5: {}, __VLS_7: {}, __VLS_9: {}, __VLS_11: {}, __VLS_13: {
3
+ message: string;
4
+ }, __VLS_15: {
3
5
  error: string;
4
6
  };
5
7
  type __VLS_Slots = {} & {
@@ -15,19 +17,26 @@ type __VLS_Slots = {} & {
15
17
  } & {
16
18
  footer?: (props: typeof __VLS_11) => any;
17
19
  } & {
18
- 'error-message'?: (props: typeof __VLS_13) => any;
20
+ 'success-message'?: (props: typeof __VLS_13) => any;
21
+ } & {
22
+ 'error-message'?: (props: typeof __VLS_15) => any;
19
23
  };
20
24
  declare const __VLS_component: import("vue").DefineComponent<LoginFormProps, void, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {} & {
21
25
  error: (error: string) => any;
22
26
  submit: (data: LoginFormData) => any;
23
27
  success: (user: User) => any;
28
+ "forgot-password-error": (error: string) => any;
29
+ "forgot-password-success": () => any;
24
30
  }, string, import("vue").PublicProps, Readonly<LoginFormProps> & Readonly<{
25
31
  onError?: ((error: string) => any) | undefined;
26
32
  onSubmit?: ((data: LoginFormData) => any) | undefined;
27
33
  onSuccess?: ((user: User) => any) | undefined;
34
+ "onForgot-password-error"?: ((error: string) => any) | undefined;
35
+ "onForgot-password-success"?: (() => any) | undefined;
28
36
  }>, {
29
37
  apiEndpoint: string;
30
38
  redirectTo: string;
39
+ forgotPasswordEndpoint: string;
31
40
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
32
41
  declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
33
42
  export default _default;
@@ -0,0 +1,62 @@
1
+ <script setup>
2
+ import { ref } from "vue";
3
+ import { navigateTo } from "#app";
4
+ import { useAuth } from "../composables/useAuth";
5
+ const props = defineProps({
6
+ redirectTo: { type: String, required: false, default: "/login" },
7
+ linkText: { type: String, required: false, default: "Logout" },
8
+ confirmMessage: { type: String, required: false, default: "Are you sure you want to logout?" },
9
+ class: { type: String, required: false }
10
+ });
11
+ const emit = defineEmits(["success", "click", "error"]);
12
+ const { logout } = useAuth();
13
+ const isLoading = ref(false);
14
+ const error = ref("");
15
+ const handleLogout = async (event) => {
16
+ event.preventDefault();
17
+ if (!confirm(props.confirmMessage)) {
18
+ return;
19
+ }
20
+ isLoading.value = true;
21
+ error.value = "";
22
+ try {
23
+ emit("click");
24
+ await logout();
25
+ emit("success");
26
+ if (props.redirectTo) {
27
+ await navigateTo(props.redirectTo);
28
+ }
29
+ } catch (err) {
30
+ const errorMessage = err instanceof Error ? err.message : "Logout failed";
31
+ error.value = errorMessage;
32
+ emit("error", errorMessage);
33
+ } finally {
34
+ isLoading.value = false;
35
+ }
36
+ };
37
+ </script>
38
+
39
+ <template>
40
+ <div>
41
+ <div
42
+ v-if="error"
43
+ class="error-message"
44
+ >
45
+ {{ error }}
46
+ </div>
47
+
48
+ <a
49
+ href="#"
50
+ :class="['logout-link', { loading: isLoading }, props.class]"
51
+ :disabled="isLoading"
52
+ @click="handleLogout"
53
+ >
54
+ <span v-if="isLoading">Logging out...</span>
55
+ <span v-else>{{ linkText }}</span>
56
+ </a>
57
+ </div>
58
+ </template>
59
+
60
+ <style scoped>
61
+ .logout-link{color:#dc3545;cursor:pointer;font-size:.875rem;font-weight:500;text-decoration:none;transition:color .2s}.logout-link:hover:not(.loading){color:#c82333;text-decoration:underline}.logout-link.loading{cursor:not-allowed;opacity:.6;pointer-events:none}.error-message{color:#dc3545;font-size:.875rem;margin-bottom:.5rem}
62
+ </style>
@@ -0,0 +1,20 @@
1
+ interface LogoutLinkProps {
2
+ redirectTo?: string;
3
+ linkText?: string;
4
+ confirmMessage?: string;
5
+ class?: string;
6
+ }
7
+ declare const _default: import("vue").DefineComponent<LogoutLinkProps, void, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {} & {
8
+ click: () => any;
9
+ error: (error: string) => any;
10
+ success: () => any;
11
+ }, string, import("vue").PublicProps, Readonly<LogoutLinkProps> & Readonly<{
12
+ onClick?: (() => any) | undefined;
13
+ onError?: ((error: string) => any) | undefined;
14
+ onSuccess?: (() => any) | undefined;
15
+ }>, {
16
+ redirectTo: string;
17
+ linkText: string;
18
+ confirmMessage: string;
19
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
20
+ export default _default;
@@ -1,4 +1,5 @@
1
1
  import type { User } from '../../types.js';
2
2
  export declare const useAuth: () => {
3
3
  user: import("vue").Ref<User | null, User | null>;
4
+ logout: () => Promise<void>;
4
5
  };
@@ -1,7 +1,19 @@
1
1
  import { useState } from "#app";
2
2
  export const useAuth = () => {
3
3
  const user = useState("user", () => null);
4
+ const logout = async () => {
5
+ try {
6
+ await $fetch("/api/logout", {
7
+ method: "GET"
8
+ });
9
+ user.value = null;
10
+ } catch (error) {
11
+ console.error("Logout failed:", error);
12
+ user.value = null;
13
+ }
14
+ };
4
15
  return {
5
- user
16
+ user,
17
+ logout
6
18
  };
7
19
  };
@@ -1,5 +1,6 @@
1
- import { defineNuxtPlugin, useRuntimeConfig } from "#app";
1
+ import { addRouteMiddleware, defineNuxtPlugin, useRuntimeConfig, navigateTo } from "#app";
2
2
  import { checkTableExists } from "../utils";
3
+ import { useAuth } from "./composables/useAuth.js";
3
4
  export default defineNuxtPlugin(async (_nuxtApp) => {
4
5
  const { nuxtUsers } = useRuntimeConfig();
5
6
  const options = nuxtUsers;
@@ -7,4 +8,20 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
7
8
  if (!hasMigrationsTable) {
8
9
  console.warn("[Nuxt Users] \u26A0\uFE0F Migrations table does not exist, you should run the migration script to create it by running: npx nuxt-users migrate");
9
10
  }
11
+ addRouteMiddleware(
12
+ "auth.global",
13
+ (to, from) => {
14
+ if (to.path === "/login") {
15
+ return;
16
+ }
17
+ const { user } = useAuth();
18
+ if (user.value || nuxtUsers.auth?.whitelist?.includes(to.path)) {
19
+ console.log("[Nuxt Users] \u{1F510} Auth middleware: Access allowed");
20
+ return;
21
+ }
22
+ console.log(`[Nuxt Users] \u{1F510} Auth middleware: Redirecting from ${from.path} to login - no user and not whitelisted`);
23
+ return navigateTo("/login");
24
+ },
25
+ { global: true }
26
+ );
10
27
  });
@@ -46,7 +46,7 @@ export default defineEventHandler(async (event) => {
46
46
  sameSite: "lax",
47
47
  // Adjust as needed
48
48
  maxAge: 60 * 60 * 24 * 7,
49
- // Example: 7 days
49
+ // 7 days
50
50
  path: "/"
51
51
  });
52
52
  const { password: _, ...userWithoutPassword } = user;
@@ -0,0 +1,4 @@
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<{
2
+ message: string;
3
+ }>>;
4
+ export default _default;
@@ -0,0 +1,21 @@
1
+ import { defineEventHandler, getCookie, setCookie } from "h3";
2
+ import { useRuntimeConfig } from "#imports";
3
+ import { useDb } from "../../../utils";
4
+ export default defineEventHandler(async (event) => {
5
+ const { nuxtUsers } = useRuntimeConfig();
6
+ const options = nuxtUsers;
7
+ const db = await useDb(options);
8
+ const personalAccessTokensTable = options.tables.personalAccessTokens;
9
+ const token = getCookie(event, "auth_token");
10
+ if (token) {
11
+ await db.sql`DELETE FROM {${personalAccessTokensTable}} WHERE token = ${token}`;
12
+ }
13
+ setCookie(event, "auth_token", "", {
14
+ httpOnly: true,
15
+ secure: process.env.NODE_ENV === "production",
16
+ sameSite: "lax",
17
+ maxAge: 0,
18
+ path: "/"
19
+ });
20
+ return { message: "Logged out successfully" };
21
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-users",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "A comprehensive user management module for Nuxt 3 applications with authentication, database support, and CLI tools",
5
5
  "keywords": [
6
6
  "nuxt",
@@ -1,2 +0,0 @@
1
- declare const _default: import("#app").RouteMiddleware;
2
- export default _default;
@@ -1,12 +0,0 @@
1
- import { defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo } from "#app";
2
- import { useAuth } from "../composables/useAuth.js";
3
- export default defineNuxtRouteMiddleware((to, _from) => {
4
- const { nuxtUsers } = useRuntimeConfig();
5
- const { user } = useAuth();
6
- if (to.path === "/login") {
7
- return;
8
- }
9
- if (!user.value && !nuxtUsers.auth?.whitelist?.includes(to.path)) {
10
- return navigateTo("/login");
11
- }
12
- });