nuxt-auth-utils 0.3.4 → 0.3.5

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
@@ -2,6 +2,6 @@ import * as _nuxt_schema from '@nuxt/schema';
2
2
 
3
3
  interface ModuleOptions {
4
4
  }
5
- declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
5
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
6
6
 
7
7
  export { type ModuleOptions, _default as default };
package/dist/module.d.ts CHANGED
@@ -2,6 +2,6 @@ import * as _nuxt_schema from '@nuxt/schema';
2
2
 
3
3
  interface ModuleOptions {
4
4
  }
5
- declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
5
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
6
6
 
7
7
  export { type ModuleOptions, _default as default };
package/dist/module.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.3.4",
4
+ "version": "0.3.5",
5
5
  "builder": {
6
- "@nuxt/module-builder": "0.8.1",
6
+ "@nuxt/module-builder": "0.8.3",
7
7
  "unbuild": "2.0.0"
8
8
  }
9
9
  }
@@ -1,6 +1,8 @@
1
1
  import { defineNuxtPlugin, useUserSession } from "#imports";
2
2
  export default defineNuxtPlugin(async (nuxtApp) => {
3
- if (!nuxtApp.payload.serverRendered || Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
3
+ if (!nuxtApp.payload.serverRendered) {
4
+ await useUserSession().fetch();
5
+ } else if (Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
4
6
  nuxtApp.hook("app:mounted", async () => {
5
7
  await useUserSession().fetch();
6
8
  });
@@ -1,6 +1,7 @@
1
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
1
+ import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parsePath } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthAuth0EventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -9,12 +10,7 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }) {
9
10
  });
10
11
  const { code } = getQuery(event);
11
12
  if (!config.clientId || !config.clientSecret || !config.domain) {
12
- const error = createError({
13
- statusCode: 500,
14
- message: "Missing NUXT_OAUTH_AUTH0_CLIENT_ID or NUXT_OAUTH_AUTH0_CLIENT_SECRET or NUXT_OAUTH_AUTH0_DOMAIN env variables."
15
- });
16
- if (!onError) throw error;
17
- return onError(event, error);
13
+ return handleMissingConfiguration(event, "auth0", ["clientId", "clientSecret", "domain"], onError);
18
14
  }
19
15
  const authorizationURL = `https://${config.domain}/authorize`;
20
16
  const tokenURL = `https://${config.domain}/oauth/token`;
@@ -57,13 +53,7 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }) {
57
53
  return { error };
58
54
  });
