Skip to content

Commit

Permalink
feat(User):add sevices
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdijafariii committed Aug 13, 2024
1 parent 220a93e commit 6e45404
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions AnalysisData/AnalysisData/JwtService/JwtService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public async Task<string> GenerateJwtToken(string userName)
var roles = user.UserRoles;
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, userName),
new Claim("Name", userName),
};
foreach (var role in roles)
{
var result = await _roleRepository.GetRole(role.Id);
claims.Add(new Claim(ClaimTypes.Role, result.RoleName));
claims.Add(new Claim("Roles", result.RoleName));
}

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));

Check warning on line 38 in AnalysisData/AnalysisData/JwtService/JwtService.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.
Expand Down
40 changes: 40 additions & 0 deletions AnalysisData/AnalysisData/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using AnalysisData.Data;
using AnalysisData.Repository.UserRepository.Abstraction;
using AnalysisData.UserManage.LoginModel;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace AnalysisData.Services;

public class UserService : ControllerBase
{
private readonly IUserRepository _userRepository;

public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public async Task<IActionResult> Login([FromBody] UserLoginModel userLoginModel)
{
var user = await _userRepository.GetUser(userLoginModel.userName);
if (user is null)
{
return NotFound($"Unable to load user with username '{userLoginModel.userName}'");
}

if (user.Password != userLoginModel.password)
{
return Unauthorized("Invalid username or password.");
}

return Ok(new
{
Massage = "welcome",
Token = "سشی"
});



}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnalysisData.UserManage.LoginModel;

public class UserLoginModel
{
public string userName { get; set; }

Check warning on line 5 in AnalysisData/AnalysisData/UserManage/LoginModel/UserLoginModel.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable property 'userName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string password { get; set; }

Check warning on line 6 in AnalysisData/AnalysisData/UserManage/LoginModel/UserLoginModel.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable property 'password' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public bool rememberMe { get; set; }
}
2 changes: 1 addition & 1 deletion AnalysisData/AnalysisData/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=mohaymen;Username=postgres;Password=@Database20;Timeout=300"
"DefaultConnection": "Host=localhost;Database=mohaymen;Username=postgres;Password=mahdijm;Timeout=300"
},

"AllowedHosts": "*"
Expand Down
1 change: 1 addition & 0 deletions AnalysisData/global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sdk": {
"version": "8.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
Expand Down

0 comments on commit 6e45404

Please sign in to comment.