-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from quizer-app/develop
Add logout endpoint
- Loading branch information
Showing
6 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Quizer.Application/Authentication/Commands/Logout/LogoutCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; |
35 changes: 35 additions & 0 deletions
35
src/Quizer.Application/Authentication/Commands/Logout/LogoutCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Quizer.Application/Authentication/Commands/Logout/LogoutResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Quizer.Contracts.Authentication; | ||
|
||
public record LogoutResponse( | ||
bool IsSuccess | ||
); |