-
Notifications
You must be signed in to change notification settings - Fork 382
-
Hey everyone!
I try to configure a simple integration with Google OAuth and see that under the hood Modern.js migrated to Hono which delivers with https://www.npmjs.com/package/@hono/oauth-providers
How I can use it and bind to Modern.js middlewares? I see two types middleware: MiddlewareHandler and UnstableMiddleware. Which from them are appropriate for this integration and how I can refactor the code from Hono:
app.use(
'/google',
googleAuth({
client_id: Bun.env.GOOGLE_ID,
client_secret: Bun.env.GOOGLE_SECRET,
scope: ['openid', 'email', 'profile'],
})
)
app.get('/google', (c) => {
const token = c.get('token')
const grantedScopes = c.get('granted-scopes')
const user = c.get('user-google')
return c.json({
token,
grantedScopes,
user,
})
})
To Modern.js ecosystem?
Thank you so much in advance!
Beta Was this translation helpful? Give feedback.
All reactions
Thanks for your feedback, Using MiddlewareHandler, the UnstableMiddleware is deprecated.
https://modernjs.dev/zh/guides/advanced-features/web-server.html#middleware
You can try to code it like this:
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime'; const googleAuthHandler: MiddlewareHandler = googleAuth({ client_id: Bun.env.GOOGLE_ID, client_secret: Bun.env.GOOGLE_SECRET, scope: ['openid', 'email', 'profile'], }); const googleRouteHandler: MiddlewareHandler = (c) => { const token = c.get('token') const grantedScopes = c.get('granted-scopes') const user = c.get('user-google') return c.json({ token, grantedScopes, user, ...
Replies: 2 comments 1 reply
-
Thanks for your feedback, Using MiddlewareHandler, the UnstableMiddleware is deprecated.
https://modernjs.dev/zh/guides/advanced-features/web-server.html#middleware
You can try to code it like this:
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime'; const googleAuthHandler: MiddlewareHandler = googleAuth({ client_id: Bun.env.GOOGLE_ID, client_secret: Bun.env.GOOGLE_SECRET, scope: ['openid', 'email', 'profile'], }); const googleRouteHandler: MiddlewareHandler = (c) => { const token = c.get('token') const grantedScopes = c.get('granted-scopes') const user = c.get('user-google') return c.json({ token, grantedScopes, user, }) }) export default defineServerConfig({ middlewares: [ { name: 'google-auth', handler: googleAuthHandler, }, { name: 'google-route-handler', path: '/google', handler: googleRouteHandler, }, ], });
If you have any questions, feel free to comment. We just released the Hono based Middleware recently :) @keepview cc
And wee do not expose the hono instance app directly because we are concerned that users might modify the default behavior we intend, which could cause unnecessary confusion.
For now, it seems that the Hono middleware can meet most demand. If it doesn’t, please let us know.
Beta Was this translation helpful? Give feedback.
All reactions
-
It works, thank you! ❤️
Beta Was this translation helpful? Give feedback.
All reactions
-
I will check, Thank you!
Beta Was this translation helpful? Give feedback.