πAuthentication
Authentication Domain
Overview
The authentication domain within the FoodService application encompasses the functionalities related to user authentication, authorization, and user management. This domain ensures secure access to the application's resources by verifying the identity of users and managing their roles and permissions.
Components
AuthController
The AuthController is responsible for handling incoming requests related to user authentication and authorization. It includes endpoints for user registration (SignUp), user login (SignIn), adding users to administrative roles (AddUserToAdminRole), retrieving the current user (GetCurrentUser), listing users (ListUsers), and retrieving user details (GetUserDto). This controller enforces authorization rules using ASP.NET Core's authorization attributes, restricting access to certain endpoints based on user roles.
AuthCommand
The AuthCommand class implements the business logic for authentication operations defined in the IAuthCommand interface. It delegates the execution of these operations to the IAuthService. The class handles user sign-up, sign-in, adding users to admin roles, retrieving the current user, listing users, and retrieving user DTOs. It encapsulates the responses using ResponseCommon objects to indicate the success or failure of operations.
AuthService
The AuthService class provides implementations for authentication-related operations defined in the IAuthService interface. It interacts with repositories, ASP.NET Identity's user manager, configuration settings, and HTTP context accessor to perform operations such as listing users, retrieving user details, updating and deleting users, user sign-up, adding users to admin roles, user sign-in, and retrieving the current user. This class utilizes ASP.NET Identity for user management and JWT authentication for generating authentication tokens.
OpenAPI Endpoints
/api/Auth/sign-up
/api/Auth/sign-upHTTP Method: POST
Description: Registers a new user.
Request Body: Accepts a JSON object of type
SignUpDto.Response: Returns a
BooleanResponseCommonindicating the success or failure of the operation.Flowchart:
|--- Start | | | |--- Call SignUp method of AuthCommand | | | | | |--- Call SignUp method of AuthService | | | | | | | |--- Check if username already exists | | | | | | | | | |--- If exists, throw ArgumentException "Username already exists" | | | | | | | |--- Check if email already exists | | | | | | | | | |--- If exists, throw ArgumentException "Email already exists" | | | | | | | |--- Create a new ClientUser with the provided information | | | | | | | |--- Call _userManager.CreateAsync to create the user | | | | | | | | | |--- If fails, throw ArgumentException with error description | | | | | | | |--- Check if this is the first user | | | | | | | | | |--- If true, call _userManager.AddToRoleAsync to add the user to the Admin role | | | | | | | | | | | |--- If fails, throw ArgumentException "Failed to add user to admin role" | | | | | | | |--- Return true if signup is successful | | | | | |--- Return ResponseCommon<bool> indicating success or failure to the SignUp method of AuthCommand | | |--- End
/api/Auth/sign-in
/api/Auth/sign-inHTTP Method: POST
Description: Signs in a user.
Request Body: Accepts a JSON object of type
SignInDto.Response: Returns an
SsoDtoResponseCommoncontaining the authentication token and user information.Flowchart:
/api/Auth/add-user-to-admin-role
/api/Auth/add-user-to-admin-roleHTTP Method: POST
Description: Adds a user to the administrator role.
Request Body: Accepts an integer representing the user ID.
Response: Returns a
BooleanResponseCommonindicating the success or failure of the operation.Flowchart:
/api/Auth/get-current-user
/api/Auth/get-current-userHTTP Method: GET
Description: Retrieves the currently authenticated user.
Response: Returns a
UserBaseResponseCommoncontaining information about the current user.Flowchart:
/api/Auth/list-users
/api/Auth/list-usersHTTP Method: GET
Description: Lists all users.
Response: Returns a
ClientUserListResponseCommoncontaining a list of users.Flowchart:
/api/Auth/get-userdto
/api/Auth/get-userdtoHTTP Method: GET
Description: Retrieves a user DTO by ID.
Parameters: Accepts an integer
idas a query parameter.Response: Returns a
UserDtoResponseCommoncontaining the user DTO.Flowchart:
References
Last updated