nuxt-bearer-auth 0.1.4 → 0.1.6

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.d.mts CHANGED
@@ -42,6 +42,18 @@ declare const module = defineNuxtModule({
42
42
  addCsrfTokenToEventCtx: true,
43
43
  headerName: options.csrf.headerName
44
44
  });
45
+ const prefix2 = options.routes?.localApiPrefix || "/api/auth";
46
+ nuxt.options.routeRules = {
47
+ ...nuxt.options.routeRules,
48
+ [`${prefix2}/login`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/login`] },
49
+ [`${prefix2}/register`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/register`] },
50
+ [`${prefix2}/social-login`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/social-login`] },
51
+ [`${prefix2}/forgot-password`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/forgot-password`] },
52
+ [`${prefix2}/reset-password`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/reset-password`] },
53
+ [`${prefix2}/otp-verification`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/otp-verification`] },
54
+ [`${prefix2}/resend-otp/**`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/resend-otp/**`] },
55
+ "/api/_csrf": { csurf: false, ...nuxt.options.routeRules?.["/api/_csrf"] }
56
+ };
45
57
  }
46
58
  addImports([
47
59
  {
package/dist/module.d.ts CHANGED
@@ -42,6 +42,18 @@ declare const module = defineNuxtModule({
42
42
  addCsrfTokenToEventCtx: true,
43
43
  headerName: options.csrf.headerName
44
44
  });
45
+ const prefix2 = options.routes?.localApiPrefix || "/api/auth";
46
+ nuxt.options.routeRules = {
47
+ ...nuxt.options.routeRules,
48
+ [`${prefix2}/login`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/login`] },
49
+ [`${prefix2}/register`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/register`] },
50
+ [`${prefix2}/social-login`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/social-login`] },
51
+ [`${prefix2}/forgot-password`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/forgot-password`] },
52
+ [`${prefix2}/reset-password`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/reset-password`] },
53
+ [`${prefix2}/otp-verification`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/otp-verification`] },
54
+ [`${prefix2}/resend-otp/**`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/resend-otp/**`] },
55
+ "/api/_csrf": { csurf: false, ...nuxt.options.routeRules?.["/api/_csrf"] }
56
+ };
45
57
  }
46
58
  addImports([
47
59
  {
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": "^3.12.0 || ^4.0.0"
6
6
  },
7
- "version": "0.1.4",
7
+ "version": "0.1.6",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "0.8.4",
10
10
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -130,6 +130,18 @@ const module = defineNuxtModule({
130
130
  addCsrfTokenToEventCtx: true,
131
131
  headerName: options.csrf.headerName
132
132
  });
133
+ const prefix2 = options.routes?.localApiPrefix || "/api/auth";
134
+ nuxt.options.routeRules = {
135
+ ...nuxt.options.routeRules,
136
+ [`${prefix2}/login`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/login`] },
137
+ [`${prefix2}/register`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/register`] },
138
+ [`${prefix2}/social-login`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/social-login`] },
139
+ [`${prefix2}/forgot-password`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/forgot-password`] },
140
+ [`${prefix2}/reset-password`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/reset-password`] },
141
+ [`${prefix2}/otp-verification`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/otp-verification`] },
142
+ [`${prefix2}/resend-otp/**`]: { csurf: false, ...nuxt.options.routeRules?.[`${prefix2}/resend-otp/**`] },
143
+ "/api/_csrf": { csurf: false, ...nuxt.options.routeRules?.["/api/_csrf"] }
144
+ };
133
145
  }
134
146
  addImports([
135
147
  {
@@ -1,12 +1,23 @@
1
- import { navigateTo, useRuntimeConfig, useState } from "#app";
1
+ import { navigateTo, useNuxtApp, useRuntimeConfig, useState } from "#app";
2
2
  import { computed, watch } from "vue";
3
3
  import { $fetch } from "ofetch";
4
4
  let serverCheckPromise = null;
5
5
  let serverCheckResolve = null;
6
6
  async function authFetch(request, options = {}) {
7
7
  try {
8
- const csrf = typeof useCsrf === "function" ? useCsrf() : null;
9
- const fetcher = csrf?.csrfFetch || $fetch;
8
+ let fetcher = $fetch;
9
+ if (import.meta.client) {
10
+ try {
11
+ const nuxtApp = useNuxtApp();
12
+ const $csrfFetch = nuxtApp.$csrfFetch;
13
+ if ($csrfFetch) {
14
+ fetcher = $csrfFetch;
15
+ } else if (typeof useCsrf === "function") {
16
+ fetcher = useCsrf()?.csrfFetch || $fetch;
17
+ }
18
+ } catch {
19
+ }
20
+ }
10
21
  return await fetcher(request, options);
11
22
  } catch (error) {
12
23
  throw error;
@@ -10,10 +10,33 @@ import { createBearerAuthSession } from "../../utils/sessions.js";
10
10
  export default defineEventHandler(async (event) => {
11
11
  try {
12
12
  const body = await readBody(event);
13
- if (!body.identifier || !body.password) {
13
+ if (!body || typeof body !== "object" || Object.keys(body).length === 0) {
14
14
  throw createError({
15
15
  statusCode: 422,
16
- statusMessage: "Identifier and password are required"
16
+ statusMessage: "Login credentials are required"
17
+ });
18
+ }
19
+ const hasCustomIdentifier = Object.keys(body).some(
20
+ (key) => key !== "password" && key !== "pass" && key !== "remember" && key !== "rememberMe" && Boolean(body[key])
21
+ );
22
+ const identifier = body.identifier ?? body.email ?? body.username ?? body.phone ?? body.mobile ?? body.phone_number ?? (hasCustomIdentifier ? true : void 0);
23
+ const password = body.password ?? body.pass;
24
+ if (!identifier && !password) {
25
+ throw createError({
26
+ statusCode: 422,
27
+ statusMessage: "Login credentials are required. Provide an identifier (e.g. email, username, phone, mobile, or identifier) and a password."
28
+ });
29
+ }
30
+ if (!identifier) {
31
+ throw createError({
32
+ statusCode: 422,
33
+ statusMessage: "Login identifier is required (e.g. email, username, phone, mobile, or identifier)."
34
+ });
35
+ }
36
+ if (!password) {
37
+ throw createError({
38
+ statusCode: 422,
39
+ statusMessage: "Password is required."
17
40
  });
18
41
  }
19
42
  const response = await callAuthApi(getAuthEndpoint("login"), {
@@ -6,10 +6,30 @@ import { createBearerAuthSession } from "../../utils/sessions.js";
6
6
  export default defineEventHandler(async (event) => {
7
7
  try {
8
8
  const body = await readBody(event);
9
- if (!body.otp || !body.identifier) {
9
+ if (!body || typeof body !== "object" || Object.keys(body).length === 0) {
10
10
  throw createError({
11
11
  statusCode: 422,
12
- statusMessage: "OTP and identifier are required"
12
+ statusMessage: "OTP verification credentials are required"
13
+ });
14
+ }
15
+ const otp = body.otp ?? body.code;
16
+ const identifier = body.identifier ?? body.email ?? body.phone ?? body.mobile ?? body.username ?? body.phone_number;
17
+ if (!otp && !identifier) {
18
+ throw createError({
19
+ statusCode: 422,
20
+ statusMessage: "OTP code and identifier (e.g. email, phone, mobile, username, or identifier) are required."
21
+ });
22
+ }
23
+ if (!otp) {
24
+ throw createError({
25
+ statusCode: 422,
26
+ statusMessage: "OTP code is required."
27
+ });
28
+ }
29
+ if (!identifier) {
30
+ throw createError({
31
+ statusCode: 422,
32
+ statusMessage: "Identifier is required for OTP verification (e.g. email, phone, mobile, username, or identifier)."
13
33
  });
14
34
  }
15
35
  const response = await callAuthApi(getAuthEndpoint("verifyOtp"), {
@@ -33,8 +33,12 @@ export interface AuthApiResponse<T = unknown> {
33
33
  nextAction?: string;
34
34
  }
35
35
  export interface LoginCredentials {
36
- identifier: string;
37
- password: string;
36
+ identifier?: string;
37
+ email?: string;
38
+ username?: string;
39
+ phone?: string;
40
+ mobile?: string;
41
+ password?: string;
38
42
  [key: string]: unknown;
39
43
  }
40
44
  export interface SocialLoginCredentials {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-bearer-auth",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Reusable Nuxt authentication module for bearer-token APIs with Redis sessions and CSRF protection.",
5
5
  "type": "module",
6
6
  "license": "MIT",