the User query instead of the Auth user

User query instead of the Auth user
3 Replies
kapa.ai
kapa.ai2mo ago
Hi @drlcookie. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Wasp, the User entity and the Auth user are used in different contexts. The User entity is a "business logic user" which represents a user of your app. You can use this entity to store any information about the user that you want to store. For example, you might want to store the user's name or address. You can also use the user entity to define the relations between users and other entities in your app. For example, you might want to define a relation between a user and the tasks that they have created. You own the user entity and you can modify it as you wish. You can add new fields to it, remove fields from it, or change the type of the fields. You can also add new relations to it or remove existing relations from it. Here is an example of how you might define a User entity in your Wasp file:
entity User {=psl
id Int @id @default(autoincrement())
// Any other fields you want to store about the user
psl=}
entity User {=psl
id Int @id @default(autoincrement())
// Any other fields you want to store about the user
psl=}
(Source: Wasp Auth Entities Docs (https://wasp-lang.dev/docs/auth/entities#entities-explained)) On the other hand, the Auth user is used in the context of authentication. When a user signs in for the first time, Wasp creates a new user account and links it to the chosen auth provider account for future logins. The Auth, AuthIdentity and Session entities are created behind the scenes and are used to store the user's login credentials. You as the developer don't need to care about this entity most of the time. Wasp owns these entities. However, in the case you want to create a custom signup action, you will need to use the Auth and AuthIdentity entities directly. (Source: Wasp Social-auth Overview Docs (https://wasp-lang.dev/docs/auth/social-auth/overview#default-behavior)) In terms of querying, if you want to access the logged-in user in a client component, you can use the useAuth hook provided by Wasp. This hook is a thin wrapper over Wasp's useQuery hook and returns data in the same format. Here is an example of how you might use the useAuth hook in a React component:
import { useAuth, logout } from 'wasp/client/auth'
import { Link } from 'react-router-dom'
import Todo from '../Todo'

export function Main() {
const { data: user } = useAuth()

if (!user) {
return (
<span>
Please <Link to="/login">login</Link> or{' '}
<Link to="/signup">sign up</Link>.
</span>
)
} else {
return (
<>
<button onClick={logout}>Logout</button>
<Todo />
</>
)
}
}
import { useAuth, logout } from 'wasp/client/auth'
import { Link } from 'react-router-dom'
import Todo from '../Todo'

export function Main() {
const { data: user } = useAuth()

if (!user) {
return (
<span>
Please <Link to="/login">login</Link> or{' '}
<Link to="/signup">sign up</Link>.
</span>
)
} else {
return (
<>
<button onClick={logout}>Logout</button>
<Todo />
</>
)
}
}
(Source: Wasp Auth Overview Docs (https://wasp-lang.dev/docs/auth/overview#using-the-useauth-hook)) If you want to enrich the User object with more information, you would currently do additional queries in your React component for that Page. However, there is an open issue discussing the possibility of allowing a custom Query for fetching the user, per whole app or per Page, that would then be used to fetch the user (instead of using default User.find). This would allow the information to be added into user immediately upon fetching it for that page, so no additional queries need to be made. (Source: [Github Issue 1554](https://github.com/wasp-lang/wasp/issues ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @drlcookie, 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?
Filip
Filip2mo ago
Hey @drlcookie, try to describe your problem in more detail. That way, the bot and the team will have an easier time helping you
MEE6
MEE62mo ago
Wohooo @sodic, you just became a Waspeteer level 32!
Want results from more Discord servers?
Add your server