W
Wasp•4w ago
Bruno Njeru

User context

How do I add contexts for different types of users? For example user type a (normal user), user type b and user type c (both with a dashboard like admin)?
10 Replies
kapa.ai
kapa.ai•4w ago
Hi @Bruno Njeru. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To add contexts for different types of users in Wasp, including normal users and those with dashboard access like admins, you can implement a role-based access control (RBAC) system. While Wasp doesn't have built-in RBAC support yet, you can create a custom solution. Here's how you can approach this: 1. Add a role field to your User entity:
entity User {
id Int @id @default(autoincrement())
role String // e.g., "normal", "typeB", "typeC"
// other fields...
}
entity User {
id Int @id @default(autoincrement())
role String // e.g., "normal", "typeB", "typeC"
// other fields...
}
2. In your queries and actions, check the user's role to determine access:
import { HttpError } from 'wasp/server'

export const someAction = async (args, context) => {
if (!context.user) {
throw new HttpError(401)
}

if (context.user.role !== 'typeB' && context.user.role !== 'typeC') {
throw new HttpError(403)
}

// Proceed with action for typeB and typeC users
}
import { HttpError } from 'wasp/server'

export const someAction = async (args, context) => {
if (!context.user) {
throw new HttpError(401)
}

if (context.user.role !== 'typeB' && context.user.role !== 'typeC') {
throw new HttpError(403)
}

// Proceed with action for typeB and typeC users
}
3. For pages that require specific roles, use the authRequired property and check the user's role in the component:
page DashboardPage {
authRequired: true,
component: import Dashboard from "@client/admin/pages/DashboardPage"
}
page DashboardPage {
authRequired: true,
component: import Dashboard from "@client/admin/pages/DashboardPage"
}
import { useAuth } from 'wasp/client/auth'

const Dashboard = () => {
const { data: user } = useAuth()

if (user.role !== 'typeB' && user.role !== 'typeC') {
return <div>Access denied</div>
}

// Render dashboard for typeB and typeC users
}
import { useAuth } from 'wasp/client/auth'

const Dashboard = () => {
const { data: user } = useAuth()

if (user.role !== 'typeB' && user.role !== 'typeC') {
return <div>Access denied</div>
}

// Render dashboard for typeB and typeC users
}
This approach allows you to create different contexts for various user types. You can extend this concept to include more granular permissions as needed. For more information on accessing the logged-in user and implementing authorization, you can refer to: - Accessing the logged-in user (https://wasp-lang.dev/docs/auth/overview#accessing-the-logged-in-user) - Updating Operations to Check Authentication (https://wasp-lang.dev/docs/tutorial/auth#updating-operations-to-check-authentication) Note that Wasp is planning to add built-in support for role-based access control in future versions, which will make this process more straightforward Github Issue: Support for Permissions (Access Control) (https://github.com/wasp-lang/wasp/issues/584). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Bruno Njeru, 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?
Bruno Njeru
Bruno NjeruOP•3w ago
Ok thanks will try that. How about adding a form for the two conexts user type b and user type c after they sign up. They are user type a by default but may wish to change their role. Something like https://settleasy.com/ which was made by wasp @kapa.ai
kapa.ai
kapa.ai•3w ago
Let me check the knowledge base for you again. One moment! :books: Sorry @Bruno Njeru, 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?
Bruno Njeru
Bruno NjeruOP•3w ago
@kapa.ai
kapa.ai
kapa.ai•3w ago
Let me check the knowledge base for you again. One moment! :books: Sorry @Bruno Njeru, 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?
Bruno Njeru
Bruno NjeruOP•3w ago
Ok @kapa.ai
kapa.ai
kapa.ai•3w ago
Let me check the knowledge base for you again. One moment! :books: Sorry @Bruno Njeru, 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
Filip•3w ago
Lol, looks like the bot broke 😄 There isn't a first-class construct for an extra form on signup, but you can easily implement extra forms for different types of users on a page Wasp will link to after user successfully signs up. For the redirect, you can use this: https://wasp-lang.dev/docs/auth/overview#onauthsucceededredirectto-string @Bruno Njeru Also take a look at: - https://github.com/wasp-lang/wasp/issues/2277
Bruno Njeru
Bruno NjeruOP•3w ago
Ok thanks
MEE6
MEE6•3w ago
Wohooo @Bruno Njeru, you just became a Waspeteer level 1!
Want results from more Discord servers?
Add your server