Commit 5d39c9ea authored by hasan khaddour's avatar hasan khaddour

update auth services.

parent 85f8d4fd
using Microsoft.AspNetCore.Http; using FluentResults;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using PSManagement.Application.Contracts.Authentication; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Contracts.Authentication; using PSManagement.Contracts.Authentication;
using PSManagement.SharedKernel.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AuthenticationResponse = PSManagement.Contracts.Authentication.AuthenticationResponse; using AuthenticationResponse = PSManagement.Contracts.Authentication.AuthenticationResponse;
...@@ -36,7 +32,7 @@ namespace PSManagement.Api.Controllers.Authentication ...@@ -36,7 +32,7 @@ namespace PSManagement.Api.Controllers.Authentication
return Ok(response); return Ok(response);
} }
return Problem(title:"An Errorr Occured",detail:result.Error.Name,statusCode:400); return Problem(title: result.Reasons[0].Message,detail:result.Errors[0].Message,statusCode:400);
} }
[HttpPost("Register")] [HttpPost("Register")]
public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest) public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest)
...@@ -59,7 +55,7 @@ namespace PSManagement.Api.Controllers.Authentication ...@@ -59,7 +55,7 @@ namespace PSManagement.Api.Controllers.Authentication
return Ok(response); return Ok(response);
} }
return Problem(title: "An Errorr Occured", detail: result.Error.Name, statusCode: 400); return Problem(title: "An Errorr Occured "+result.Errors[0].Message , detail: result.Reasons[0].Message, statusCode: 400);
} }
} }
......
using PSManagement.SharedKernel.Utilities; //using PSManagement.SharedKernel.Utilities;
using FluentResults;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
......
using PSManagement.Application.Contracts.Authentication; using FluentResults;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Authorization; using PSManagement.Application.Contracts.Authorization;
using PSManagement.SharedKernel.Utilities; //using PSManagement.SharedKernel.Utilities;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -21,24 +22,23 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -21,24 +22,23 @@ namespace PSManagement.Infrastructure.Services.Authentication
User u = await _userRepository.GetByEmail(email); User u = await _userRepository.GetByEmail(email);
if (u is null || u.Password != password) { if (u is null || u.Password != password) {
return Result.Failure<AuthenticationResult>(new Error("404", "the password or email maybe wrong!.")); return Result.Fail<AuthenticationResult>(new Error( "the password or email maybe wrong!."));
} }
String token = _jwtTokenGenerator.GenerateToken(u.Id,u.FirstName,u.LastName,u.Email); String token = _jwtTokenGenerator.GenerateToken(u.Id,u.FirstName,u.LastName,u.Email);
return Result.Success<AuthenticationResult>( return new AuthenticationResult {
new AuthenticationResult { Id = u.Id,
Id = u.Id, Email=u.Email,
Email=u.Email, FirstName=u.FirstName,
FirstName=u.FirstName, LastName =u.LastName,
LastName =u.LastName, Token=token};
Token=token});
} }
public async Task<Result<AuthenticationResult>> Register(String email, String firstName, String lastName, String password) { public async Task<Result<AuthenticationResult>> Register(String email, String firstName, String lastName, String password) {
// check if the user exist // check if the user exist
var u = await _userRepository.GetByEmail(email); var u = await _userRepository.GetByEmail(email);
if (u is not null) { if (u is not null) {
return Result.Failure<AuthenticationResult>(new Error("404","the user already exist ")); return Result.Fail<AuthenticationResult>(new Error("the user already exist "));
} }
await _userRepository.AddAsync( await _userRepository.AddAsync(
new User{ new User{
...@@ -49,7 +49,7 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -49,7 +49,7 @@ namespace PSManagement.Infrastructure.Services.Authentication
}); });
// generate token // generate token
String token = _jwtTokenGenerator.GenerateToken(2322323,firstName,lastName,email); String token = _jwtTokenGenerator.GenerateToken(2322323,firstName,lastName,email);
return Result.Success<AuthenticationResult>( return Result.Ok<AuthenticationResult>(
new AuthenticationResult new AuthenticationResult
{ {
Id = 233233, Id = 233233,
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentResults" Version="3.16.0" />
<PackageReference Include="MediatR" Version="5.1.0" /> <PackageReference Include="MediatR" Version="5.1.0" />
</ItemGroup> </ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment