Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How can I use multiple sessions at the same time #87

Closed
LrsChrSch opened this issue Apr 21, 2023 · 3 comments
Closed

Question: How can I use multiple sessions at the same time #87

LrsChrSch opened this issue Apr 21, 2023 · 3 comments

Comments

@LrsChrSch
Copy link

I'm using Oak (Deno) to create a simple API with user authentication and I've stumbled across a certain scenario:

  1. User1 makes a request. This triggers a middleware to authenticate that user with the db. Part of the request is an action that takes some time (for example upload some files or wait for a timeout). After that action the database should update the user record (or perform any query that needs User1's authentication)
  2. Log in User2 within the time it takes for the action from (2) to complete. This changes the authenticated state of the db object to User2
  3. User1's action completes, but the query fails because it now tries to perform the query of User1 using the Authentication of User2

I'm sure that I'm missing some fundamental part of how this should work. The main question is: How can I use a different session for each request so that every surreal session uses their own authentication and can't be mixed up?

I could of course create new surreal objects that instantiate their own connections every time ofc. But that seems really counter intuitive and most importantly: very slow.

Hope you can help!

@LrsChrSch
Copy link
Author

Alright, short update.

I managed to fix this by opening a new connection to SurrealDB on every request and attaching the db object to ctx.state. Which isn't great of course (would be nicer to just use 1 with different auth states as proposed here: #61).

The reason why I mentioned that this would be slow is because... it was. I used const db = new Surreal("http://localhost:8000/rpc"); to create the connection. Turns out that this is several seconds slower than using const db = new Surreal("http://127.0.0.1:8000/rpc"); for some reason.

With the faster speed, invoking a new connection everytime is fine for my use case. I'll leave this open for now in case anyone has a better solution. But feel free to close this if you feel that this isn't relevant anymore :)

@kearfy
Copy link
Member

kearfy commented May 4, 2023

Closing this as a duplicate of #61

Anyways, to help you out: This is not possible over WebSockets as of now. The client drivers for each language are merely a representation of the APIs out there, and the WS API in SurrealDB currently does not support this :)

@kearfy kearfy closed this as completed May 4, 2023
@kearfy
Copy link
Member

kearfy commented May 4, 2023

In #95 I'm adding an HTTP client, you could have multiple http clients with different credentials

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants