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

use the result pattern for response.

parent d9535ed1
......@@ -2,10 +2,12 @@
using Microsoft.AspNetCore.Mvc;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Contracts.Authentication;
using PSManagement.SharedKernel.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AuthenticationResponse = PSManagement.Contracts.Authentication.AuthenticationResponse;
namespace PSManagement.Api.Controllers.Authentication
{
......@@ -22,33 +24,42 @@ namespace PSManagement.Api.Controllers.Authentication
[HttpPost("Login")]
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(
result.Id,
result.FirstName,
result.LastName,
result.Email,
result.Token);
return Ok(response);
if (result.IsSuccess) {
AuthenticationResponse response = new (
result.Value.Id,
result.Value.FirstName,
result.Value.LastName,
result.Value.Email,
result.Value.Token);
return Ok(response);
}
return Problem(title:"An Errorr Occured",detail:result.Error.Name,statusCode:400);
}
[HttpPost("Register")]
public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest)
{
AuthenticationResult result = await _authenticationService.Register(
Result<AuthenticationResult> result = await _authenticationService.Register(
registerRequest.Email,
registerRequest.FirstName,
registerRequest.LastName,
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(
result.Id,
result.FirstName,
result.LastName,
result.Email,
result.Token);
return Ok(response);
return Problem(title: "An Errorr Occured", detail: result.Error.Name, statusCode: 400);
}
}
......
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
......@@ -9,6 +10,7 @@ namespace PSManagement.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class HomeController : ControllerBase
{
[HttpGet]
......
......@@ -34,9 +34,31 @@ namespace PSManagement.Api
services.AddInfrastructure(Configuration);
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
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
......
......@@ -10,7 +10,7 @@
"JwtSettings": {
"secret": "super-secret-key",
"ExpireMinutes": 60,
"Issuer": "HIAST-PS-Management",
"Audience": ""
"Issuer": "HIAST-PS-Management-Server",
"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