nuxt-auth-utils 0.4.1 → 0.4.3

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
  },
@@ -215,9 +215,11 @@ It can also be set using environment variables:
215
215
  - Google
216
216
  - Instagram
217
217
  - Keycloak
218
+ - Linear
218
219
  - LinkedIn
219
220
  - Microsoft
220
221
  - PayPal
222
+ - Polar
221
223
  - Spotify
222
224
  - Steam
223
225
  - 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.3",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -147,6 +147,11 @@ const module = defineNuxtModule({
147
147
  realm: "",
148
148
  redirectURL: ""
149
149
  });
150
+ runtimeConfig.oauth.linear = defu(runtimeConfig.oauth.linear, {
151
+ clientId: "",
152
+ clientSecret: "",
153
+ redirectURL: ""
154
+ });
150
155
  runtimeConfig.oauth.linkedin = defu(runtimeConfig.oauth.linkedin, {
151
156
  clientId: "",
152
157
  clientSecret: "",
@@ -209,6 +214,11 @@ const module = defineNuxtModule({
209
214
  clientSecret: "",
210
215
  redirectURL: ""
211
216
  });
217
+ runtimeConfig.oauth.polar = defu(runtimeConfig.oauth.polar, {
218
+ clientId: "",
219
+ clientSecret: "",
220
+ redirectURL: ""
221
+ });
212
222
  }
213
223
  });
214
224
 
