atinux / nuxt-auth-utils

Minimal Auth module for Nuxt 3.
MIT License
818 stars 79 forks source link

Help: How update session user in client side? #160

Closed chz closed 6 days ago

chz commented 1 week ago

Hi @atinux. Thanks for amazing work on it!

I need help. How can i update session.user object on client side? Lat say i have socket and i need update unread key inside user object from socket data.unread. or i must call server route and update it in serverside? every time when i get socket updates?

await setUserSession(event, {...}) must be used in server side and need event.

atinux commented 1 week ago

If you trust the sockets, just use a ref() on client-side it should be enough, like unreadNotifications = ref(0).

chz commented 1 week ago

@atinux no i mean we have salted and hashed cookie where user object inside it. how can o update it?

Barbapapazes commented 1 week ago

@atinux no i mean we have salted and hashed cookie where user object inside it. how can o update it?

You must update using a server endpoint. A session is, for the client, a black box and this is by design. Imagine all the potential security issues if you were able to change data inside your session without by yourself, without asking a server (that will verified everything).

On your client, you can re-fetch (const { fetch } = useUserSession(event)) to retrieve new session data.

chz commented 1 week ago

@atinux no i mean we have salted and hashed cookie where user object inside it. how can o update it?

You must update using a server endpoint. A session is, for the client, a black box and this is by design. Imagine all the potential security issues if you were able to change data inside your session without by yourself, without asking a server (that will verified everything).

On your client, you can re-fetch (const { fetch } = useUserSession(event)) to retrieve new session data.

I know this. But i want update only user.[key] in client side.Best option save user data to state?

atinux commented 6 days ago

You can make a copy of the user into another ref() if you want @chz

chz commented 6 days ago

You can make a copy of the user into another ref() if you want @chz

Thank you.