nuxt-auth-utils 0.0.14 → 0.0.16

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
@@ -90,6 +90,7 @@ The following helpers are auto-imported in your `server/` directory.
90
90
  ```ts
91
91
  // Set a user session, note that this data is encrypted in the cookie but can be decrypted with an API call
92
92
  // Only store the data that allow you to recognize an user, but do not store sensitive data
93
+ // Merges new data with existing data using defu()
93
94
  await setUserSession(event, {
94
95
  user: {
95
96
  // ... user data
@@ -98,6 +99,9 @@ await setUserSession(event, {
98
99
  // Any extra fields
99
100
  })
100
101
 
102
+ // Replace a user session. Same behaviour as setUserSession, except it does not merge data with existing data
103
+ await replaceUserSession(event, data)
104
+
101
105
  // Get the current user session
102
106
  const session = await getUserSession(event)
103
107
 
@@ -150,6 +154,7 @@ It can also be set using environment variables:
150
154
  #### Supported OAuth Providers
151
155
 
152
156
  - Auth0
157
+ - AWS Cognito
153
158
  - Battle.net
154
159
  - Discord
155
160
  - 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.16"
5
5
  }
package/dist/module.mjs CHANGED
@@ -33,6 +33,7 @@ const module = defineNuxtModule({
33
33
  "sessionHooks",
34
34
  "getUserSession",
35
35
  "setUserSession",
36
+ "replaceUserSession",
36
37
  "clearUserSession",
37
38
  "requireUserSession"
38
39
  ]
@@ -108,6 +109,12 @@ const module = defineNuxtModule({
108
109
  clientId: "",
109
110
  clientSecret: ""
110
111
  });
112
+ runtimeConfig.oauth.cognito = defu(runtimeConfig.oauth.cognito, {
113
+ clientId: "",
114
+ clientSecret: "",
115
+ region: "",
116
+ userPoolId: ""
117
+ });
111
118
  }
112
119
  });
113
120
 
@@ -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,10 +27,5 @@ export interface OAuthGoogleConfig {
27
27
  * @default 'https://oauth2.googleapis.com/token'
28
28
  */
29
29
  tokenURL?: string;
30
- /**
31
- * Redirect URL post authenticating via google
32
- * @default '/auth/google'
33
- */
34
- redirectUrl: '/auth/google';
35
30
  }
36
31
  export declare function googleEventHandler({ config, onSuccess, onError, }: OAuthConfig<OAuthGoogleConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
@@ -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
  }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server.json"
3
+ }
@@ -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
  };
@@ -20,5 +20,11 @@ export declare function getUserSession(event: H3Event): Promise<UserSession>;
20
20
  * @param data User session data, please only store public information since it can be decoded with API calls
21
21
  */
22
22
  export declare function setUserSession(event: H3Event, data: UserSession): Promise<UserSession>;
23
+ /**
24
+ * Replace a user session
25
+ * @param event
26
+ * @param data User session data, please only store public information since it can be decoded with API calls
27
+ */
28
+ export declare function replaceUserSession(event: H3Event, data: UserSession): Promise<UserSession>;
23
29
  export declare function clearUserSession(event: H3Event): Promise<boolean>;
24
30
  export declare function requireUserSession(event: H3Event): Promise<UserSession>;
@@ -11,6 +11,12 @@ export async function setUserSession(event, data) {
11
11
  await session.update(defu(data, session.data));
12
12
  return session.data;
13
13
  }
14
+ export async function replaceUserSession(event, data) {
15
+ const session = await _useSession(event);
16
+ await session.clear();
17
+ await session.update(data);
18
+ return session.data;
19
+ }
14
20
  export async function clearUserSession(event) {
15
21
  const session = await _useSession(event);
16
22
  await sessionHooks.callHookParallel("clear", session.data, event);
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.16",
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.10.1",
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.91",
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.10.1",
45
+ "@nuxt/test-utils": "^3.11.0",
46
+ "@nuxt/ui": "^2.13.0",
47
+ "@nuxt/ui-pro": "^0.7.5",
48
+ "@types/node": "^20.11.17",
49
49
  "changelogen": "^0.5.5",
50
50
  "eslint": "^8.56.0",
51
- "nuxt": "^3.9.0",
51
+ "nuxt": "^3.10.1",
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
  }