#How to implement next-auth with convex?

15 messages · Page 1 of 1 (latest)

weary bough
#

Hi!
I am building a next.js application. The basic idea is to create teams/projects to work together in X,Y,Z stuff. I am having some problems with authentication, I set up next-auth with some providers and the sessions work fine. But when it comes to Convex, I don't know how to protect data (only allow memebers of a team access the data of that team). And when an user authenticates if its new I would like to add it to the DB else just access that user data from the DB.

Thanks for your time!

hexed ridge
#

Hey there!

You generally want to set up a users table in Convex to store your users in, and sync relevant data from your auth provider, typically the id used by the auth provider is one such field.

When the user signs up or logs in, get the convex user based on the auth info. If there's no user, that's when you create one.

#

Once you have a user from your Convex table, you can use that for any authorization across your app. For example if you only want members of a given team to be able to view that team's projects, you can write a function that takes the current Convex user document and asserts team membership, and run that before executing a privileged query.

weary bough
#

Amazing! Thank you very much. I'll try to implement it

wide anchor
rotund sun
wide anchor
rotund sun
wide anchor
ripe fjord
sage parrot
tulip sable
#

@ripe fjord this is a nice start, but it seems to cover only gating routes based on NextAuth. I think folks here would want to combine this with a way to authenticate the client (which NextAuth doesn’t make easy, but could be done via session IDs or custom JWTs).