-2

I have a jwt token returned by surrealdb's signin() method.

How do I populate a user object on authenticated requests using this token?

chovy
  • 72,281
  • 52
  • 227
  • 295
  • Well usually don't populate user objects with tokens. The Token is sent as the value of the Authorization header in requests for validation. The server checks if the token is valid before the request is considered authentic. – Haroon Azhar Khan Oct 08 '22 at 08:14
  • Can you please share your code and what you're trying to achieve? – Haroon Azhar Khan Oct 08 '22 at 08:16
  • i'm trying to get a user object from a token ie `Authroization: Bearer ` header. Both oak and express can do this. – chovy Oct 08 '22 at 08:28
  • 1
    I don't think the token will have any user info, but where the request for authentication was initiated will have user data. example flow: 1- user gives details to your app. 2- your app sends a request with those credentials/data to surrealdb to be authenticated. 3- In the resolved block of the request i.e when you get a valid response 200. Handle the user's submitted data to create a user object and store the token in the session store to be sent out later with requests. Hope this helps. – Haroon Azhar Khan Oct 08 '22 at 08:40
  • That worked. I had to put the user object in the session along with the token. – chovy Oct 08 '22 at 10:26
  • Storing users is optional and depends on your implementation and design. – Haroon Azhar Khan Oct 08 '22 at 11:40

1 Answers1

1

You can't get user data from the token but you should use the user's provided details to create a Userobject. You will need to store the token in a session store or on the client side to send it in the future requests.