Which is proper way for api authentication for nextJs api? taking more time to give response

Hello im getting session and checking in api. here screenshot attached but just that api taking 2-3 seconds to give response which is the proper way for api authentication in nextJs? im using secondary database redis for session store. this api saves 3 key values to postgress using drizzle orm. whats the major improvements i need to do here? is it possible to do create server action and calling it from client component? i tried some error came.
No description
No description
9 Replies
bekacru
bekacru2w ago
try to measure how much getSession is taking specifically. If you use secondary storage it should be fast enough in general. Also look into cookie cache.
varundeva
varundevaOP2w ago
I was disabled cookie caching Maybe thats why causing much time However i started to use server actions One more thing @bekacru I have extended user table and added isOnboarded boolean That's to check user onboarded or not And I'm getting that key in session Once user onboarded that flag turns to true But my session coming from cache So its not turning to true till logout and login Or session cache time i set 10 minutes Is there any way to manually update session cache?
Ping
Ping2w ago
Side note: Make sure you have the nextCookies plugin if you haven't already! https://www.better-auth.com/docs/integrations/next#server-action-cookies
Is there any way to manually update session cache?
You can probably just fetch the session using your ORM, then manually set the cookie to be the newly updated. Also, I recommend indexing important fields in your database to allow queries to run faster: https://www.better-auth.com/docs/guides/optimizing-for-performance#using-indexes
varundeva
varundevaOP2w ago
Are you saying get session from db and update? I'm using secondary database also. There also i need to update the session basically Any function available to refresh the session?
Ping
Ping2w ago
Oh, you're using secondary DB. I'm unaware of any, sorry. Bekacru might know though.
varundeva
varundevaOP2w ago
Yep Forcefully logginout user or asking user to relogin wont looks good Else i have to make separate api to get that isOnboarded flag Now along with userdata that comes easily My plan is if user not onboarded redirecting them to onboard page
Ping
Ping2w ago
Yeah.
ItsTeegj
ItsTeegj2w ago
Not sure if this will help you, but I needed to do a similar thing which was update the session for some additional fields I had on the user. What I ended up doing was calling the updateUser method on the authclient just so that it updated the session with updated user field, while also making a separate call to my backend. I know it’s not the cleanest, but that worked for me. So in your case, you would call the updateUser method and set the isOnboarded to true. I also used router.refresh() from Nextjs too after that
varundeva
varundevaOP2w ago
Now I'm updating user data from server action But my session is in secondary database redis Basically i want refresh the session in redis. Now even refresh page also not helping out It will call session api and gets same session from redis which is not updated

Did you find this page helpful?