Skip to content

Assignment-2#9

Open
prashant1shukla wants to merge 4 commits intomainfrom
Assignment_2
Open

Assignment-2#9
prashant1shukla wants to merge 4 commits intomainfrom
Assignment_2

Conversation

@prashant1shukla
Copy link
Owner

Screenshot 2024-05-18 183317 Screenshot 2024-05-18 183421 Screenshot 2024-05-18 183533 Screenshot 2024-05-18 183615 Screenshot 2024-05-18 183644

[HttpPost]
public IActionResult Login(LoginViewModel login)
{
var user = _authenticationService.AuthenticateUser(login);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use var

{
var user = _authenticationService.AuthenticateUser(login);
var token = _tokenService.GenerateToken(user);
return Ok(new { token });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should have separate DTOs/View Model for this response.

Comment on lines +27 to +28
var user = _authenticationService.AuthenticateUser(login);
var token = _tokenService.GenerateToken(user);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can write a function in service itself which first authenticates the user and then directly returns the token

Comment on lines +24 to +27
if (!_userService.RegisterUser(user))
{
return Conflict("Username already exists");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be in the service itself. You can throw custom exception and catch it in the middleware directly and then can convert the message.

Comment on lines +27 to +38
var username = User.Identity.Name;
// Comparing the usernames using GetUserByUsername function
var user = _userService.GetUserByUsername(username);

//For invalid token
if (user == null)
{
return NotFound("User not found");
}

// Returning the user data if a valid token is passed
return Ok(user);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should also be in the service layer with a custom exception to handle user not found

Comment on lines +22 to +41
public string GenerateToken(UserModel user)
{
// providing the security key and credentials for token generation
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

// Taking data from appsettings.json
var token = new JwtSecurityToken(
issuer: _config["Jwt:Issuer"],
audience: _config["Jwt:Audience"],
claims: new[]
{
new Claim(ClaimTypes.Name, user.Username)
// Add additional claims if needed (e.g., user roles)
},
expires: DateTime.UtcNow.AddMinutes(30), // Token expires in 30 minutes
signingCredentials: credentials
);

return new JwtSecurityTokenHandler().WriteToken(token);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having this as a separate service function you can create a jwtUtil class and call use this function in authentication service directly.


//Post request to register a user
[HttpPost]
public IActionResult Register(UserModel user)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of directly using UserModel use DTOs/ViewModel

Comment on lines +12 to +18
if (UserDataStore.Users.Any(u => u.Username == user.Username))
{
return false; // Username already exists
}

//If the username already does not exist, make new registration
UserDataStore.Users.Add(user);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encrypt the password before saving.

}

// Function to get the User data, it is used in User Controller
public UserModel GetUserByUsername(string username)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use DTO/View Model as the return type.

// Check if password contains any special characters
if (password.Any(char.IsPunctuation) || password.Any(char.IsSymbol))
{
return new ValidationResult("Password cannot contain special characters");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this for Name validation and returning a message related to password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants