Commit 37c7f934 authored by hasan khaddour's avatar hasan khaddour

use the result pattern for response.

parent d9535ed1
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AuthenticationResponse = PSManagement.Contracts.Authentication.AuthenticationResponse;
namespace PSManagement.Api.Controllers.Authentication namespace PSManagement.Api.Controllers.Authentication
{ {
...@@ -22,33 +24,42 @@ namespace PSManagement.Api.Controllers.Authentication ...@@ -22,33 +24,42 @@ namespace PSManagement.Api.Controllers.Authentication
[HttpPost("Login")] [HttpPost("Login")]
public async Task<IActionResult> Login([FromBody] LoginRequest loginRequest) public async Task<IActionResult> Login([FromBody] LoginRequest loginRequest)
{ {
AuthenticationResult result = await _authenticationService.Login(loginRequest.Email,loginRequest.PassWorrd); Result<AuthenticationResult> result = await _authenticationService.Login(loginRequest.Email, loginRequest.PassWorrd);
AuthenticationResponse response = new AuthenticationResponse( if (result.IsSuccess) {
result.Id, AuthenticationResponse response = new (
result.FirstName, result.Value.Id,
result.LastName, result.Value.FirstName,
result.Email, result.Value.LastName,
result.Token); result.Value.Email,
return Ok(response); result.Value.Token);
return Ok(response);
}
return Problem(title:"An Errorr Occured",detail:result.Error.Name,statusCode:400);
} }
[HttpPost("Register")] [HttpPost("Register")]
public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest) public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest)
{ {
AuthenticationResult result = await _authenticationService.Register( Result<AuthenticationResult> result = await _authenticationService.Register(
registerRequest.Email, registerRequest.Email,
registerRequest.FirstName, registerRequest.FirstName,
registerRequest.LastName, registerRequest.LastName,
registerRequest.Password); registerRequest.Password);
if (result.IsSuccess)
{
AuthenticationResponse response = new (
result.Value.Id,
result.Value.FirstName,
result.Value.LastName,
result.Value.Email,
result.Value.Token);
return Ok(response);
}
AuthenticationResponse response = new AuthenticationResponse( return Problem(title: "An Errorr Occured", detail: result.Error.Name, statusCode: 400);
result.Id,
result.FirstName,
result.LastName,
result.Email,
result.Token);
return Ok(response);
} }
} }
......
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
...@@ -9,6 +10,7 @@ namespace PSManagement.Api.Controllers ...@@ -9,6 +10,7 @@ namespace PSManagement.Api.Controllers
{ {
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize]
public class HomeController : ControllerBase public class HomeController : ControllerBase
{ {
[HttpGet] [HttpGet]
......
...@@ -34,9 +34,31 @@ namespace PSManagement.Api ...@@ -34,9 +34,31 @@ namespace PSManagement.Api
services.AddInfrastructure(Configuration); services.AddInfrastructure(Configuration);
services.AddControllers(); services.AddControllers();
services.AddSwaggerGen(c => services.AddSwaggerGen(options =>
{ {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "PSManagement.Api", Version = "v1" }); options.SwaggerDoc("v1", new OpenApiInfo { Title = "PSManagement.Api", Version = "v1" });
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
}); });
} }
...@@ -54,8 +76,8 @@ namespace PSManagement.Api ...@@ -54,8 +76,8 @@ namespace PSManagement.Api
app.UseRouting(); app.UseRouting();
app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapControllers(); endpoints.MapControllers();
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"JwtSettings": { "JwtSettings": {
"secret": "super-secret-key", "secret": "super-secret-key",
"ExpireMinutes": 60, "ExpireMinutes": 60,
"Issuer": "HIAST-PS-Management", "Issuer": "HIAST-PS-Management-Server",
"Audience": "" "Audience": "All"
} }
} }
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