User Management system Angular | angular 18 Projects
Table of Contents
Introduction
In this tutorial, we will create a User Management system using Angular 18 integrated with a .NET API. This application will have two main roles: Admin and User. The Admin can manage users — creating, editing, and deleting them — while Users can only view their details and upload their profile pictures. This project will guide you through setting up the Angular application and implementing the necessary features step-by-step.
Step 1: Set Up the Angular Project
-
Create a New Angular Project:
- Use the Angular CLI to create a new project:
ng new user-management-app
- Navigate into the project folder:
cd user-management-app
- Use the Angular CLI to create a new project:
-
Install Bootstrap:
- Install Bootstrap using npm:
npm install bootstrap
- Add Bootstrap to your
angular.json
file under thestyles
array:"styles": [ "src/styles.css", "node_modules/bootstrap/dist/css/bootstrap.min.css" ]
- Install Bootstrap using npm:
-
Set Up Routing:
- Create a routing module if not already present, and set up routes for Login, User List, Create User, and Layout components.
Step 2: Create Required Components
-
Generate Components:
- Use Angular CLI to create the following components:
ng generate component components/Login ng generate component components/Layout ng generate component components/UserList ng generate component components/CreateUser
- Use Angular CLI to create the following components:
-
Set Up Component Structure:
- Ensure the
Layout
component contains a router outlet to load child components based on user roles.
- Ensure the
Step 3: Implement User Login
-
Design Login UI:
- Create a login form in the
Login
component HTML file with fields for email and password.
- Create a login form in the
-
Handle Login Logic:
- Create a service to handle API calls. For login, create a method to call the login API:
login(email: string, password: string) { return this.httpClient.post('API_URL/login', { email, password }); }
- Create a service to handle API calls. For login, create a method to call the login API:
-
Bind Form Data:
- Use Angular's two-way binding (
ngModel
) to bind form inputs to a login object in the component.
- Use Angular's two-way binding (
-
Navigate Based on Role:
- After successful login, check the user’s role and navigate accordingly (to User List for Admin or Create User for User).
Step 4: Implement User Management for Admin
-
Display User List:
- In the
UserList
component, fetch the list of users from the API and display them in a table.
- In the
-
Add Create User Functionality:
- Include a button to navigate to the
CreateUser
component where new users can be added.
- Include a button to navigate to the
-
Handle User Creation:
- Create a form in the
CreateUser
component to capture user details. On submission, call the API to create the user.
- Create a form in the
-
Implement Edit and Delete Functions:
- For each user in the list, provide Edit and Delete buttons.
- Edit: Navigate to the
CreateUser
component with the user ID to populate the form with existing data. - Delete: Confirm deletion and call the delete API.
- Edit: Navigate to the
- For each user in the list, provide Edit and Delete buttons.
Step 5: Implement User Profile for Regular Users
-
User Dashboard:
- After login, regular users should be directed to the
CreateUser
component where they can view and update their profile.
- After login, regular users should be directed to the
-
Display User Information:
- Fetch the logged-in user's data from the API and display it in the form, allowing them to upload a profile picture.
-
Handle Profile Updates:
- When submitting the form, call the update API to save changes.
Conclusion
In this tutorial, we successfully built a User Management system with Angular 18 and a .NET API. We implemented role-based access, allowing admins to manage users while restricting regular users to their profiles.
Key Takeaways:
- Set up Angular with Bootstrap for styling.
- Create components and services to handle user authentication and management.
- Implement routing for a smooth user experience.
- Use Angular's reactive features (like
ngModel
) to manage form data easily.
Next Steps:
- Explore adding additional features like user roles management, better error handling, and user notifications.
- Deploy the application to a web server for real-world usage.