Replies: 3 comments
-
Found a way to get a session on server API: const { user } = await getServerSession({ req, res }, nextAuthOptions); However, this method does not init JWT-callback. Is there any way to change user information on the server API side? |
Beta Was this translation helpful? Give feedback.
-
It would be nice then to have some |
Beta Was this translation helpful? Give feedback.
-
I solved my problems in this way, I update session-cookie and set with response: import { setCookie } from '@utils/server/cookie';
import { encode, getToken } from 'next-auth/jwt';
import { nextAuthOptions } from '@pages/api/auth/[...nextauth]';
const secret = process.env.COOKIE_KEY_SECRET;
// ...
// ...
// ---Server API function---
const token = await getToken({ req, secret });
const _user = token.user;
_user.phone = '+2222';
const _cookie = {
name : '__Secure-next-auth.session-token',
options: {
httpOnly: true,
sameSite: 'lax',
path : '/',
secure : true,
expires : addMilliseconds(Date.now(), nextAuthOptions.session.maxAge),
},
value: await encode({
token: { ...token, user: _user },
secret
})
};
setCookie(res, _cookie);
// ... But the session is not updated in export async function getServerSideProps(context) {
const { nextAuthOptions } = require('@pages/api/auth/[...nextauth]');
let user = null;
try {
const session = await getServerSession(context, nextAuthOptions);
user = session?.user ?? null;
} catch (error) {
context.req.log.error(error);
}
return {
props: { user },
};
} If there is a simpler and better method, please tell me!!! 🙏 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I understand correctly that my problem is similar to the one indicated in this discussion - #2604.
And the method can't provide me with the current session from the received request yet?
However, I can still get a session from the JWT token, although it seems to me that this is not quite the right solution
The project uses a secure HTTPS connection with redirection via TCP connection. On a par with protected cookies.
Beta Was this translation helpful? Give feedback.
All reactions