nuxt-auth-utils 0.0.23 → 0.0.24

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
@@ -146,6 +146,7 @@ It can also be set using environment variables:
146
146
  - AWS Cognito
147
147
  - Battle.net
148
148
  - Discord
149
+ - Facebook
149
150
  - GitHub
150
151
  - Google
151
152
  - Keycloak
package/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.0.23"
4
+ "version": "0.0.24"
5
5
  }
package/dist/module.mjs CHANGED
@@ -119,6 +119,10 @@ const module = defineNuxtModule({
119
119
  region: "",
120
120
  userPoolId: ""
121
121
  });
122
+ runtimeConfig.oauth.facebook = defu(runtimeConfig.oauth.facebook, {
123
+ clientId: "",
124
+ clientSecret: ""
125
+ });
122
126
  }
123
127
  });
124
128
 
@@ -0,0 +1,36 @@
1
+ import type { OAuthConfig } from '#auth-utils';
2
+ export interface OAuthFacebookConfig {
3
+ /**
4
+ * Facebook OAuth Client ID
5
+ * @default process.env.NUXT_OAUTH_FACEBOOK_CLIENT_ID
6
+ */
7
+ clientId?: string;
8
+ /**
9
+ * Facebook OAuth Client Secret
10
+ * @default process.env.NUXT_OAUTH_FACEBOOK_CLIENT_SECRET
11
+ */
12
+ clientSecret?: string;
13
+ /**
14
+ * Facebook OAuth Scope
15
+ * @default []
16
+ * @see https://developers.facebook.com/docs/permissions
17
+ * @example [ 'email' ],
18
+ */
19
+ scope?: string[];
20
+ /**
21
+ * Facebook OAuth Authorization URL
22
+ * @default 'https://www.facebook.com/v19.0/dialog/oauth'
23
+ */
24
+ authorizationURL?: string;
25
+ /**
26
+ * Facebook OAuth Token URL
27
+ * @default 'https://graph.facebook.com/v19.0/oauth/access_token'
28
+ */
29
+ tokenURL?: string;
30
+ /**
31
+ * Extra authorization parameters to provide to the authorization URL
32
+ * @see https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow/
33
+ */
34
+ authorizationParams?: Record<string, string>;
35
+ }
36
+ export declare function facebookEventHandler({ config, onSuccess, onError, }: OAuthConfig<OAuthFacebookConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
@@ -0,0 +1,86 @@
1
+ import {
2
+ eventHandler,
3
+ createError,
4
+ getQuery,
5
+ getRequestURL,
6
+ sendRedirect
7
+ } from "h3";
8
+ import { ofetch } from "ofetch";
9
+ import { withQuery } from "ufo";
10
+ import { defu } from "defu";
11
+ import { useRuntimeConfig } from "#imports";
12
+ export function facebookEventHandler({
13
+ config,
14
+ onSuccess,
15
+ onError
16
+ }) {
17
+ return eventHandler(async (event) => {
18
+ config = defu(config, useRuntimeConfig(event).oauth?.facebook, {
19
+ authorizationURL: "https://www.facebook.com/v19.0/dialog/oauth",
20
+ tokenURL: "https://graph.facebook.com/v19.0/oauth/access_token",
21
+ authorizationParams: {}
22
+ });
23
+ const query = getQuery(event);
24
+ if (query.error) {
25
+ const error = createError({
26
+ statusCode: 401,
27
+ message: `Facebook login failed: ${query.error || "Unknown error"}`,
28
+ data: query
29
+ });
30
+ if (!onError)
31
+ throw error;
32
+ return onError(event, error);
33
+ }
34
+ if (!config.clientId) {
35
+ const error = createError({
36
+ statusCode: 500,
37
+ message: "Missing NUXT_OAUTH_FACEBOOK_CLIENT_ID or NUXT_OAUTH_FACEBOOK_CLIENT_SECRET env variables."
38
+ });
39
+ if (!onError)
40
+ throw error;
41
+ return onError(event, error);
42
+ }
43
+ const redirectUrl = getRequestURL(event).href;
44
+ if (!query.code) {
45
+ config.scope = config.scope || [];
46
+ return sendRedirect(
47
+ event,
48
+ withQuery(config.authorizationURL, {
49
+ client_id: config.clientId,
50
+ redirect_uri: redirectUrl,
51
+ scope: config.scope.join(" ")
52
+ })
53
+ );
54
+ }
55
+ const tokens = await $fetch(config.tokenURL, {
56
+ method: "POST",
57
+ body: {
58
+ client_id: config.clientId,
59
+ client_secret: config.clientSecret,
60
+ redirect_uri: redirectUrl,
61
+ code: query.code
62
+ }
63
+ });
64
+ if (tokens.error) {
65
+ const error = createError({
66
+ statusCode: 401,
67
+ message: `Facebook login failed: ${tokens.error || "Unknown error"}`,
68
+ data: tokens
69
+ });
70
+ if (!onError)
71
+ throw error;
72
+ return onError(event, error);
73
+ }
74
+ const accessToken = tokens.access_token;
75
+ const user = await ofetch(
76
+ `https://graph.facebook.com/v19.0/me?fields=id,name&access_token=${accessToken}`
77
+ );
78
+ if (!user) {
79
+ throw new Error("Facebook login failed: no user found");
80
+ }
81
+ return onSuccess(event, {
82
+ user,
83
+ tokens
84
+ });
85
+ });
86
+ }
@@ -9,6 +9,7 @@ import { battledotnetEventHandler } from '../lib/oauth/battledotnet';
9
9
  import { keycloakEventHandler } from '../lib/oauth/keycloak';
10
10
  import { linkedinEventHandler } from '../lib/oauth/linkedin';
11
11
  import { cognitoEventHandler } from '../lib/oauth/cognito';
12
+ import { facebookEventHandler } from '../lib/oauth/facebook';
12
13
  export declare const oauth: {
13
14
  githubEventHandler: typeof githubEventHandler;
14
15
  spotifyEventHandler: typeof spotifyEventHandler;
@@ -21,4 +22,5 @@ export declare const oauth: {
21
22
  keycloakEventHandler: typeof keycloakEventHandler;
22
23
  linkedinEventHandler: typeof linkedinEventHandler;
23
24
  cognitoEventHandler: typeof cognitoEventHandler;
25
+ facebookEventHandler: typeof facebookEventHandler;
24
26
  };
@@ -9,6 +9,7 @@ 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
11
  import { cognitoEventHandler } from "../lib/oauth/cognito.mjs";
12
+ import { facebookEventHandler } from "../lib/oauth/facebook.mjs";
12
13
  export const oauth = {
13
14
  githubEventHandler,
14
15
  spotifyEventHandler,
@@ -20,5 +21,6 @@ export const oauth = {
20
21
  battledotnetEventHandler,
21
22
  keycloakEventHandler,
22
23
  linkedinEventHandler,
23
- cognitoEventHandler
24
+ cognitoEventHandler,
25
+ facebookEventHandler
24
26
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "Minimalist Auth module for Nuxt with SSR",
5
5
  "repository": "Atinux/nuxt-auth-utils",
6
6
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  "devDependencies": {
42
42
  "@iconify-json/simple-icons": "^1.1.99",
43
43
  "@nuxt/devtools": "latest",
44
- "@nuxt/eslint-config": "^0.3.7",
44
+ "@nuxt/eslint-config": "^0.3.8",
45
45
  "@nuxt/module-builder": "^0.5.5",
46
46
  "@nuxt/schema": "^3.11.2",
47
47
  "@nuxt/test-utils": "^3.12.1",