-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting Auth Session in a server component, Set CallbackURL issue! #483
Comments
Hi @aifirstd3v @divyam234 Do you have any idea? |
@aifirstd3v If you are using nextjs you should use next-auth only as both of them are tightly coupled. Your import { Auth } from '@auth/core'
import type { Session } from '@auth/core/types'
import type { JWT } from '@auth/core/jwt'
import type { AdapterUser } from '@auth/core/adapters'
type AuthUser = {
session: Session
token?: JWT
user?: AdapterUser
}
function getServerSession(req:Request) {
const config = initAuthConfig() // Authjs config
config.secret ??= process.env.AUTH_SECRET
config.basePath ??= '/api/auth'
config.trustHost = true
const origin = process.env.AUTH_URL ? new URL(process.env.AUTH_URL ).origin : new URL(c.req.url).origin
const request = new Request(`${origin}${config.basePath}/session`, {
headers: { cookie: req.headers.get('cookie') ?? '' },
})
let authUser: AuthUser = {} as AuthUser
const response = (await Auth(request, {
...config,
callbacks: {
...config.callbacks,
async session(...args) {
authUser = args[0]
const session = (await config.callbacks?.session?.(...args)) ?? args[0].session
const user = args[0].user ?? args[0].token
return { user, ...session } satisfies Session
},
},
})) as Response
const session = (await response.json()) as Session | null
return session && session.user ? authUser : null
} You can add this in Nextjs middleware |
@divyam234 Thing is how to call the getServerSession(req) in server components with Request parameter? I don't know how to pass the req in nextjs server component.. Also, setting callbackUrl doesn't work. After logging, there is no way return to the callback url that I set. Well..If I use this auth middleware in react 19 with server components, I think it would be very useful to have such a feature for a server component. |
@aifirstd3v you dont have to use this in server component use this in nextjs midleware which gives you request object and run this for client routes you want to secure. |
@aifirstd3v Of course, it should work for the NextJS middleware because it provides the Request/Response. I just wanted to know how to use such a thing in server components freely :). |
The original problem is probably related to #537 (which now includes a fix) - |
I'am developing a simple NextJS app with hono & auth.js.
My Question is
setCookie
to overwrite the cookie, it doesn't work.For sure, The Client useSession works like the official document(readme).
But, I'd like to get an auth session from a server component in NextJS. So, I created a hook for this purpose.
However, The session is always Null although I have've signed-in and had an auth session after github(provider) login.
I need get a session in server components not client components because of a special purpose(to check the auth status in the server more securely without evaluating a client page component. )
Next-Auth library has a getServerSession() but it seems that hono auth.js middleware doesn't have such a thing.
Any idea would be helpful 👍
The text was updated successfully, but these errors were encountered: