Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Modern.js and Hono OAuth Middleware #7274

Answered by zllkjc
RuBAN-GT asked this question in Q&A
Discussion options

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!

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
1 reply
Comment options

It works, thank you! ❤️

Answer selected by RuBAN-GT
Comment options

I will check, Thank you!

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /