Managing User Access with Roles and Profiles
Managing User Access with Roles and Profiles
Controlling who can access what in your application is very important. That’s where roles and profiles help.
✅ What Are Roles?
Roles define what a user can do in the system.
π― Common Roles:
-
Admin – full control
-
Editor – can change content
-
Viewer – read-only access
-
User – regular access
π‘ Example:
✅ What Are Profiles?
Profiles store user-related information, like:
-
Name
-
Email
-
Preferences
-
Photo
-
Language
-
Theme
Profiles help personalize the experience but do not control permissions.
π§ Difference Between Roles and Profiles:
| Feature | Role | Profile |
|---|---|---|
| Purpose | Access control | User info & preferences |
| Editable by | Admin | User (mostly) |
| Affects | What user can do | How user sees the system |
π Why Manage Roles?
-
Prevent unauthorized access
-
Give the right access to the right person
-
Improve security
-
Separate concerns (Admin vs. User)
π ️ Implementing Role-Based Access in a Web App
Let’s use Node.js + Express + MongoDB + React for the example.
π MongoDB User Schema Example
π§ͺ Middleware to Check Role in Express.js
π Example Routes with Role Protection
π§π» React: Conditional Rendering Based on Role
π Frontend Route Guard (React + React Router)
π Profile Management in React
π Updating Role in Admin Panel
π Secure Practices
-
Always validate role on server-side
-
Do not trust frontend-only checks
-
Store role info securely (e.g., JWT or sessions)
-
Use HTTPS
-
Keep roles simple and easy to understand
π§ Bonus: Role Hierarchy (Optional)
You can define role levels:
π‘️ JWT Token with Role Example
Use this info to authorize routes without querying DB every time.
π¦ Summary Table
| Feature | Example |
|---|---|
| Role Field | admin, editor, viewer |
| Profile Data | Name, Theme, Language |
| Access Check | Express Middleware |
| UI Display | React Role-Based Components |
| Auth Storage | JWT or Session |
π§© Real-World Example:
Imagine an online learning platform:
| Role | Access Rights |
|---|---|
| Admin | Add/delete courses, manage users |
| Teacher | Upload materials, grade students |
| Student | View lessons, submit assignments |
You can create roles and apply access rules as shown above.
Learn Salesforce Development Training Course
Comments
Post a Comment