nuxt-auth-utils 0.3.5 → 0.3.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/README.md CHANGED
@@ -199,6 +199,7 @@ It can also be set using environment variables:
199
199
  - PayPal
200
200
  - Spotify
201
201
  - Steam
202
+ - TikTok
202
203
  - Twitch
203
204
  - X (Twitter)
204
205
  - XSUAA
@@ -250,7 +251,7 @@ export default defineNitroPlugin(() => {
250
251
  // throw createError({ ... }) if session is invalid for example
251
252
  })
252
253
 
253
- // Called when we call useServerSession().clear() or clearUserSession(event)
254
+ // Called when we call useUserSession().clear() or clearUserSession(event)
254
255
  sessionHooks.hook('clear', async (session, event) => {
255
256
  // Log that user logged out
256
257
  })
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.3.5",
4
+ "version": "0.3.6",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.3",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -149,6 +149,11 @@ const module = defineNuxtModule({
149
149
  clientSecret: "",
150
150
  redirectURL: ""
151
151
  });
152
+ runtimeConfig.oauth.tiktok = defu(runtimeConfig.oauth.tiktok, {
153
+ clientKey: "",
154
+ clientSecret: "",
155
+ redirectURL: ""
156
+ });
152
157
  }
153
158
  });
154
159
 
@@ -1,21 +1,21 @@
1
- import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
- import { withQuery, parsePath } from "ufo";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
- import { handleMissingConfiguration, handleAccessTokenErrorResponse } from "../utils.js";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
5
  import { useRuntimeConfig } from "#imports";
6
6
  export function oauthAuth0EventHandler({ config, onSuccess, onError }) {
7
7
  return eventHandler(async (event) => {
8
8
  config = defu(config, useRuntimeConfig(event).oauth?.auth0, {
9
9
  authorizationParams: {}
10
10
  });
11
- const { code } = getQuery(event);
12
11
  if (!config.clientId || !config.clientSecret || !config.domain) {
13
12
  return handleMissingConfiguration(event, "auth0", ["clientId", "clientSecret", "domain"], onError);
14
13
  }
15
14
  const authorizationURL = `https://${config.domain}/authorize`;
16
15
  const tokenURL = `https://${config.domain}/oauth/token`;
17
- const redirectURL = config.redirectURL || getRequestURL(event).href;
18
- if (!code) {
16
+ const query = getQuery(event);
17
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
18
+ if (!query.code) {
19
19
  config.scope = config.scope || ["openid", "offline_access"];
20
20
  if (config.emailRequired && !config.scope.includes("email")) {
21
21
  config.scope.push("email");
@@ -34,23 +34,17 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }) {
34
34
  })
35
35
  );
36
36
  }
37
- const tokens = await $fetch(
38
- tokenURL,
39
- {
40
- method: "POST",
41
- headers: {
42
- "Content-Type": "application/json"
43
- },
44
- body: {
45
- grant_type: "authorization_code",
46
- client_id: config.clientId,
47
- client_secret: config.clientSecret,
48
- redirect_uri: parsePath(redirectURL).pathname,
49
- code
50
- }
37
+ const tokens = await requestAccessToken(tokenURL, {
38
+ headers: {
39
+ "Content-Type": "application/json"
40
+ },
41
+ body: {
42
+ grant_type: "authorization_code",
43
+ client_id: config.clientId,
44
+ client_secret: config.clientSecret,
45
+ redirect_uri: redirectURL,
46
+ code: query.code
51
47
  }
52
- ).catch((error) => {
53
- return { error };
54
48
  });
55
49
  if (tokens.error) {
56
50
  return handleAccessTokenErrorResponse(event, "auth0", tokens, onError);
@@ -1,9 +1,9 @@
1
- import { randomUUID } from "node:crypto";
2
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
3
- import { withQuery, parsePath } from "ufo";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
4
3
  import { defu } from "defu";
5
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
6
- import { useRuntimeConfig } from "#imports";
4
+ import { randomUUID } from "uncrypto";
5
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
6
+ import { useRuntimeConfig, createError } from "#imports";
7
7
  export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
8
8
  return eventHandler(async (event) => {
9
9
  config = defu(config, useRuntimeConfig(event).oauth?.battledotnet, {
@@ -12,7 +12,6 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
12
12
  authorizationParams: {}
13
13
  });
14
14
  const query = getQuery(event);
15
- const { code } = query;
16
15
  if (query.error) {
17
16
  const error = createError({
18
17
  statusCode: 401,
@@ -30,8 +29,8 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
30
29
  onError
31
30
  );
32
31
  }
33
- const redirectURL = config.redirectURL || getRequestURL(event).href;
34
- if (!code) {
32
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
33
+ if (!query.code) {
35
34
  config.scope = config.scope || ["openid"];
36
35
  config.region = config.region || "EU";
37
36
  if (config.region === "CN") {
@@ -55,24 +54,16 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
55
54
  if (!config.scope.includes("openid")) {
56
55
  config.scope.push("openid");
57
56
  }
58
- const authCode = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64");
59
- const tokens = await $fetch(
60
- config.tokenURL,
61
- {
62
- method: "POST",
63
- headers: {
64
- "Content-Type": "application/x-www-form-urlencoded",
65
- "Authorization": `Basic ${authCode}`
66
- },
67
- params: {
68
- code,
69
- grant_type: "authorization_code",
70
- scope: config.scope.join(" "),
71
- redirect_uri: parsePath(redirectURL).pathname
72
- }
57
+ const tokens = await requestAccessToken(config.tokenURL, {
58
+ headers: {
59
+ Authorization: `Basic ${Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64")}`
60
+ },
61
+ params: {
62
+ grant_type: "authorization_code",
63
+ scope: config.scope.join(" "),
64
+ redirect_uri: redirectURL,
65
+ code: query.code
73
66
  }
74
- ).catch((error) => {
75
- return { error };
76
67
  });
77
68
  if (tokens.error) {
78
69
  return handleAccessTokenErrorResponse(event, "battle.net", tokens, onError);
@@ -1,22 +1,22 @@
1
- import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
- import { withQuery, parsePath } from "ufo";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
5
  import { useRuntimeConfig } from "#imports";
6
6
  export function oauthCognitoEventHandler({ config, onSuccess, onError }) {
7
7
  return eventHandler(async (event) => {
8
8
  config = defu(config, useRuntimeConfig(event).oauth?.cognito, {
9
9
  authorizationParams: {}
10
10
  });
11
- const { code } = getQuery(event);
12
11
  if (!config.clientId || !config.clientSecret || !config.userPoolId || !config.region) {
13
12
  return handleMissingConfiguration(event, "cognito", ["clientId", "clientSecret", "userPoolId", "region"], onError);
14
13
  }
15
14
  const urlBase = config?.domain || `${config.userPoolId}.auth.${config.region}.amazoncognito.com`;
16
15
  const authorizationURL = `https://${urlBase}/oauth2/authorize`;
17
16
  const tokenURL = `https://${urlBase}/oauth2/token`;
18
- const redirectURL = config.redirectURL || getRequestURL(event).href;
19
- if (!code) {
17
+ const query = getQuery(event);
18
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
19
+ if (!query.code) {
20
20
  config.scope = config.scope || ["openid", "profile"];
21
21
  return sendRedirect(
22
22
  event,
@@ -29,18 +29,18 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }) {
29
29
  })
30
30
  );
31
31
  }
32
- const tokens = await $fetch(
32
+ const tokens = await requestAccessToken(
33
33
  tokenURL,
34
34
  {
35
- method: "POST",
36
- headers: {
37
- "Content-Type": "application/x-www-form-urlencoded"
38
- },
39
- body: `grant_type=authorization_code&client_id=${config.clientId}&client_secret=${config.clientSecret}&redirect_uri=${parsePath(redirectURL).pathname}&code=${code}`
35
+ body: {
36
+ grant_type: "authorization_code",
37
+ client_id: config.clientId,
38
+ client_secret: config.clientSecret,
39
+ redirect_uri: redirectURL,
40
+ code: query.code
41
+ }
40
42
  }
41
- ).catch((error) => {
42
- return { error };
43
- });
43
+ );
44
44
  if (tokens.error) {
45
45
  return handleAccessTokenErrorResponse(event, "cognito", tokens, onError);
46
46
  }
@@ -1,7 +1,7 @@
1
- import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
- import { withQuery, parseURL, stringifyParsedURL } from "ufo";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
5
  import { useRuntimeConfig } from "#imports";
6
6
  export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
7
7
  return eventHandler(async (event) => {
@@ -11,12 +11,12 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
11
11
  profileRequired: true,
12
12
  authorizationParams: {}
13
13
  });
14
- const { code } = getQuery(event);
14
+ const query = getQuery(event);
15
15
  if (!config.clientId || !config.clientSecret) {
16
16
  return handleMissingConfiguration(event, "discord", ["clientId", "clientSecret"], onError);
17
17
  }
18
- const redirectURL = config.redirectURL || getRequestURL(event).href;
19
- if (!code) {
18
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
19
+ if (!query.code) {
20
20
  config.scope = config.scope || [];
21
21
  if (config.emailRequired && !config.scope.includes("email")) {
22
22
  config.scope.push("email");
@@ -35,25 +35,14 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
35
35
  })
36
36
  );
37
37
  }
38
- const parsedRedirectUrl = parseURL(redirectURL);
39
- parsedRedirectUrl.search = "";
40
- const tokens = await $fetch(
41
- config.tokenURL,
42
- {
43
- method: "POST",
44
- headers: {
45
- "Content-Type": "application/x-www-form-urlencoded"
46
- },
47
- body: new URLSearchParams({
48
- client_id: config.clientId,
49
- client_secret: config.clientSecret,
50
- grant_type: "authorization_code",
51
- redirect_uri: stringifyParsedURL(parsedRedirectUrl),
52
- code
53
- }).toString()
38
+ const tokens = await requestAccessToken(config.tokenURL, {
39
+ body: {
40
+ client_id: config.clientId,
41
+ client_secret: config.clientSecret,
42
+ grant_type: "authorization_code",
43
+ redirect_uri: redirectURL,
44
+ code: query.code
54
45
  }
55
- ).catch((error) => {
56
- return { error };
57
46
  });
58
47
  if (tokens.error) {
59
48
  return handleAccessTokenErrorResponse(event, "discord", tokens, onError);
@@ -1,14 +1,8 @@
1
- import {
2
- eventHandler,
3
- createError,
4
- getQuery,
5
- getRequestURL,
6
- sendRedirect
7
- } from "h3";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
8
2
  import { withQuery } from "ufo";
9
3
  import { defu } from "defu";
10
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
11
- import { useRuntimeConfig } from "#imports";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
+ import { useRuntimeConfig, createError } from "#imports";
12
6
  export function oauthFacebookEventHandler({
13
7
  config,
14
8
  onSuccess,
@@ -33,7 +27,7 @@ export function oauthFacebookEventHandler({
33
27
  if (!config.clientId) {
34
28
  return handleMissingConfiguration(event, "facebook", ["clientId"], onError);
35
29
  }
36
- const redirectURL = config.redirectURL || getRequestURL(event).href;
30
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
37
31
  if (!query.code) {
38
32
  config.scope = config.scope || [];
39
33
  return sendRedirect(
@@ -45,11 +39,11 @@ export function oauthFacebookEventHandler({
45
39
  })
46
40
  );
47
41
  }
48
- const tokens = await $fetch(config.tokenURL, {
49
- method: "POST",
42
+ const tokens = await requestAccessToken(config.tokenURL, {
50
43
  body: {
51
44
  client_id: config.clientId,
52
45
  client_secret: config.clientSecret,
46
+ grant_type: "authorization_code",
53
47
  redirect_uri: redirectURL,
54
48
  code: query.code
55
49
  }
@@ -1,8 +1,8 @@
1
- import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
2
  import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
5
- import { useRuntimeConfig } from "#imports";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
+ import { useRuntimeConfig, createError } from "#imports";
6
6
  export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
7
7
  return eventHandler(async (event) => {
8
8
  config = defu(config, useRuntimeConfig(event).oauth?.github, {
@@ -23,12 +23,12 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
23
23
  if (!config.clientId || !config.clientSecret) {
24
24
  return handleMissingConfiguration(event, "github", ["clientId", "clientSecret"], onError);
25
25
  }
26
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
26
27
  if (!query.code) {
27
28
  config.scope = config.scope || [];
28
29
  if (config.emailRequired && !config.scope.includes("user:email")) {
29
30
  config.scope.push("user:email");
30
31
  }
31
- const redirectURL = config.redirectURL || getRequestURL(event).href;
32
32
  return sendRedirect(
33
33
  event,
34
34
  withQuery(config.authorizationURL, {
@@ -39,17 +39,15 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
39
39
  })
40
40
  );
41
41
  }
42
- const tokens = await $fetch(
43
- config.tokenURL,
44
- {
45
- method: "POST",
46
- body: {
47
- client_id: config.clientId,
48
- client_secret: config.clientSecret,
49
- code: query.code
50
- }
42
+ const tokens = await requestAccessToken(config.tokenURL, {
43
+ body: {
44
+ grant_type: "authorization_code",
45
+ client_id: config.clientId,
46
+ client_secret: config.clientSecret,
47
+ redirect_uri: redirectURL,
48
+ code: query.code
51
49
  }
52
- );
50
+ });
53
51
  if (tokens.error) {
54
52
  return handleAccessTokenErrorResponse(event, "github", tokens, onError);
55
53
  }
@@ -1,12 +1,7 @@
1
- import {
2
- eventHandler,
3
- getQuery,
4
- getRequestURL,
5
- sendRedirect
6
- } from "h3";
7
- import { withQuery, parsePath } from "ufo";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
8
3
  import { defu } from "defu";
9
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
10
5
  import { useRuntimeConfig } from "#imports";
11
6
  export function oauthGoogleEventHandler({
12
7
  config,
@@ -20,12 +15,12 @@ export function oauthGoogleEventHandler({
20
15
  userURL: "https://www.googleapis.com/oauth2/v3/userinfo",
21
16
  authorizationParams: {}
22
17
  });
23
- const { code } = getQuery(event);
18
+ const query = getQuery(event);
24
19
  if (!config.clientId) {
25
20
  return handleMissingConfiguration(event, "google", ["clientId"], onError);
26
21
  }
27
- const redirectURL = config.redirectURL || getRequestURL(event).href;
28
- if (!code) {
22
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
23
+ if (!query.code) {
29
24
  config.scope = config.scope || ["email", "profile"];
30
25
  return sendRedirect(
31
26
  event,
@@ -38,18 +33,14 @@ export function oauthGoogleEventHandler({
38
33
  })
39
34
  );
40
35
  }
41
- const body = {
42
- grant_type: "authorization_code",
43
- redirect_uri: parsePath(redirectURL).pathname,
44
- client_id: config.clientId,
45
- client_secret: config.clientSecret,
46
- code
47
- };
48
- const tokens = await $fetch(config.tokenURL, {
49
- method: "POST",
50
- body
51
- }).catch((error) => {
52
- return { error };
36
+ const tokens = await requestAccessToken(config.tokenURL, {
37
+ body: {
38
+ grant_type: "authorization_code",
39
+ code: query.code,
40
+ client_id: config.clientId,
41
+ client_secret: config.clientSecret,
42
+ redirect_uri: redirectURL
43
+ }
53
44
  });
54
45
  if (tokens.error) {
55
46
  return handleAccessTokenErrorResponse(event, "google", tokens, onError);
@@ -1,14 +1,8 @@
1
- import {
2
- eventHandler,
3
- createError,
4
- getQuery,
5
- getRequestURL,
6
- sendRedirect
7
- } from "h3";
8
- import { withQuery, parsePath } from "ufo";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
9
3
  import { defu } from "defu";
10
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
11
- import { useRuntimeConfig } from "#imports";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
+ import { useRuntimeConfig, createError } from "#imports";
12
6
  export function oauthKeycloakEventHandler({
13
7
  config,
14
8
  onSuccess,
@@ -19,7 +13,6 @@ export function oauthKeycloakEventHandler({
19
13
  authorizationParams: {}
20
14
  });
21
15
  const query = getQuery(event);
22
- const { code } = query;
23
16
  if (query.error) {
24
17
  const error = createError({
25
18
  statusCode: 401,
@@ -35,8 +28,8 @@ export function oauthKeycloakEventHandler({
35
28
  const realmURL = `${config.serverUrl}/realms/${config.realm}`;
36
29
  const authorizationURL = `${realmURL}/protocol/openid-connect/auth`;
37
30
  const tokenURL = `${realmURL}/protocol/openid-connect/token`;
38
- const redirectURL = config.redirectURL || getRequestURL(event).href;
39
- if (!code) {
31
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
32
+ if (!query.code) {
40
33
  config.scope = config.scope || ["openid"];
41
34
  return sendRedirect(
42
35
  event,
@@ -53,20 +46,14 @@ export function oauthKeycloakEventHandler({
53
46
  if (!config.scope.includes("openid")) {
54
47
  config.scope.push("openid");
55
48
  }
56
- const tokens = await $fetch(tokenURL, {
57
- method: "POST",
58
- headers: {
59
- "Content-Type": "application/x-www-form-urlencoded"
60
- },
61
- body: new URLSearchParams({
49
+ const tokens = await requestAccessToken(tokenURL, {
50
+ body: {
51
+ grant_type: "authorization_code",
62
52
  client_id: config.clientId,
63
53
  client_secret: config.clientSecret,
64
- grant_type: "authorization_code",
65
- redirect_uri: parsePath(redirectURL).pathname,
66
- code
67
- }).toString()
68
- }).catch((error) => {
69
- return { error };
54
+ redirect_uri: redirectURL,
55
+ code: query.code
56
+ }
70
57
  });
71
58
  if (tokens.error) {
72
59
  return handleAccessTokenErrorResponse(event, "keycloak", tokens, onError);
@@ -1,4 +1,4 @@
1
- import type { H3Event, H3Error } from 'h3';
1
+ import type { OAuthConfig } from '#auth-utils';
2
2
  export interface OAuthLinkedInConfig {
3
3
  /**
4
4
  * LinkedIn OAuth Client ID
@@ -42,13 +42,4 @@ export interface OAuthLinkedInConfig {
42
42
  */
43
43
  redirectURL?: string;
44
44
  }
45
- interface OAuthConfig {
46
- config?: OAuthLinkedInConfig;
47
- onSuccess: (event: H3Event, result: {
48
- user: any;
49
- tokens: any;
50
- }) => Promise<void> | void;
51
- onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
52
- }
53
- export declare function oauthLinkedInEventHandler({ config, onSuccess, onError }: OAuthConfig): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
54
- export {};
45
+ export declare function oauthLinkedInEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthLinkedInConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
@@ -1,7 +1,7 @@
1
- import { eventHandler, getQuery, getRequestURL, sendRedirect } from "h3";
2
- import { withQuery, parseURL, stringifyParsedURL } from "ufo";
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
- import { handleAccessTokenErrorResponse, handleMissingConfiguration } from "../utils.js";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
5
  import { useRuntimeConfig } from "#imports";
6
6
  export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
7
7
  return eventHandler(async (event) => {
@@ -10,12 +10,12 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
10
10
  tokenURL: "https://www.linkedin.com/oauth/v2/accessToken",
11
11
  authorizationParams: {}
12
12
  });
13
- const { code } = getQuery(event);
13
+ const query = getQuery(event);
14
14
  if (!config.clientId || !config.clientSecret) {
15
15
  return handleMissingConfiguration(event, "linkedin", ["clientId", "clientSecret"], onError);
16
16
  }
17
- const redirectURL = config.redirectURL || getRequestURL(event).href;
18
- if (!code) {
17
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
18
+ if (!query.code) {
19
19
  config.scope = config.scope || [];
20
20
  if (!config.scope.length) {
21
21
  config.scope.push("profile", "openid", "email");
@@ -34,25 +34,14 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
34
34
  })
35
35
  );
36
36
  }
37
- const parsedRedirectUrl = parseURL(redirectURL);
38
- parsedRedirectUrl.search = "";
39
- const tokens = await $fetch(
40
- config.tokenURL,
41
- {
42
- method: "POST",
43
- headers: {
44
- "Content-Type": "application/x-www-form-urlencoded"
45
- },
46
- body: new URLSearchParams({
47
- grant_type: "authorization_code",
48
- code,
49
- client_id: config.clientId,
50
- client_secret: config.clientSecret,
51
- redirect_uri: stringifyParsedURL(parsedRedirectUrl)
52
- }).toString()
37
+ const tokens = await requestAccessToken(config.tokenURL, {
38
+ body: {
39
+ grant_type: "authorization_code",
40
+ code: query.code,
41
+ client_id: config.clientId,
42
+ client_secret: config.clientSecret,
43
+ redirect_uri: redirectURL
53
44
  }
54
- ).catch((error) => {
55
- return { error };
56
45
  });
57
46
  if (tokens.error) {
58
47
  return handleAccessTokenErrorResponse(event, "linkedin", tokens, onError);
@@ -1,4 +1,4 @@
1
- import type { H3Event, H3Error } from 'h3';
1
+ import type { OAuthConfig } from '#auth-utils';
2
2
  export interface OAuthMicrosoftConfig {
3
3
  /**
4
4
  * Microsoft OAuth Client ID
@@ -51,13 +51,4 @@ export interface OAuthMicrosoftConfig {
51
51
  */
52
52
  redirectURL?: string;
53
53
  }
54
- interface OAuthConfig {
55
- config?: OAuthMicrosoftConfig;
56
- onSuccess: (event: H3Event, result: {
57
- user: any;
58
- tokens: any;
59
- }) => Promise<void> | void;
60
- onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
61
- }
62
- export declare function oauthMicrosoftEventHandler({ config, onSuccess, onError }: OAuthConfig): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
63
- export {};
54
+ export declare function oauthMicrosoftEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthMicrosoftConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;