Skip to content

Commit

Permalink
fix: move server plugin to core
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed May 14, 2024
1 parent a8a8713 commit 6262001
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
19 changes: 1 addition & 18 deletions playground/server/plugins/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default defineNitroPlugin((nitroApp) => {
export default defineNitroPlugin(() => {
sessionHooks.hook('fetch', async (session) => {
// Extend User Session
// Or throw createError({ ... }) if session is invalid
Expand All @@ -11,21 +11,4 @@ export default defineNitroPlugin((nitroApp) => {
// Log that user logged out
console.log('User logged out')
})

// 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>
`)
})
})
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
22 changes: 22 additions & 0 deletions src/runtime/server/plugins/oauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineNitroPlugin } from '#imports'

Check failure on line 1 in src/runtime/server/plugins/oauth.ts

View workflow job for this annotation

GitHub Actions / test

'"#imports"' has no exported member named 'defineNitroPlugin'. Did you mean 'defineNuxtPlugin'?

export default defineNitroPlugin((nitroApp) => {

Check failure on line 3 in src/runtime/server/plugins/oauth.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'nitroApp' implicitly has an 'any' type.
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) => {

Check failure on line 10 in src/runtime/server/plugins/oauth.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'html' implicitly has an 'any' type.
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 6262001

Please sign in to comment.