59
55
  if (tokens.error) {
60
- const error = createError({
61
- statusCode: 401,
62
- message: `Auth0 login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
63
- data: tokens
64
- });
65
- if (!onError) throw error;
66
- return onError(event, error);
56
+ return handleAccessTokenErrorResponse(event, "auth0", tokens, onError);
67
57
  }
68
58
  const tokenType = tokens.token_type;
69
59
  const accessToken = tokens.access_token;
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
2
2
  import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
3
3
  import { withQuery, parsePath } from "ufo";
4
4
  import { defu } from "defu";
5
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
5
6
  import { useRuntimeConfig } from "#imports";
6
7
  export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
7
8
  return eventHandler(async (event) => {
@@ -22,12 +23,12 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
22
23
  return onError(event, error);
23
24
  }
24
25
  if (!config.clientId || !config.clientSecret) {
25
- const error = createError({
26
- statusCode: 500,
27
- message: "Missing NUXT_OAUTH_BATTLEDOTNET_CLIENT_ID or NUXT_OAUTH_BATTLEDOTNET_CLIENT_SECRET env variables."
28
- });
29
- if (!onError) throw error;
30
- return onError(event, error);
26
+ return handleMissingConfiguration(
27
+ event,
28
+ "battledotnet",
29
+ ["clientId", "clientSecret"],
30
+ onError
31
+ );
31
32
  }
32
33
  const redirectURL = config.redirectURL || getRequestURL(event).href;
33
34
  if (!code) {
@@ -74,13 +75,7 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
74
75
  return { error };
75
76
  });
76
77
  if (tokens.error) {
77
- const error = createError({
78
- statusCode: 401,
79
- message: `Battle.net login failed: ${tokens.error || "Unknown error"}`,
80
- data: tokens
81
- });
82
- if (!onError) throw error;
83
- return onError(event, error);
78
+ return handleAccessTokenErrorResponse(event, "battle.net", tokens, onError);
84
79
  }
85
80
  const accessToken = tokens.access_token;
86
81
  const user = await $fetch("https://oauth.battle.net/userinfo", {
@@ -35,5 +35,10 @@ export interface OAuthCognitoConfig {
35
35
  * @default process.env.NUXT_OAUTH_COGNITO_REDIRECT_URL or current URL
36
36
  */
37
37
  redirectURL?: string;
38
+ /**
39
+ * AWS Cognito App Custom Domain – some pool configurations require this
40
+ * @default ''
41
+ */
42
+ domain?: string;
38
43
  }
39
44
  export declare function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthCognitoConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
@@ -1,6 +1,7 @@
1
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
1
+ import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parsePath } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthCognitoEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -9,15 +10,11 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }) {
9
10
  });
10
11
  const { code } = getQuery(event);
11
12
  if (!config.clientId || !config.clientSecret || !config.userPoolId || !config.region) {
12
- const error = createError({
13
- statusCode: 500,
14
- message: "Missing NUXT_OAUTH_COGNITO_CLIENT_ID, NUXT_OAUTH_COGNITO_CLIENT_SECRET, NUXT_OAUTH_COGNITO_USER_POOL_ID, or NUXT_OAUTH_COGNITO_REGION env variables."
15
- });
16
- if (!onError) throw error;
17
- return onError(event, error);
13
+ return handleMissingConfiguration(event, "cognito", ["clientId", "clientSecret", "userPoolId", "region"], onError);
18
14
  }
19
- const authorizationURL = `https://${config.userPoolId}.auth.${config.region}.amazoncognito.com/oauth2/authorize`;
20
- const tokenURL = `https://${config.userPoolId}.auth.${config.region}.amazoncognito.com/oauth2/token`;
15
+ const urlBase = config?.domain || `${config.userPoolId}.auth.${config.region}.amazoncognito.com`;
16
+ const authorizationURL = `https://${urlBase}/oauth2/authorize`;
17
+ const tokenURL = `https://${urlBase}/oauth2/token`;
21
18
  const redirectURL = config.redirectURL || getRequestURL(event).href;
22
19
  if (!code) {
23
20
  config.scope = config.scope || ["openid", "profile"];
@@ -45,17 +42,11 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }) {
45
42
  return { error };
46
43
  });
47
44
  if (tokens.error) {
48
- const error = createError({
49
- statusCode: 401,
50
- message: `Cognito login failed: ${tokens.error_description || "Unknown error"}`,
51
- data: tokens
52
- });
53
- if (!onError) throw error;
54
- return onError(event, error);
45
+ return handleAccessTokenErrorResponse(event, "cognito", tokens, onError);
55
46
  }
56
47
  const tokenType = tokens.token_type;
57
48
  const accessToken = tokens.access_token;
58
- const user = await $fetch(`https://${config.userPoolId}.auth.${config.region}.amazoncognito.com/oauth2/userInfo`, {
49
+ const user = await $fetch(`https://${urlBase}/oauth2/userInfo`, {
59
50
  headers: {
60
51
  Authorization: `${tokenType} ${accessToken}`
61
52
  }
@@ -1,6 +1,7 @@
1
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
1
+ import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parseURL, stringifyParsedURL } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -12,12 +13,7 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
12
13
  });
13
14
  const { code } = getQuery(event);
14
15
  if (!config.clientId || !config.clientSecret) {
15
- const error = createError({
16
- statusCode: 500,
17
- message: "Missing NUXT_OAUTH_DISCORD_CLIENT_ID or NUXT_OAUTH_DISCORD_CLIENT_SECRET env variables."
18
- });
19
- if (!onError) throw error;
20
- return onError(event, error);
16
+ return handleMissingConfiguration(event, "discord", ["clientId", "clientSecret"], onError);
21
17
  }
22
18
  const redirectURL = config.redirectURL || getRequestURL(event).href;
23
19
  if (!code) {
@@ -60,13 +56,7 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
60
56
  return { error };
61
57
  });
62
58
  if (tokens.error) {
63
- const error = createError({
64
- statusCode: 401,
65
- message: `Discord login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
66
- data: tokens
67
- });
68
- if (!onError) throw error;
69
- return onError(event, error);
59
+ return handleAccessTokenErrorResponse(event, "discord", tokens, onError);
70
60
  }
71
61
  const accessToken = tokens.access_token;
72
62
  const user = await $fetch("https://discord.com/api/users/@me", {
@@ -7,6 +7,7 @@ import {
7
7
  } from "h3";
8
8
  import { withQuery } from "ufo";
9
9
  import { defu } from "defu";
10
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
10
11
  import { useRuntimeConfig } from "#imports";
11
12
  export function oauthFacebookEventHandler({
12
13
  config,
@@ -30,12 +31,7 @@ export function oauthFacebookEventHandler({
30
31
  return onError(event, error);
31
32
  }
32
33
  if (!config.clientId) {
33
- const error = createError({
34
- statusCode: 500,
35
- message: "Missing NUXT_OAUTH_FACEBOOK_CLIENT_ID or NUXT_OAUTH_FACEBOOK_CLIENT_SECRET env variables."
36
- });
37
- if (!onError) throw error;
38
- return onError(event, error);
34
+ return handleMissingConfiguration(event, "facebook", ["clientId"], onError);
39
35
  }
40
36
  const redirectURL = config.redirectURL || getRequestURL(event).href;
41
37
  if (!query.code) {
@@ -59,13 +55,7 @@ export function oauthFacebookEventHandler({
59
55
  }
60
56
  });
61
57
  if (tokens.error) {
62
- const error = createError({
63
- statusCode: 401,
64
- message: `Facebook login failed: ${tokens.error || "Unknown error"}`,
65
- data: tokens
66
- });
67
- if (!onError) throw error;
68
- return onError(event, error);
58
+ return handleAccessTokenErrorResponse(event, "facebook", tokens, onError);
69
59
  }
70
60
  const accessToken = tokens.access_token;
71
61
  config.fields = config.fields || ["id", "name"];
@@ -1,6 +1,7 @@
1
1
  import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -20,12 +21,7 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
20
21
  return onError(event, error);
21
22
  }
22
23
  if (!config.clientId || !config.clientSecret) {
23
- const error = createError({
24
- statusCode: 500,
25
- message: "Missing NUXT_OAUTH_GITHUB_CLIENT_ID or NUXT_OAUTH_GITHUB_CLIENT_SECRET env variables."
26
- });
27
- if (!onError) throw error;
28
- return onError(event, error);
24
+ return handleMissingConfiguration(event, "github", ["clientId", "clientSecret"], onError);
29
25
  }
30
26
  if (!query.code) {
31
27
  config.scope = config.scope || [];
@@ -55,13 +51,7 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
55
51
  }
56
52
  );
57
53
  if (tokens.error) {
58
- const error = createError({
59
- statusCode: 401,
60
- message: `GitHub login failed: ${tokens.error || "Unknown error"}`,
61
- data: tokens
62
- });
63
- if (!onError) throw error;
64
- return onError(event, error);
54
+ return handleAccessTokenErrorResponse(event, "github", tokens, onError);
65
55
  }
66
56
  const accessToken = tokens.access_token;
67
57
  const user = await $fetch("https://api.github.com/user", {
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  eventHandler,
3
- createError,
4
3
  getQuery,
5
4
  getRequestURL,
6
5
  sendRedirect
7
6
  } from "h3";
8
7
  import { withQuery, parsePath } from "ufo";
9
8
  import { defu } from "defu";
9
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
10
10
  import { useRuntimeConfig } from "#imports";
11
11
  export function oauthGoogleEventHandler({
12
12
  config,
@@ -22,12 +22,7 @@ export function oauthGoogleEventHandler({
22
22
  });
23
23
  const { code } = getQuery(event);
24
24
  if (!config.clientId) {
25
- const error = createError({
26
- statusCode: 500,
27
- message: "Missing NUXT_OAUTH_GOOGLE_CLIENT_ID env variables."
28
- });
29
- if (!onError) throw error;
30
- return onError(event, error);
25
+ return handleMissingConfiguration(event, "google", ["clientId"], onError);
31
26
  }
32
27
  const redirectURL = config.redirectURL || getRequestURL(event).href;
33
28
  if (!code) {
@@ -57,13 +52,7 @@ export function oauthGoogleEventHandler({
57
52
  return { error };
58
53
  });
59
54
  if (tokens.error) {
60
- const error = createError({
61
- statusCode: 401,
62
- message: `Google login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
63
- data: tokens
64
- });
65
- if (!onError) throw error;
66
- return onError(event, error);
55
+ return handleAccessTokenErrorResponse(event, "google", tokens, onError);
67
56
  }
68
57
  const accessToken = tokens.access_token;
69
58
  const user = await $fetch(
@@ -7,6 +7,7 @@ import {
7
7
  } from "h3";
8
8
  import { withQuery, parsePath } from "ufo";
9
9
  import { defu } from "defu";
10
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
10
11
  import { useRuntimeConfig } from "#imports";
11
12
  export function oauthKeycloakEventHandler({
12
13
  config,
@@ -29,12 +30,7 @@ export function oauthKeycloakEventHandler({
29
30
  return onError(event, error);
30
31
  }
31
32
  if (!config.clientId || !config.clientSecret || !config.serverUrl || !config.realm) {
32
- const error = createError({
33
- statusCode: 500,
34
- message: "Missing NUXT_OAUTH_KEYCLOAK_CLIENT_ID or NUXT_OAUTH_KEYCLOAK_CLIENT_SECRET or NUXT_OAUTH_KEYCLOAK_SERVER_URL or NUXT_OAUTH_KEYCLOAK_REALM env variables."
35
- });
36
- if (!onError) throw error;
37
- return onError(event, error);
33
+ return handleMissingConfiguration(event, "keycloak", ["clientId", "clientSecret", "serverUrl", "realm"], onError);
38
34
  }
39
35
  const realmURL = `${config.serverUrl}/realms/${config.realm}`;
40
36
  const authorizationURL = `${realmURL}/protocol/openid-connect/auth`;
@@ -73,13 +69,7 @@ export function oauthKeycloakEventHandler({
73
69
  return { error };
74
70
  });
75
71
  if (tokens.error) {
76
- const error = createError({
77
- statusCode: 401,
78
- message: `Keycloak login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
79
- data: tokens
80
- });
81
- if (!onError) throw error;
82
- return onError(event, error);
72
+ return handleAccessTokenErrorResponse(event, "keycloak", tokens, onError);
83
73
  }
84
74
  const accessToken = tokens.access_token;
85
75
  const user = await $fetch(
@@ -1,6 +1,7 @@
1
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
1
+ import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parseURL, stringifyParsedURL } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -11,12 +12,7 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
11
12
  });
12
13
  const { code } = getQuery(event);
13
14
  if (!config.clientId || !config.clientSecret) {
14
- const error = createError({
15
- statusCode: 500,
16
- message: "Missing NUXT_OAUTH_LINKEDIN_CLIENT_ID or NUXT_OAUTH_LINKEDIN_CLIENT_SECRET env variables."
17
- });
18
- if (!onError) throw error;
19
- return onError(event, error);
15
+ return handleMissingConfiguration(event, "linkedin", ["clientId", "clientSecret"], onError);
20
16
  }
21
17
  const redirectURL = config.redirectURL || getRequestURL(event).href;
22
18
  if (!code) {
@@ -59,13 +55,7 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
59
55
  return { error };
60
56
  });
61
57
  if (tokens.error) {
62
- const error = createError({
63
- statusCode: 401,
64
- message: `LinkedIn login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
65
- data: tokens
66
- });
67
- if (!onError) throw error;
68
- return onError(event, error);
58
+ return handleAccessTokenErrorResponse(event, "linkedin", tokens, onError);
69
59
  }
70
60
  const accessToken = tokens.access_token;
71
61
  const user = await $fetch("https://api.linkedin.com/v2/userinfo", {
@@ -1,6 +1,7 @@
1
1
  import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parsePath } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthMicrosoftEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -9,12 +10,7 @@ export function oauthMicrosoftEventHandler({ config, onSuccess, onError }) {
9
10
  });
10
11
  const { code } = getQuery(event);
11
12
  if (!config.clientId || !config.clientSecret || !config.tenant) {
12
- const error = createError({
13
- statusCode: 500,
14
- message: "Missing NUXT_OAUTH_MICROSOFT_CLIENT_ID or NUXT_OAUTH_MICROSOFT_CLIENT_SECRET or NUXT_OAUTH_MICROSOFT_TENANT env variables."
15
- });
16
- if (!onError) throw error;
17
- return onError(event, error);
13
+ return handleMissingConfiguration(event, "microsoft", ["clientId", "clientSecret", "tenant"], onError);
18
14
  }
19
15
  const authorizationURL = config.authorizationURL || `https://login.microsoftonline.com/${config.tenant}/oauth2/v2.0/authorize`;
20
16
  const tokenURL = config.tokenURL || `https://login.microsoftonline.com/${config.tenant}/oauth2/v2.0/token`;
@@ -51,13 +47,7 @@ export function oauthMicrosoftEventHandler({ config, onSuccess, onError }) {
51
47
  return { error };
52
48
  });
53
49
  if (tokens.error) {
54
- const error = createError({
55
- statusCode: 401,
56
- message: `Microsoft login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
57
- data: tokens
58
- });
59
- if (!onError) throw error;
60
- return onError(event, error);
50
+ return handleAccessTokenErrorResponse(event, "microsoft", tokens, onError);
61
51
  }
62
52
  const tokenType = tokens.token_type;
63
53
  const accessToken = tokens.access_token;
@@ -1,6 +1,7 @@
1
1
  import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parsePath } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthPaypalEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -12,12 +13,7 @@ export function oauthPaypalEventHandler({ config, onSuccess, onError }) {
12
13
  });
13
14
  const { code } = getQuery(event);
14
15
  if (!config.clientId) {
15
- const error = createError({
16
- statusCode: 500,
17
- message: "Missing NUXT_OAUTH_PAYPAL_CLIENT_ID env variables."
18
- });
19
- if (!onError) throw error;
20
- return onError(event, error);
16
+ return handleMissingConfiguration(event, "paypal", ["clientId"], onError);
21
17
  }
22
18
  let paypalAPI = "api-m.paypal.com";
23
19
  if (config.sandbox) {
@@ -65,13 +61,7 @@ export function oauthPaypalEventHandler({ config, onSuccess, onError }) {
65
61
  return { error };
66
62
  });
67
63
  if (tokens.error) {
68
- const error = createError({
69
- statusCode: 401,
70
- message: `PayPal login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
71
- data: tokens
72
- });
73
- if (!onError) throw error;
74
- return onError(event, error);
64
+ return handleAccessTokenErrorResponse(event, "paypal", tokens, onError);
75
65
  }
76
66
  const accessToken = tokens.access_token;
77
67
  const users = await $fetch(`https://${paypalAPI}/v1/identity/openidconnect/userinfo`, {
@@ -1,6 +1,7 @@
1
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
1
+ import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parsePath } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthSpotifyEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -11,12 +12,7 @@ export function oauthSpotifyEventHandler({ config, onSuccess, onError }) {
11
12
  });
12
13
  const { code } = getQuery(event);
13
14
  if (!config.clientId || !config.clientSecret) {
14
- const error = createError({
15
- statusCode: 500,
16
- message: "Missing NUXT_OAUTH_SPOTIFY_CLIENT_ID or NUXT_OAUTH_SPOTIFY_CLIENT_SECRET env variables."
17
- });
18
- if (!onError) throw error;
19
- return onError(event, error);
15
+ return handleMissingConfiguration(event, "spotify", ["clientId", "clientSecret"], onError);
20
16
  }
21
17
  const redirectURL = config.redirectURL || getRequestURL(event).href;
22
18
  if (!code) {
@@ -54,13 +50,7 @@ export function oauthSpotifyEventHandler({ config, onSuccess, onError }) {
54
50
  return { error };
55
51
  });
56
52
  if (tokens.error) {
57
- const error = createError({
58
- statusCode: 401,
59
- message: `Spotify login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
60
- data: tokens
61
- });
62
- if (!onError) throw error;
63
- return onError(event, error);
53
+ return handleAccessTokenErrorResponse(event, "spotify", tokens, onError);
64
54
  }
65
55
  const accessToken = tokens.access_token;
66
56
  const user = await $fetch("https://api.spotify.com/v1/me", {
@@ -1,6 +1,7 @@
1
1
  import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthSteamEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -9,12 +10,7 @@ export function oauthSteamEventHandler({ config, onSuccess, onError }) {
9
10
  });
10
11
  const query = getQuery(event);
11
12
  if (!config.apiKey) {
12
- const error = createError({
13
- statusCode: 500,
14
- message: "Missing NUXT_OAUTH_STEAM_API_KEY env variable."
15
- });
16
- if (!onError) throw error;
17
- return onError(event, error);
13
+ return handleMissingConfiguration(event, "steam", ["apiKey"], onError);
18
14
  }
19
15
  if (!query["openid.claimed_id"]) {
20
16
  const redirectURL = config.redirectURL || getRequestURL(event).href;
@@ -1,6 +1,7 @@
1
1
  import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parsePath } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthTwitchEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
@@ -11,12 +12,7 @@ export function oauthTwitchEventHandler({ config, onSuccess, onError }) {
11
12
  });
12
13
  const { code } = getQuery(event);
13
14
  if (!config.clientId) {
14
- const error = createError({
15
- statusCode: 500,
16
- message: "Missing NUXT_OAUTH_TWITCH_CLIENT_ID env variables."
17
- });
18
- if (!onError) throw error;
19
- return onError(event, error);
15
+ return handleMissingConfiguration(event, "twitch", ["clientId"], onError);
20
16
  }
21
17
  const redirectURL = config.redirectURL || getRequestURL(event).href;
22
18
  if (!code) {
@@ -54,13 +50,7 @@ export function oauthTwitchEventHandler({ config, onSuccess, onError }) {
54
50
  return { error };
55
51
  });
56
52
  if (tokens.error) {
57
- const error = createError({
58
- statusCode: 401,
59
- message: `Twitch login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
60
- data: tokens
61
- });
62
- if (!onError) throw error;
63
- return onError(event, error);
53
+ return handleAccessTokenErrorResponse(event, "twitch", tokens, onError);
64
54
  }
65
55
  const accessToken = tokens.access_token;
66
56
  const users = await $fetch("https://api.twitch.tv/helix/users", {
@@ -17,11 +17,6 @@ export interface OAuthXConfig {
17
17
  * @example ['tweet.read', 'users.read', 'offline.access'],
18
18
  */
19
19
  scope?: string[];
20
- /**
21
- * Require email from user
22
- * @default false
23
- */
24
- emailRequired?: boolean;
25
20
  /**
26
21
  * X OAuth Authorization URL
27
22
  * @default 'https://twitter.com/i/oauth2/authorize'
@@ -41,7 +36,7 @@ export interface OAuthXConfig {
41
36
  * Extra authorization parameters to provide to the authorization URL
42
37
  * @see https://developer.x.com/en/docs/authentication/oauth-2-0/user-access-token
43
38
  */
44
- authorizationParams: Record<string, string>;
39
+ authorizationParams?: Record<string, string>;
45
40
  /**
46
41
  * Redirect URL to to allow overriding for situations like prod failing to determine public hostname
47
42
  * @default process.env.NUXT_OAUTH_X_REDIRECT_URL or current URL
@@ -1,13 +1,13 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import {
3
3
  eventHandler,
4
- createError,
5
4
  getQuery,
6
5
  getRequestURL,
7
6
  sendRedirect
8
7
  } from "h3";
9
8
  import { withQuery, parsePath } from "ufo";
10
9
  import { defu } from "defu";
10
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
11
11
  import { useRuntimeConfig } from "#imports";
12
12
  export function oauthXEventHandler({
13
13
  config,
@@ -16,9 +16,9 @@ export function oauthXEventHandler({
16
16
  }) {
17
17
  return eventHandler(async (event) => {
18
18
  config = defu(config, useRuntimeConfig(event).oauth?.x, {
19
- authorizationURL: "https://twitter.com/i/oauth2/authorize",
20
- tokenURL: "https://api.twitter.com/2/oauth2/token",
21
- userURL: "https://api.twitter.com/2/users/me",
19
+ authorizationURL: "https://x.com/i/oauth2/authorize",
20
+ tokenURL: "https://api.x.com/2/oauth2/token",
21
+ userURL: "https://api.x.com/2/users/me",
22
22
  authorizationParams: {
23
23
  state: randomUUID(),
24
24
  code_challenge: randomUUID()
@@ -26,12 +26,7 @@ export function oauthXEventHandler({
26
26
  });
27
27
  const { code } = getQuery(event);
28
28
  if (!config.clientId) {
29
- const error = createError({
30
- statusCode: 500,
31
- message: "Missing NUXT_OAUTH_X_CLIENT_ID env variables."
32
- });
33
- if (!onError) throw error;
34
- return onError(event, error);
29
+ return handleMissingConfiguration(event, "x", ["clientId"], onError);
35
30
  }
36
31
  const redirectURL = config.redirectURL || getRequestURL(event).href;
37
32
  if (!code) {
@@ -50,7 +45,7 @@ export function oauthXEventHandler({
50
45
  }
51
46
  const params = {
52
47
  grant_type: "authorization_code",
53
- code_verifier: config.authorizationParams.code_challenge,
48
+ code_verifier: config.authorizationParams?.code_challenge,
54
49
  redirect_uri: parsePath(redirectURL).pathname,
55
50
  code
56
51
  };
@@ -66,13 +61,7 @@ export function oauthXEventHandler({
66
61
  return { error };
67
62
  });
68
63
  if (tokens.error) {
69
- const error = createError({
70
- statusCode: 401,
71
- message: `X login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
72
- data: tokens
73
- });
74
- if (!onError) throw error;
75
- return onError(event, error);
64
+ return handleAccessTokenErrorResponse(event, "x", tokens, onError);
76
65
  }
77
66
  const accessToken = tokens.access_token;
78
67
  const user = await $fetch(
@@ -88,29 +77,6 @@ export function oauthXEventHandler({
88
77
  ).catch((error) => {
89
78
  return error;
90
79
  });
91
- if (config.emailRequired) {
92
- const emailData = await $fetch("https://api.twitter.com/1.1/account/verify_credentials.json", {
93
- headers: {
94
- Authorization: `Bearer ${accessToken}`
95
- },
96
- query: {
97
- include_email: "true",
98
- skip_status: "true"
99
- }
100
- }).catch((error) => {
101
- return error;
102
- });
103
- if (emailData && emailData.email) {
104
- user.email = emailData.email;
105
- } else {
106
- const error = createError({
107
- statusCode: 401,
108
- message: "Twitter login failed: no user email found"
109
- });
110
- if (!onError) throw error;
111
- return onError(event, error);
112
- }
113
- }
114
80
  return onSuccess(event, {
115
81
  tokens,
116
82
  user: user?.data
@@ -1,18 +1,14 @@
1
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
1
+ import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
2
  import { withQuery, parsePath } from "ufo";
3
3
  import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
5
  import { useRuntimeConfig } from "#imports";
5
6
  export function oauthXSUAAEventHandler({ config, onSuccess, onError }) {
6
7
  return eventHandler(async (event) => {
7
8
  config = defu(config, useRuntimeConfig(event).oauth?.xsuaa);
8
9
  const { code } = getQuery(event);
9
10
  if (!config.clientId || !config.clientSecret || !config.domain) {
10
- const error = createError({
11
- statusCode: 500,
12
- message: "Missing NUXT_OAUTH_XSUAA_CLIENT_ID or NUXT_OAUTH_XSUAA_CLIENT_SECRET or NUXT_OAUTH_XSUAA_DOMAIN env variables."
13
- });
14
- if (!onError) throw error;
15
- return onError(event, error);
11
+ return handleMissingConfiguration(event, "xsuaa", ["clientId", "clientSecret", "domain"], onError);
16
12
  }
17
13
  const authorizationURL = `https://${config.domain}/oauth/authorize`;
18
14
  const tokenURL = `https://${config.domain}/oauth/token`;
@@ -48,13 +44,7 @@ export function oauthXSUAAEventHandler({ config, onSuccess, onError }) {
48
44
  return { error };
49
45
  });
50
46
  if (tokens.error) {
51
- const error = createError({
52
- statusCode: 401,
53
- message: `XSUAA login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
54
- data: tokens
55
- });
56
- if (!onError) throw error;
57
- return onError(event, error);
47
+ return handleAccessTokenErrorResponse(event, "auth0", tokens, onError);
58
48
  }
59
49
  const tokenType = tokens.token_type;
60
50
  const accessToken = tokens.access_token;
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  eventHandler,
3
- createError,
4
3
  getQuery,
5
4
  getRequestURL,
6
5
  sendRedirect
7
6
  } from "h3";
8
7
  import { withQuery, parseURL, stringifyParsedURL } from "ufo";
9
8
  import { defu } from "defu";
9
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
10
10
  import { useRuntimeConfig } from "#imports";
11
11
  export function oauthYandexEventHandler({
12
12
  config,
@@ -21,12 +21,7 @@ export function oauthYandexEventHandler({
21
21
  });
22
22
  const { code } = getQuery(event);
23
23
  if (!config.clientId || !config.clientSecret) {
24
- const error = createError({
25
- statusCode: 500,
26
- message: "Missing NUXT_OAUTH_YANDEX_CLIENT_ID or NUXT_OAUTH_YANDEX_CLIENT_SECRET env variables."
27
- });
28
- if (!onError) throw error;
29
- return onError(event, error);
24
+ return handleMissingConfiguration(event, "yandex", ["clientId", "clientSecret"], onError);
30
25
  }
31
26
  const redirectURL = config.redirectURL || getRequestURL(event).href;
32
27
  if (!code) {
@@ -60,13 +55,7 @@ export function oauthYandexEventHandler({
60
55
  return { error };
61
56
  });
62
57
  if (tokens.error) {
63
- const error = createError({
64
- statusCode: 401,
65
- message: `Yandex login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
66
- data: tokens
67
- });
68
- if (!onError) throw error;
69
- return onError(event, error);
58
+ return handleAccessTokenErrorResponse(event, "yandex", tokens, onError);
70
59
  }
71
60
  const accessToken = tokens.access_token;
72
61
  const user = await $fetch(config.userURL, {
@@ -0,0 +1,9 @@
1
+ import type { H3Event } from 'h3';
2
+ import type { OnError, OAuthProvider } from '#auth-utils';
3
+ /**
4
+ * Handle OAuth access token error response
5
+ *
6
+ * @see https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
7
+ */
8
+ export declare function handleAccessTokenErrorResponse(event: H3Event, oauthProvider: OAuthProvider, oauthError: any, onError?: OnError): void | Promise<void>;
9
+ export declare function handleMissingConfiguration(event: H3Event, provider: OAuthProvider, missingKeys: string[], onError?: OnError): void | Promise<void>;
@@ -0,0 +1,21 @@
1
+ import { snakeCase, upperFirst } from "scule";
2
+ import { createError } from "#imports";
3
+ export function handleAccessTokenErrorResponse(event, oauthProvider, oauthError, onError) {
4
+ const message = `${upperFirst(oauthProvider)} login failed: ${oauthError.error_description || oauthError.error || "Unknown error"}`;
5
+ const error = createError({
6
+ statusCode: 401,
7
+ message,
8
+ data: oauthError
9
+ });
10
+ if (!onError) throw error;
11
+ return onError(event, error);
12
+ }
13
+ export function handleMissingConfiguration(event, provider, missingKeys, onError) {
14
+ const environmentVariables = missingKeys.map((key) => `NUXT_OAUTH_${provider.toUpperCase()}_${snakeCase(key).toUpperCase()}`);
15
+ const error = createError({
16
+ statusCode: 500,
17
+ message: `Missing ${environmentVariables.join(" or ")} env ${missingKeys.length > 1 ? "variables" : "variable"}.`
18
+ });
19
+ if (!onError) throw error;
20
+ return onError(event, error);
21
+ }
@@ -1,2 +1,2 @@
1
1
  export type { User, UserSession, UserSessionRequired, UserSessionComposable } from './session.js';
2
- export type { OAuthConfig } from './oauth-config.js';
2
+ export type { OAuthConfig, OAuthProvider, OnError } from './oauth-config.js';
@@ -1,9 +1,11 @@
1
1
  import type { H3Event, H3Error } from 'h3';
2
+ export type OAuthProvider = 'auth0' | 'battledotnet' | 'cognito' | 'discord' | 'facebook' | 'github' | 'google' | 'keycloak' | 'linkedin' | 'microsoft' | 'paypal' | 'spotify' | 'steam' | 'twitch' | 'x' | 'xsuaa' | 'yandex' | (string & {});
3
+ export type OnError = (event: H3Event, error: H3Error) => Promise<void> | void;
2
4
  export interface OAuthConfig<TConfig, TUser = any, TTokens = any> {
3
5
  config?: TConfig;
4
6
  onSuccess: (event: H3Event, result: {
5
7
  user: TUser;
6
8
  tokens: TTokens;
7
9
  }) => Promise<void> | void;
8
- onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
10
+ onError?: OnError;
9
11
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "Minimalist Auth module for Nuxt with SSR",
5
5
  "repository": "Atinux/nuxt-auth-utils",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
- "packageManager": "pnpm@9.7.0",
8
+ "packageManager": "pnpm@9.9.0",
9
9
  "exports": {
10
10
  ".": {
11
11
  "types": "./dist/types.d.ts",
@@ -32,26 +32,27 @@
32
32
  "test:watch": "vitest watch"
33
33
  },
34
34
  "dependencies": {
35
- "@nuxt/kit": "^3.12.4",
35
+ "@nuxt/kit": "^3.13.0",
36
36
  "defu": "^6.1.4",
37
37
  "hookable": "^5.5.3",
38
38
  "ofetch": "^1.3.4",
39
39
  "ohash": "^1.1.3",
40
40
  "pathe": "^1.1.2",
41
+ "scule": "^1.3.0",
41
42
  "uncrypto": "^0.1.3"
42
43
  },
43
44
  "devDependencies": {
44
- "@iconify-json/simple-icons": "^1.1.113",
45
+ "@iconify-json/simple-icons": "^1.1.115",
45
46
  "@nuxt/devtools": "latest",
46
- "@nuxt/eslint-config": "^0.5.0",
47
- "@nuxt/module-builder": "^0.8.1",
48
- "@nuxt/schema": "^3.12.4",
49
- "@nuxt/test-utils": "^3.14.0",
47
+ "@nuxt/eslint-config": "^0.5.3",
48
+ "@nuxt/module-builder": "^0.8.3",
49
+ "@nuxt/schema": "^3.13.0",
50
+ "@nuxt/test-utils": "^3.14.1",
50
51
  "@nuxt/ui": "^2.18.4",
51
52
  "@nuxt/ui-pro": "^1.4.1",
52
53
  "changelogen": "^0.5.5",
53
- "eslint": "^9.9.0",
54
- "nuxt": "^3.12.4",
54
+ "eslint": "^9.9.1",
55
+ "nuxt": "^3.13.0",
55
56
  "typescript": "^5.5.4",
56
57
  "vitest": "^2.0.5",
57
58
  "vue-tsc": "^2.0.29"