Skip to content

Commit

Permalink
feat: add fields support to facebook provider
Browse files Browse the repository at this point in the history
* feat: add fields support to facebook provider

* fix: move server plugin to core

* lint fix

* chore: fix types

---------

Co-authored-by: Sébastien Chopin <seb@nuxt.com>
  • Loading branch information
ozancakir and atinux authored May 16, 2024
1 parent c9e4ff7 commit 8e53936
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFile, readFile } from 'node:fs/promises'
import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit'
import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler, addServerPlugin } from '@nuxt/kit'
import { join } from 'pathe'
import { defu } from 'defu'
import { randomUUID } from 'uncrypto'
Expand Down Expand Up @@ -45,6 +45,7 @@ export default defineNuxtModule<ModuleOptions>({
],
})
}
addServerPlugin(resolver.resolve('./runtime/server/plugins/oauth'))
// Waiting for https://github.com/nuxt/nuxt/pull/24000/files
// addServerImportsDir(resolver.resolve('./runtime/server/utils'))
addServerHandler({
Expand Down
17 changes: 14 additions & 3 deletions src/runtime/server/lib/oauth/facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export interface OAuthFacebookConfig {
*/
scope?: string[]

/**
* Facebook OAuth User Fields
* @default [ 'id', 'name'],
* @see https://developers.facebook.com/docs/graph-api/guides/field-expansion
* @example [ 'id', 'name', 'email' ],
*/
fields?: string[]

/**
* Facebook OAuth Authorization URL
* @default 'https://www.facebook.com/v19.0/dialog/oauth'
Expand Down Expand Up @@ -121,9 +129,12 @@ export function facebookEventHandler({

const accessToken = tokens.access_token
// TODO: improve typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await ofetch(
`https://graph.facebook.com/v19.0/me?fields=id,name&access_token=${accessToken}`,

config.fields = config.fields || ['id', 'name']
const fields = config.fields.join(',')

const user = await ofetch(
`https://graph.facebook.com/v19.0/me?fields=${fields}&access_token=${accessToken}`,
)

if (!user) {
Expand Down
23 changes: 23 additions & 0 deletions src/runtime/server/plugins/oauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { NitroApp } from 'nitropack'
import { defineNitroPlugin } from 'nitropack/runtime'

export default defineNitroPlugin((nitroApp: NitroApp) => {
if (process.env.NUXT_OAUTH_FACEBOOK_CLIENT_ID && process.env.NUXT_OAUTH_FACEBOOK_CLIENT_SECRET) {
// In facebook login, the url is redirected to /#_=_ which is not a valid route
// so we remove it from the url, we are loading this long before the app is loaded
// by using render:html hook
// this is a hack, but it works
// https://stackoverflow.com/questions/7131909/facebook-callback-appends-to-return-url
nitroApp.hooks.hook('render:html', (html) => {
html.head.unshift(`
<script>
if (window.location.hash === "#_=_"){
history.replaceState
? history.replaceState(null, null, window.location.href.split("#")[0])
: window.location.hash = "";
}
</script>
`)
})
}
})

0 comments on commit 8e53936

Please sign in to comment.