@@ -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,41 @@
1
+ import type { OAuthConfig } from '#auth-utils';
2
+ export interface OAuthLinearConfig {
3
+ /**
4
+ * Linear OAuth Client ID
5
+ * @default process.env.NUXT_OAUTH_LINEAR_CLIENT_ID
6
+ */
7
+ clientId?: string;
8
+ /**
9
+ * Linear OAuth Client Secret
10
+ * @default process.env.NUXT_OAUTH_LINEAR_CLIENT_SECRET
11
+ */
12
+ clientSecret?: string;
13
+ /**
14
+ * Linear OAuth Scope
15
+ * @default ['read']
16
+ * @see https://developers.linear.app/docs/oauth/authentication#scopes
17
+ * @example ['read', 'write', 'issues:create', 'comments:create', 'timeSchedule:write', 'admin']
18
+ */
19
+ scope?: string[];
20
+ /**
21
+ * Linear OAuth Authorization URL
22
+ * @default 'https://linear.app/oauth/authorize'
23
+ */
24
+ authorizationURL?: string;
25
+ /**
26
+ * Linear OAuth Token URL
27
+ * @default 'https://api.linear.app/oauth/token'
28
+ */
29
+ tokenURL?: string;
30
+ /**
31
+ * Extra authorization parameters to provide to the authorization URL
32
+ * @see https://developers.linear.app/docs/oauth/authentication#id-2.-redirect-user-access-requests-to-linear
33
+ */
34
+ authorizationParams?: Record<string, string>;
35
+ /**
36
+ * Redirect URL to allow overriding for situations like prod failing to determine public hostname
37
+ * @default process.env.NUXT_OAUTH_LINEAR_REDIRECT_URL or current URL
38
+ */
39
+ redirectURL?: string;
40
+ }
41
+ export declare function defineOAuthLinearEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthLinearConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
@@ -0,0 +1,80 @@
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
3
+ import { defu } from "defu";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
+ import { useRuntimeConfig, createError } from "#imports";
6
+ export function defineOAuthLinearEventHandler({ config, onSuccess, onError }) {
7
+ return eventHandler(async (event) => {
8
+ config = defu(config, useRuntimeConfig(event).oauth?.linear, {
9
+ authorizationURL: "https://linear.app/oauth/authorize",
10
+ tokenURL: "https://api.linear.app/oauth/token",
11
+ authorizationParams: {}
12
+ });
13
+ const query = getQuery(event);
14
+ if (query.error) {
15
+ const error = createError({
16
+ statusCode: 401,
17
+ message: `Linear login failed: ${query.error || "Unknown error"}`,
18
+ data: query
19
+ });
20
+ if (!onError) throw error;
21
+ return onError(event, error);
22
+ }
23
+ if (!config.clientId || !config.clientSecret) {
24
+ return handleMissingConfiguration(event, "linear", ["clientId", "clientSecret"], onError);
25
+ }
26
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
27
+ if (!query.code) {
28
+ config.scope = config.scope || ["read"];
29
+ return sendRedirect(
30
+ event,
31
+ withQuery(config.authorizationURL, {
32
+ response_type: "code",
33
+ client_id: config.clientId,
34
+ redirect_uri: redirectURL,
35
+ scope: config.scope.join(" "),
36
+ ...config.authorizationParams
37
+ })
38
+ );
39
+ }
40
+ const tokens = await requestAccessToken(config.tokenURL, {
41
+ headers: {
42
+ "Content-Type": "application/x-www-form-urlencoded"
43
+ },
44
+ body: {
45
+ grant_type: "authorization_code",
46
+ client_id: config.clientId,
47
+ client_secret: config.clientSecret,
48
+ redirect_uri: redirectURL,
49
+ code: query.code
50
+ }
51
+ });
52
+ if (tokens.error) {
53
+ return handleAccessTokenErrorResponse(event, "linear", tokens, onError);
54
+ }
55
+ const accessToken = tokens.access_token;
56
+ const user = await $fetch("https://api.linear.app/graphql", {
57
+ method: "POST",
58
+ headers: {
59
+ "Authorization": `Bearer ${accessToken}`,
60
+ "Content-Type": "application/json"
61
+ },
62
+ body: JSON.stringify({
63
+ query: "{ viewer { id name email } }"
64
+ })
65
+ });
66
+ if (!user.data || !user.data.viewer) {
67
+ const error = createError({
68
+ statusCode: 500,
69
+ message: "Could not get Linear user",
70
+ data: tokens
71
+ });
72
+ if (!onError) throw error;
73
+ return onError(event, error);
74
+ }
75
+ return onSuccess(event, {
76
+ tokens,
77
+ user: user.data.viewer
78
+ });
79
+ });
80
+ }
@@ -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
+ }
@@ -37,7 +37,7 @@ export declare function replaceUserSession(event: H3Event, data: UserSession, co
37
37
  * @param event The Request (h3) event
38
38
  * @returns true if the session was cleared
39
39
  */
40
- export declare function clearUserSession(event: H3Event): Promise<boolean>;
40
+ export declare function clearUserSession(event: H3Event, config?: Partial<SessionConfig>): Promise<boolean>;
41
41
  /**
42
42
  * Require a user session, throw a 401 error if the user is not logged in
43
43
  * @param event
@@ -17,8 +17,8 @@ export async function replaceUserSession(event, data, config) {
17
17
  await session.update(data);
18
18
  return session.data;
19
19
  }
20
- export async function clearUserSession(event) {
21
- const session = await _useSession(event);
20
+ export async function clearUserSession(event, config) {
21
+ const session = await _useSession(event, config);
22
22
  await sessionHooks.callHookParallel("clear", session.data, event);
23
23
  await session.clear();
24
24
  return true;
@@ -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' | 'linear' | '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,11 +1,11 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Add Authentication to Nuxt applications with secured & sealed cookies sessions.",
5
5
  "repository": "atinux/nuxt-auth-utils",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
- "packageManager": "pnpm@9.11.0",
8
+ "packageManager": "pnpm@9.12.0",
9
9
  "exports": {
10
10
  ".": {
11
11
  "types": "./dist/types.d.ts",
@@ -55,7 +55,7 @@
55
55
  }
56
56
  },
57
57
  "devDependencies": {
58
- "@iconify-json/simple-icons": "^1.2.4",
58
+ "@iconify-json/simple-icons": "^1.2.6",
59
59
  "@nuxt/devtools": "latest",
60
60
  "@nuxt/eslint-config": "^0.5.7",
61
61
  "@nuxt/module-builder": "^0.8.4",
@@ -65,10 +65,10 @@
65
65
  "@nuxt/ui-pro": "^1.4.3",
66
66
  "@simplewebauthn/types": "^10.0.0",
67
67
  "changelogen": "^0.5.7",
68
- "eslint": "^9.11.1",
68
+ "eslint": "^9.12.0",
69
69
  "nuxt": "^3.13.2",
70
70
  "typescript": "^5.6.2",
71
- "vitest": "^2.1.1",
71
+ "vitest": "^2.1.2",
72
72
  "vue-tsc": "^2.1.6"
73
73
  }
74
74
  }