W
Wasp•4w ago
nole

How do you access a user's identity through a query?

Context Myapp is configured to use usernameAndPassword auth method.
app TodoApp {
wasp: {
version: "^0.16.3"
},
title: "TodoApp",
auth: {
// Tells Wasp which entity to use for storing users.
userEntity: User,
methods: {
// Enable username and password auth.
usernameAndPassword: {}
},
// We'll see how this is used in a bit.
onAuthFailedRedirectTo: "/login"
}
}
app TodoApp {
wasp: {
version: "^0.16.3"
},
title: "TodoApp",
auth: {
// Tells Wasp which entity to use for storing users.
userEntity: User,
methods: {
// Enable username and password auth.
usernameAndPassword: {}
},
// We'll see how this is used in a bit.
onAuthFailedRedirectTo: "/login"
}
}
s I am trying to show an entity based off of a parameter in the URL šŸ“„ main.wasp
// Wasp: `version: "^0.16.3"`
route UserPagesRoute { path: "/pages/:username", to: UserPages }
page UserPages {
component: import { UserPages } from "@src/UserPages"
}
// Wasp: `version: "^0.16.3"`
route UserPagesRoute { path: "/pages/:username", to: UserPages }
page UserPages {
component: import { UserPages } from "@src/UserPages"
}
My attempt I thought this was the right way to do it but, šŸ“„ queries.ts
// username is a parameter that comes from the URL
export const getUserPages = async ({ username }: { username: string }, context) => {
console.log(`1 username: ${username}`)

//Return all the pages with the author of `username`
return context.entities.Page.findMany({
// I don't think I am formatting the `where` correctly
where: { user: { identities: { username: username } } },
orderBy: { id: 'asc' },
})
}
// username is a parameter that comes from the URL
export const getUserPages = async ({ username }: { username: string }, context) => {
console.log(`1 username: ${username}`)

//Return all the pages with the author of `username`
return context.entities.Page.findMany({
// I don't think I am formatting the `where` correctly
where: { user: { identities: { username: username } } },
orderBy: { id: 'asc' },
})
}
I'm getting this error:
[ Server!] Unknown argument `identities`. Available options are marked with ?.
[ Server!] Unknown argument `identities`. Available options are marked with ?.
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
kapa.ai
kapa.ai•4w ago
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:
export const getUserPages = async ({ username }: { username: string }, context) => {
console.log(`1 username: ${username}`)

// First, find the user with the given username
const user = await context.entities.User.findFirst({
where: { username: username }
});

if (!user) {
return [];
}

// Then find pages authored by this user
return context.entities.Page.findMany({
where: { userId: user.id },
orderBy: { id: 'asc' },
});
}
export const getUserPages = async ({ username }: { username: string }, context) => {
console.log(`1 username: ${username}`)

// First, find the user with the given username
const user = await context.entities.User.findFirst({
where: { username: username }
});

if (!user) {
return [];
}

// Then find pages authored by this user
return context.entities.Page.findMany({
where: { userId: user.id },
orderBy: { id: 'asc' },
});
}
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:
entity User {=psl
id Int @id @default(autoincrement())
username String @unique
// other fields...
psl=}
entity User {=psl
id Int @id @default(autoincrement())
username String @unique
// other fields...
psl=}
2. Your Page entity should have a relation to the User entity:
entity Page {=psl
id Int @id @default(autoincrement())
userId Int
user User @relation(fields: [userId], references: [id])
// other fields...
psl=}
entity Page {=psl
id Int @id @default(autoincrement())
userId Int
user User @relation(fields: [userId], references: [id])
// other fields...
psl=}
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?
Carlos
Carlos•3w ago
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.
nole
noleOP•3w ago
Yes, thank you very much!

Did you find this page helpful?