Skip to content

Commit

Permalink
Merge pull request #40 from quizer-app/develop
Browse files Browse the repository at this point in the history
Add logout endpoint
  • Loading branch information
EloToJaa authored Jan 30, 2024
2 parents c67f6fe + e8f4178 commit c484159
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Quizer.Api/Common/Mapping/AuthenticationMappingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Mapster;
using Quizer.Application.Authentication.Commands.Login;
using Quizer.Application.Authentication.Commands.Logout;
using Quizer.Application.Authentication.Commands.Register;
using Quizer.Contracts.Authentication;

Expand All @@ -18,5 +19,8 @@ public void Register(TypeAdapterConfig config)

config.NewConfig<RegisterResult, RegisterResponse>()
.Map(dest => dest, src => src.User);

config.NewConfig<LogoutResult, LogoutResponse>()
.Map(dest => dest, src => src);
}
}
18 changes: 18 additions & 0 deletions src/Quizer.Api/Controllers/V1/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Quizer.Application.Authentication.Commands.Login;
using Quizer.Application.Authentication.Commands.Logout;
using Quizer.Application.Authentication.Commands.RefreshToken;
using Quizer.Application.Authentication.Commands.Register;
using Quizer.Contracts.Authentication;
Expand Down Expand Up @@ -62,6 +63,23 @@ public async Task<IActionResult> Login(LoginRequest request)
);
}

[HttpDelete("logout")]
public async Task<IActionResult> Logout()
{
string? refreshToken = Request.Cookies["refreshToken"];
if (refreshToken is null)
return Unauthorized();

var query = new LogoutCommand(refreshToken);

var result = await _mediator.Send(query);

return result.Match(
data => Ok(_mapper.Map<LogoutResponse>(data)),
Problem
);
}

[HttpPost("refresh")]
public async Task<IActionResult> RefreshToken()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using ErrorOr;
using MediatR;

namespace Quizer.Application.Authentication.Commands.Logout;

public record LogoutCommand(
string RefreshToken
) : IRequest<ErrorOr<LogoutResult>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using ErrorOr;
using MediatR;
using Microsoft.AspNetCore.Identity;
using Quizer.Application.Common.Interfaces.Authentication;
using Quizer.Application.Common.Interfaces.Persistance;
using Quizer.Domain.Common.Errors;
using Quizer.Domain.RefreshTokenAggregate;
using Quizer.Domain.UserAggregate;

namespace Quizer.Application.Authentication.Commands.Logout;

public class LogoutCommandHandler : IRequestHandler<LogoutCommand, ErrorOr<LogoutResult>>
{
private readonly IRefreshTokenRepository _refreshTokenRepository;
private readonly SignInManager<User> _signInManager;

public LogoutCommandHandler(SignInManager<User> signInManager, IRefreshTokenRepository refreshTokenRepository)
{
_signInManager = signInManager;
_refreshTokenRepository = refreshTokenRepository;
}

public async Task<ErrorOr<LogoutResult>> Handle(LogoutCommand query, CancellationToken cancellation)
{
var token = await _refreshTokenRepository.Get(TokenId.Create(query.RefreshToken));

if(token is null)
return Errors.Authentication.InvalidCredentials;

_refreshTokenRepository.Delete(token);
await _signInManager.SignOutAsync();

return new LogoutResult(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Quizer.Application.Authentication.Commands.Logout;

public record LogoutResult(
bool IsSuccess
);
5 changes: 5 additions & 0 deletions src/Quizer.Contracts/Authentication/LogoutResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Quizer.Contracts.Authentication;

public record LogoutResponse(
bool IsSuccess
);

0 comments on commit c484159

Please sign in to comment.