- Notifications
You must be signed in to change notification settings - Fork6
An example of a full-stack application for Authentication and Authorization, build with ASP.NET Core 9.0 (Minimal API) and Angular 19.
License
Gramli/AuthApi
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This full-stack solution demonstrates user registration, login, and role-based access control using Angular and .NET. The backend showcasesAuthentication andAuthorization withJWT tokens, demonstrating the use of Authorization policies inminimal API endpoints and adding custom claims through middleware. These are all implemented following Clean Architecture and various design patterns. The frontend illustrates managing JWT tokens usingguards andinterceptors, with all components implemented asstandalone components andsignals.
Example API allows to:
- register user
- login user
- change user role
- get user and service info
Endpoints use different types of authorization policies.
- .NET SDK 9.0+
- Angular CLI 19+
- Node.js 20.11.1+
To install the project using Git Bash:
- Clone the repository:
git clone https://github.com/Gramli/AuthApi.git
- Navigate to the project directory:
cd AuthApi/src
- Install the backend dependencies:
dotnet restore
- Navigate to the frontend directory and install dependencies:
cd Auth.Frontendnpm install
Expected IDE
- Backend: Visual Studio 2019+ or JetBrains Rider 2024.2.7+
- Frontend: Visual Studio Code 1.94.2+ or WebStorm 2024.2.4+
Run Frontend
- Open theAuth.Frontend project folder:
- In WebStorm, use the run or debug button to start the project.
- In VS Code, run the project in the terminal using the command
ng serve
.
- In your browser, navigate tohttp://localhost:4200/.
- Open theAuth.Frontend project folder:
Run Backend
- Open theAuthSol.sln project in Rider or Visual Studio.
- Use the run button to start the backend project.
Once both the frontend and backend are running, you’re all set to start using the app. Enjoy! :)
Select theAuth.API startup item in VS or Rider and try it.
- Go to Tests/HttpDebugTests folder and opendebug-tests.http file in VS2022
- Send Login request
- Obtain jwtToken from response and use it in another requests in Authorization header
The primary goal of this project is to create a practical example of authorization and authentication using Minimal API and Clean Architecture, while also enhancing my skills with Angular.
The backend followsClean Architecture, with the application layer split intoCore andDomain projects:
- TheCore project contains business rules.
- TheDomain project holds business entities.
- CQRS Pattern: Separates handlers into commands and queries, with repositories structured similarly.
- No MediatR: Minimal API supports injecting handlers directly into endpoint map methods, eliminating the need forMediatR.
- Result Pattern: Uses theResult pattern (viaFluentResults package) instead of throwing exceptions. Each handler returns an
HttpDataResponse
object containing data, error messages, and the HTTP response code.
The Angular frontend is organized into two main folders:
- Core: Contains "feature" components (each with specific feature logic).
- Shared: Stores common components, services, and extensions shared between feature components.
This example demonstrates JWT token management on the client side. After obtaining the token from the API, it is stored in local storage via theJwtTokenService. TheAuthorizeGuard checks if the client already has a token to protect routes, andauthInterceptor automatically adds the token header to every request.
The project usesPrimeNG andPrimeFlex for styling and layout.