Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions SnakeAid.Service/Implements/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ public async Task<ApiResponse<AuthResponse>> RegisterAsync(RegisterRequest reque
null, false, "Email is already in use.", HttpStatusCode.BadRequest, "EMAIL_IN_USE");
}

//Map role
if (targetRole == null)
{
targetRole = RegisterRole.Member;
}

if (!RegisterRoleMap.TryGetValue(targetRole.Value, out var role))
{
return ApiResponseBuilder.CreateResponse<AuthResponse>(
null, false, "Invalid role.", HttpStatusCode.BadRequest, "INVALID_ROLE");
}

// Create new account
var user = new Account
{
Expand All @@ -79,7 +91,7 @@ public async Task<ApiResponse<AuthResponse>> RegisterAsync(RegisterRequest reque
FullName = request.FullName ?? string.Empty,
PhoneNumber = request.PhoneNumber,
IsActive = false,
Role = AccountRole.User,
Role = role,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
Expand All @@ -95,10 +107,7 @@ public async Task<ApiResponse<AuthResponse>> RegisterAsync(RegisterRequest reque
null, false, "Registration failed.", HttpStatusCode.UnprocessableEntity, "VALIDATION_ERROR", errors);
}

if (targetRole == null)
{
targetRole = RegisterRole.Member;
}


switch (targetRole)
{
Expand Down Expand Up @@ -524,4 +533,15 @@ private async Task<bool> ValidateRefreshTokenAsync(Account user, string refreshT
}

#endregion

#region Private Methods
private static readonly Dictionary<RegisterRole, AccountRole> RegisterRoleMap =
new()
{
{ RegisterRole.Member, AccountRole.User },
{ RegisterRole.Rescuer, AccountRole.Rescuer },
{ RegisterRole.Expert, AccountRole.Expert }
};
#endregion
}