How do you access a user's identity through a query?
Context
Myapp is configured to use
usernameAndPassword
auth method.
s
I am trying to show an entity based off of a parameter in the URL
š main.wasp
My attempt
I thought this was the right way to do it but,
š queries.ts
I'm getting this error:
What I've tried so far
- I have tried logging out the entities to try and find the right way to access the username.
- I have tried talking to the AI in my IDE, but the suggestions have been to add an identity's field to the user schema.
- I think wasp attaches this relation to the user entity without being explicitly defined.3 Replies
Hi @nole. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In Wasp, when using the
usernameAndPassword
auth method, the username is not directly accessible through the identities
field in the way you're trying to query it. Let me explain how to properly access a user's identity in a query.
Accessing User Identity in Wasp
When Wasp sets up authentication, it creates several entities in the background, including Auth
and AuthIdentity
. However, these aren't directly queryable in the way you're attempting.
The Correct Approach
For your use case, you need to query the User entity first, and then access the pages associated with that user:
Important Notes
1. In your User entity, you need to have a username
field defined. When using usernameAndPassword
auth, the User entity needs to have the username field directly on it:
2. Your Page entity should have a relation to the User entity:
3. If you need to access auth-specific information, Wasp provides helper functions like getUsername()
that you can use on the user object as documented here.
The error you're seeing (Unknown argument 'identities'
) occurs because the identities
field is not directly accessible in the Prisma query. The auth identities are managed by Wasp internally through the Auth entity as explained in the documentation.
Remember that when using usernameAndPassword
auth, the username field should be directly on your User entity, not accessed through the auth identities.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @nole, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Hi @nole, you have an example on how access the currently-logged-in user from a server operation in our docs:
https://wasp.sh/docs/auth/overview#on-the-server
Is that useful?
Overview | Wasp
Auth is an essential piece of any serious application. That's why Wasp provides authentication and authorization support out of the box.
Yes, thank you very much!