nuxt-auth-utils 0.4.1 → 0.4.2

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
@@ -126,7 +126,7 @@ await setUserSession(event, {
126
126
  user: {
127
127
  login: 'atinux'
128
128
  },
129
- // Private data accessible on server/ routes
129
+ // Private data accessible only on server/ routes
130
130
  secure: {
131
131
  apiToken: '1234567890'
132
132
  },
@@ -218,6 +218,7 @@ It can also be set using environment variables:
218
218
  - LinkedIn
219
219
  - Microsoft
220
220
  - PayPal
221
+ - Polar
221
222
  - Spotify
222
223
  - Steam
223
224
  - TikTok
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.4.1",
4
+ "version": "0.4.2",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -209,6 +209,11 @@ const module = defineNuxtModule({
209
209
  clientSecret: "",
210
210
  redirectURL: ""
211
211
  });
212
+ runtimeConfig.oauth.polar = defu(runtimeConfig.oauth.polar, {
213
+ clientId: "",
214
+ clientSecret: "",
215
+ redirectURL: ""
216
+ });
212
217
  }
213
218
  });
214
219
 
@@ -1,2 +1,2 @@
1
- declare const _default: any;
1
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
2
2
  export default _default;
@@ -1,2 +1,2 @@
1
- declare const _default: any;
1
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
2
2
  export default _default;
@@ -0,0 +1,49 @@
1
+ import type { OAuthConfig } from '#auth-utils';
2
+ export interface OAuthPolarConfig {
3
+ /**
4
+ * Polar Client ID
5
+ * @default process.env.NUXT_OAUTH_POLAR_CLIENT_ID
6
+ */
7
+ clientId?: string;
8
+ /**
9
+ * Polar OAuth Client Secret
10
+ * @default process.env.NUXT_OAUTH_POLAR_CLIENT_SECRET
11
+ */
12
+ clientSecret?: string;
13
+ /**
14
+ * Polar OAuth Scope
15
+ * @default []
16
+ * @see https://api.polar.sh/.well-known/openid-configuration
17
+ * @example ['openid']
18
+ */
19
+ scope?: string[];
20
+ /**
21
+ * Require email from user, adds the ['email'] scope if not present
22
+ * @default false
23
+ */
24
+ emailRequired?: boolean;
25
+ /**
26
+ * Polar OAuth Authorization URL
27
+ * @see https://docs.polar.sh/api/authentication#start-the-authorization-flow
28
+ * @default 'https://polar.sh/oauth2/authorize'
29
+ */
30
+ authorizationURL?: string;
31
+ /**
32
+ * Polar OAuth Token URL
33
+ * @see https://docs.polar.sh/api/authentication#exchange-the-authorization-code
34
+ * @default 'https://api.polar.sh/v1/oauth2/token'
35
+ */
36
+ tokenURL?: string;
37
+ /**
38
+ * Extra authorization parameters to provide to the authorization URL
39
+ * @see https://docs.polar.sh/api/authentication#user-vs-organization-access-tokens
40
+ * @example { sub_type: 'organization' }
41
+ */
42
+ authorizationParams?: Record<string, string>;
43
+ /**
44
+ * Redirect URL to to allow overriding for situations like prod failing to determine public hostname
45
+ * @default process.env.NUXT_OAUTH_POLAR_REDIRECT_URL or current URL
46
+ */
47
+ redirectURL?: string;
48
+ }
49
+ export declare function defineOAuthPolarEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthPolarConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
@@ -0,0 +1,67 @@
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
3
+ import { defu } from "defu";
4
+ import { handleAccessTokenErrorResponse, handleMissingConfiguration, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
+ import { useRuntimeConfig, createError } from "#imports";
6
+ export function defineOAuthPolarEventHandler({ config, onSuccess, onError }) {
7
+ return eventHandler(async (event) => {
8
+ config = defu(config, useRuntimeConfig(event).oauth?.polar, {
9
+ authorizationURL: "https://polar.sh/oauth2/authorize",
10
+ tokenURL: "https://api.polar.sh/v1/oauth2/token"
11
+ });
12
+ const query = getQuery(event);
13
+ if (!config.clientId || !config.clientSecret) {
14
+ return handleMissingConfiguration(event, "polar", ["clientId", "clientSecret"], onError);
15
+ }
16
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
17
+ if (!query.code) {
18
+ config.scope = config.scope || [];
19
+ if (!config.scope.includes("openid"))
20
+ config.scope.push("openid");
21
+ if (config.emailRequired && !config.scope.includes("email"))
22
+ config.scope.push("email");
23
+ return sendRedirect(
24
+ event,
25
+ withQuery(config.authorizationURL, {
26
+ response_type: "code",
27
+ client_id: config.clientId,
28
+ redirect_uri: redirectURL,
29
+ scope: config.scope.join(" "),
30
+ ...config.authorizationParams
31
+ })
32
+ );
33
+ }
34
+ const tokens = await requestAccessToken(config.tokenURL, {
35
+ body: {
36
+ grant_type: "authorization_code",
37
+ redirect_uri: redirectURL,
38
+ client_id: config.clientId,
39
+ client_secret: config.clientSecret,
40
+ code: query.code
41
+ }
42
+ });
43
+ if (tokens.error) {
44
+ return handleAccessTokenErrorResponse(event, "polar", tokens, onError);
45
+ }
46
+ const accessToken = tokens.access_token;
47
+ const user = await $fetch("https://api.polar.sh/v1/oauth2/userinfo", {
48
+ headers: {
49
+ Authorization: `Bearer ${accessToken}`,
50
+ Accept: "application/json"
51
+ }
52
+ });
53
+ if (!user) {
54
+ const error = createError({
55
+ statusCode: 500,
56
+ message: "Could not get Polar user",
57
+ data: tokens
58
+ });
59
+ if (!onError) throw error;
60
+ return onError(event, error);
61
+ }
62
+ return onSuccess(event, {
63
+ tokens,
64
+ user
65
+ });
66
+ });
67
+ }
@@ -1,5 +1,5 @@
1
1
  import type { H3Event, H3Error } from 'h3';
2
- export type OAuthProvider = 'auth0' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'github' | 'gitlab' | 'google' | 'instagram' | 'keycloak' | 'linkedin' | 'microsoft' | 'paypal' | 'spotify' | 'steam' | 'tiktok' | 'twitch' | 'vk' | 'x' | 'xsuaa' | 'yandex' | (string & {});
2
+ export type OAuthProvider = 'auth0' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'github' | 'gitlab' | 'google' | 'instagram' | 'keycloak' | 'linkedin' | 'microsoft' | 'paypal' | 'polar' | 'spotify' | 'steam' | 'tiktok' | 'twitch' | 'vk' | 'x' | 'xsuaa' | 'yandex' | (string & {});
3
3
  export type OnError = (event: H3Event, error: H3Error) => Promise<void> | void;
4
4
  export interface OAuthConfig<TConfig, TUser = any, TTokens = any> {
5
5
  config?: TConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Add Authentication to Nuxt applications with secured & sealed cookies sessions.",
5
5
  "repository": "atinux/nuxt-auth-utils",
6
6
  "license": "MIT",