nuxt-auth-utils 0.0.14 → 0.0.15

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
@@ -150,6 +150,7 @@ It can also be set using environment variables:
150
150
  #### Supported OAuth Providers
151
151
 
152
152
  - Auth0
153
+ - AWS Cognito
153
154
  - Battle.net
154
155
  - Discord
155
156
  - GitHub
package/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.0.14"
4
+ "version": "0.0.15"
5
5
  }
package/dist/module.mjs CHANGED
@@ -108,6 +108,12 @@ const module = defineNuxtModule({
108
108
  clientId: "",
109
109
  clientSecret: ""
110
110
  });
111
+ runtimeConfig.oauth.cognito = defu(runtimeConfig.oauth.cognito, {
112
+ clientId: "",
113
+ clientSecret: "",
114
+ region: "",
115
+ userPoolId: ""
116
+ });
111
117
  }
112
118
  });
113
119
 
@@ -38,5 +38,12 @@ export interface OAuthAuth0Config {
38
38
  * @see https://auth0.com/docs/authenticate/login/max-age-reauthentication
39
39
  */
40
40
  maxAge?: number;
41
+ /**
42
+ * Login connection. If no connection is specified, it will redirect to the standard Auth0 login page and show the Login Widget.
43
+ * @default ''
44
+ * @see https://auth0.com/docs/api/authentication#social
45
+ * @example 'github'
46
+ */
47
+ connection?: string;
41
48
  }
42
49
  export declare function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthAuth0Config>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
@@ -32,7 +32,8 @@ export function auth0EventHandler({ config, onSuccess, onError }) {
32
32
  redirect_uri: redirectUrl,
33
33
  scope: config.scope.join(" "),
34
34
  audience: config.audience || "",
35
- max_age: config.maxAge || 0
35
+ max_age: config.maxAge || 0,
36
+ connection: config.connection || ""
36
37
  })
37
38
  );
38
39
  }
@@ -0,0 +1,29 @@
1
+ import type { OAuthConfig } from '#auth-utils';
2
+ export interface OAuthCognitoConfig {
3
+ /**
4
+ * AWS Cognito App Client ID
5
+ * @default process.env.NUXT_OAUTH_COGNITO_CLIENT_ID
6
+ */
7
+ clientId?: string;
8
+ /**
9
+ * AWS Cognito App Client Secret
10
+ * @default process.env.NUXT_OAUTH_COGNITO_CLIENT_SECRET
11
+ */
12
+ clientSecret?: string;
13
+ /**
14
+ * AWS Cognito User Pool ID
15
+ * @default process.env.NUXT_OAUTH_COGNITO_USER_POOL_ID
16
+ */
17
+ userPoolId?: string;
18
+ /**
19
+ * AWS Cognito Region
20
+ * @default process.env.NUXT_OAUTH_COGNITO_REGION
21
+ */
22
+ region?: string;
23
+ /**
24
+ * AWS Cognito Scope
25
+ * @default []
26
+ */
27
+ scope?: string[];
28
+ }
29
+ export declare function cognitoEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthCognitoConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
@@ -0,0 +1,68 @@
1
+ import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
2
+ import { withQuery, parsePath } from "ufo";
3
+ import { ofetch } from "ofetch";
4
+ import { defu } from "defu";
5
+ import { useRuntimeConfig } from "#imports";
6
+ export function cognitoEventHandler({ config, onSuccess, onError }) {
7
+ return eventHandler(async (event) => {
8
+ config = defu(config, useRuntimeConfig(event).oauth?.cognito);
9
+ const { code } = getQuery(event);
10
+ if (!config.clientId || !config.clientSecret || !config.userPoolId || !config.region) {
11
+ const error = createError({
12
+ statusCode: 500,
13
+ 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."
14
+ });
15
+ if (!onError)
16
+ throw error;
17
+ return onError(event, error);
18
+ }
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`;
21
+ const redirectUrl = getRequestURL(event).href;
22
+ if (!code) {
23
+ config.scope = config.scope || ["openid", "profile"];
24
+ return sendRedirect(
25
+ event,
26
+ withQuery(authorizationURL, {
27
+ client_id: config.clientId,
28
+ redirect_uri: redirectUrl,
29
+ response_type: "code",
30
+ scope: config.scope.join(" ")
31
+ })
32
+ );
33
+ }
34
+ const tokens = await ofetch(
35
+ tokenURL,
36
+ {
37
+ method: "POST",
38
+ headers: {
39
+ "Content-Type": "application/x-www-form-urlencoded"
40
+ },
41
+ body: `grant_type=authorization_code&client_id=${config.clientId}&client_secret=${config.clientSecret}&redirect_uri=${parsePath(redirectUrl).pathname}&code=${code}`
42
+ }
43
+ ).catch((error) => {
44
+ return { error };
45
+ });
46
+ if (tokens.error) {
47
+ const error = createError({
48
+ statusCode: 401,
49
+ message: `Cognito login failed: ${tokens.error_description || "Unknown error"}`,
50
+ data: tokens
51
+ });
52
+ if (!onError)
53
+ throw error;
54
+ return onError(event, error);
55
+ }
56
+ const tokenType = tokens.token_type;
57
+ const accessToken = tokens.access_token;
58
+ const user = await ofetch(`https://${config.userPoolId}.auth.${config.region}.amazoncognito.com/oauth2/userInfo`, {
59
+ headers: {
60
+ Authorization: `${tokenType} ${accessToken}`
61
+ }
62
+ });
63
+ return onSuccess(event, {
64
+ tokens,
65
+ user
66
+ });
67
+ });
68
+ }
@@ -30,7 +30,7 @@ export function githubEventHandler({ config, onSuccess, onError }) {
30
30
  withQuery(config.authorizationURL, {
31
31
  client_id: config.clientId,
32
32
  redirect_uri: redirectUrl,
33
- scope: config.scope.join("%20")
33
+ scope: config.scope.join(" ")
34
34
  })
35
35
  );
36
36
  }
@@ -27,7 +27,7 @@ export function microsoftEventHandler({ config, onSuccess, onError }) {
27
27
  client_id: config.clientId,
28
28
  response_type: "code",
29
29
  redirect_uri: redirectUrl,
30
- scope: scope.join("%20")
30
+ scope: scope.join(" ")
31
31
  })
32
32
  );
33
33
  }
@@ -31,7 +31,7 @@ export function spotifyEventHandler({ config, onSuccess, onError }) {
31
31
  response_type: "code",
32
32
  client_id: config.clientId,
33
33
  redirect_uri: redirectUrl,
34
- scope: config.scope.join("%20")
34
+ scope: config.scope.join(" ")
35
35
  })
36
36
  );
37
37
  }
@@ -31,7 +31,7 @@ export function twitchEventHandler({ config, onSuccess, onError }) {
31
31
  response_type: "code",
32
32
  client_id: config.clientId,
33
33
  redirect_uri: redirectUrl,
34
- scope: config.scope.join("%20")
34
+ scope: config.scope.join(" ")
35
35
  })
36
36
  );
37
37
  }
@@ -8,6 +8,7 @@ import { discordEventHandler } from '../lib/oauth/discord';
8
8
  import { battledotnetEventHandler } from '../lib/oauth/battledotnet';
9
9
  import { keycloakEventHandler } from '../lib/oauth/keycloak';
10
10
  import { linkedinEventHandler } from '../lib/oauth/linkedin';
11
+ import { cognitoEventHandler } from '../lib/oauth/cognito';
11
12
  export declare const oauth: {
12
13
  githubEventHandler: typeof githubEventHandler;
13
14
  spotifyEventHandler: typeof spotifyEventHandler;
@@ -19,4 +20,5 @@ export declare const oauth: {
19
20
  battledotnetEventHandler: typeof battledotnetEventHandler;
20
21
  keycloakEventHandler: typeof keycloakEventHandler;
21
22
  linkedinEventHandler: typeof linkedinEventHandler;
23
+ cognitoEventHandler: typeof cognitoEventHandler;
22
24
  };
@@ -8,6 +8,7 @@ import { discordEventHandler } from "../lib/oauth/discord.mjs";
8
8
  import { battledotnetEventHandler } from "../lib/oauth/battledotnet.mjs";
9
9
  import { keycloakEventHandler } from "../lib/oauth/keycloak.mjs";
10
10
  import { linkedinEventHandler } from "../lib/oauth/linkedin.mjs";
11
+ import { cognitoEventHandler } from "../lib/oauth/cognito.mjs";
11
12
  export const oauth = {
12
13
  githubEventHandler,
13
14
  spotifyEventHandler,
@@ -18,5 +19,6 @@ export const oauth = {
18
19
  discordEventHandler,
19
20
  battledotnetEventHandler,
20
21
  keycloakEventHandler,
21
- linkedinEventHandler
22
+ linkedinEventHandler,
23
+ cognitoEventHandler
22
24
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "Minimalist Auth module for Nuxt with SSR",
5
5
  "repository": "Atinux/nuxt-auth-utils",
6
6
  "license": "MIT",
@@ -30,27 +30,27 @@
30
30
  "test:watch": "vitest watch"
31
31
  },
32
32
  "dependencies": {
33
- "@nuxt/kit": "^3.9.0",
34
- "defu": "^6.1.3",
33
+ "@nuxt/kit": "^3.9.3",
34
+ "defu": "^6.1.4",
35
35
  "hookable": "^5.5.3",
36
36
  "ofetch": "^1.3.3",
37
37
  "ohash": "^1.1.3"
38
38
  },
39
39
  "devDependencies": {
40
- "@iconify-json/simple-icons": "^1.1.86",
40
+ "@iconify-json/simple-icons": "^1.1.90",
41
41
  "@nuxt/devtools": "latest",
42
42
  "@nuxt/eslint-config": "^0.2.0",
43
43
  "@nuxt/module-builder": "^0.5.5",
44
- "@nuxt/schema": "^3.9.0",
45
- "@nuxt/test-utils": "^3.9.0",
46
- "@nuxt/ui": "^2.11.1",
47
- "@nuxt/ui-pro": "^0.6.1",
48
- "@types/node": "^20.10.6",
44
+ "@nuxt/schema": "^3.9.3",
45
+ "@nuxt/test-utils": "^3.11.0",
46
+ "@nuxt/ui": "^2.12.3",
47
+ "@nuxt/ui-pro": "^0.7.3",
48
+ "@types/node": "^20.11.10",
49
49
  "changelogen": "^0.5.5",
50
50
  "eslint": "^8.56.0",
51
- "nuxt": "^3.9.0",
51
+ "nuxt": "^3.9.3",
52
52
  "typescript": "^5.3.3",
53
- "vitest": "^1.1.3",
53
+ "vitest": "^1.2.2",
54
54
  "vue-tsc": "^1.8.27"
55
55
  }
56
56
  }