Skip to content

Commit

Permalink
fix(User):fix login (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdijafariii authored and AmirRezaZahedi committed Aug 13, 2024
1 parent b1472e6 commit b7265f3
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 16 deletions.
3 changes: 3 additions & 0 deletions AnalysisData/AnalysisData.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=AnalysisData_002FResources/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean></wpf:ResourceDictionary>
22 changes: 22 additions & 0 deletions AnalysisData/AnalysisData/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using AnalysisData.Services;
using AnalysisData.UserManage.LoginModel;
using Microsoft.AspNetCore.Mvc;

namespace AnalysisData.Controllers;
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
private readonly IUserService _userService;

public UserController(IUserService userService)
{
_userService = userService;
}
[HttpPost("login")]
public IActionResult Login([FromBody] UserLoginModel userLoginModel)
{
var userRoles = _userService.Login(userLoginModel);
return Ok(new { roles = userRoles });
}
}
45 changes: 45 additions & 0 deletions AnalysisData/AnalysisData/CookieService/CookieService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using AnalysisData.CookieSevice.abstractions;

namespace AnalysisData.CookieSevice;

public class CookieService : ICookieService
{
private readonly IHttpContextAccessor _httpContextAccessor;

public CookieService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}

public void SetCookie(string name, string token,bool rememberMe)
{
var options = new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Strict
};

if (rememberMe)
{
options.Expires = DateTimeOffset.UtcNow.AddDays(7);
}
else
{
options.IsEssential = true;
}

_httpContextAccessor.HttpContext.Response.Cookies.Append(name, token, options);
}

public string GetCookie(string name)
{
_httpContextAccessor.HttpContext.Request.Cookies.TryGetValue(name, out var value);
return value;
}

public void RemoveCookie(string name)
{
_httpContextAccessor.HttpContext.Response.Cookies.Delete(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnalysisData.CookieSevice.abstractions;

public interface ICookieService
{
void RemoveCookie(string name);
string GetCookie(string name);
void SetCookie(string name, string token,bool rememberMe);
}
2 changes: 1 addition & 1 deletion AnalysisData/AnalysisData/JwtService/JwtService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Authentication;

public class JwtService
public class JwtService : IJwtService
{
private readonly IConfiguration _configuration;
private readonly IUserRepository _userRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AnalysisData.JwtService;

public interface IJwtService
{
Task<string> GenerateJwtToken(string userName);
}
71 changes: 71 additions & 0 deletions AnalysisData/AnalysisData/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions AnalysisData/AnalysisData/Resources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">

</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="NotFoundUserException" xml:space="preserve">
<value>user not found ! </value>
</data>
</root>
8 changes: 8 additions & 0 deletions AnalysisData/AnalysisData/Services/IUserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using AnalysisData.UserManage.LoginModel;

namespace AnalysisData.Services;

public interface IUserService
{
Task<List<string>> Login(UserLoginModel userLoginModel);
}
34 changes: 19 additions & 15 deletions AnalysisData/AnalysisData/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
using AnalysisData.CookieSevice.abstractions;
using AnalysisData.Data;
using AnalysisData.JwtService;
using AnalysisData.Repository.RoleRepository.Abstraction;
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
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
private readonly ICookieService _cookieService;
private readonly IJwtService _jwtService;

public UserService(IUserRepository userRepository)
public UserService(IUserRepository userRepository, ICookieService cookieService,IJwtService jwtService)
{
_userRepository = userRepository;
_cookieService = cookieService;
_jwtService = jwtService;
}
public async Task<IActionResult> Login([FromBody] UserLoginModel userLoginModel)
public async Task<List<string>> Login(UserLoginModel userLoginModel)
{
var user = await _userRepository.GetUser(userLoginModel.userName);
if (user is null)
if (userLoginModel.userName.Length > 255)
{
return NotFound($"Unable to load user with username '{userLoginModel.userName}'");
return null;
}

if (user.Password != userLoginModel.password)
var user = await _userRepository.GetUser(userLoginModel.userName);
if (user == null || user.Password != userLoginModel.password)
{
return Unauthorized("Invalid username or password.");
return null;
}
var token = await _jwtService.GenerateJwtToken(userLoginModel.userName);

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

_cookieService.SetCookie("AuthToken", token, userLoginModel.rememberMe);

var roles = user.UserRoles.Select(ur => ur.Role.RoleName).ToList();

return roles;
}

}

0 comments on commit b7265f3

Please sign in to comment.