Why would one use iron-session instead of next-auth? Doesn't next-auth do normal username/password log-in in addition to Social (while iron-session only does the former)?
next-auth
does a lot of things like you said but it also makes you do things their way. So you have to model your database a certain way to make next-auth
work but it does come with a lot of helper functions and makes you write a lot less code.
iron-session
only does session management. You have to write all the auth logic with iron-session
which next-auth
handles for you automatically.
As far as which one is better: if you'd like complete control over your database then use iron-session
because sometimes next-auth
might not work and it's annoying. I faced an issue with it so ditched it but it just might work for you. iron-session
gives full control but you have to write the correct code. Personally, I'm facing issues with iron-session
as well with useUser
hook because I have to use react-query
and not swr
but it's probably my issue.
req.session.save()
), and not in a database, which you can do with Next-Auth using the database
option in config. –
Bawdyhouse .save()
to store session. when you logout, you have to use .destroy()
method. highly recommend checking out the official example → github.com/vercel/next.js/blob/canary/examples/… –
Garcon The session data is stored in encrypted cookies ("seals"). And only your server can decode the session data. There are no session ids, making iron sessions "stateless" from the server point of view.
–
Garcon iron-session
is lightweight library. It is simply used for session management. session
is not related only to authentication. For example, maybe you want to sign some data on one API endpoint, attached the signed data to the req.session
and you still need to reach this data on a different API endpoint iron-session
store data on the encrypted cookie and save it to the browser. From here:
Node.js stateless session utility using signed and encrypted cookies to store data. Works with Next.js, Express, and Node.js HTTP servers.
The session data is stored in encrypted cookies ("seals"). And only your server can decode the session data. There are no session ids, making iron sessions "stateless" from the server point of view.
On the other hand, next-auth
is a more robust authentication package. as the name says next-auth
, it is dedicated to authentication. It has too many authentication methods. It creates a session specifically for the user's login
© 2022 - 2024 — McMap. All rights reserved.