89 lines
3.9 KiB
C#
89 lines
3.9 KiB
C#
using ExpenseTracker.Application.Authentication;
|
|
using ExpenseTracker.Application.Authentication.Commands.RegisterWithEmail;
|
|
using ExpenseTracker.Application.Authentication.Commands.RegisterWithEmailAndPassword;
|
|
using ExpenseTracker.Application.Authentication.Queries.Login;
|
|
using ExpenseTracker.Application.Authentication.Commands.RenewAccessTokenWithBody;
|
|
using ExpenseTracker.Application.Authentication.Commands.RenewAccessTokenWithCookie;
|
|
using ExpenseTracker.Application.Authentication.Commands.RevokeRefreshTokenWithCookie;
|
|
using ExpenseTracker.Application.Authentication.Commands.RevokeRefreshTokenWithBody;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace ExpenseTracker.Api.Controllers;
|
|
|
|
[Route("authentication")]
|
|
public class AuthenticationController : BaseController
|
|
{
|
|
[HttpPost("registerWithEmail")]
|
|
public async Task RegisterWithEmail([FromBody] RegisterWithEmailCommand command, CancellationToken cancellationToken)
|
|
{
|
|
await Mediator.Send(command, cancellationToken);
|
|
}
|
|
|
|
[HttpPost("registerWithEmailAndPassword")]
|
|
public async Task RegisterWithEmailAndPassword([FromBody] RegisterWithEmailAndPasswordCommand command, CancellationToken cancellationToken)
|
|
{
|
|
await Mediator.Send(command, cancellationToken);
|
|
}
|
|
|
|
[HttpPost("loginWithBody")]
|
|
public async Task<TokensModel> LoginWithBody([FromBody] LoginQuery query, CancellationToken cancellationToken)
|
|
{
|
|
return await Mediator.Send(query, cancellationToken);
|
|
}
|
|
|
|
[HttpPost("loginWithCookie")]
|
|
public async Task<TokensModel> LoginWithCookie([FromBody] LoginQuery query, CancellationToken cancellationToken)
|
|
{
|
|
var tokens = await Mediator.Send(query, cancellationToken);
|
|
|
|
HttpContext.Response.Cookies.Delete("accessToken");
|
|
HttpContext.Response.Cookies.Delete("refreshToken");
|
|
|
|
var cookieOptions = new CookieOptions() { Path = "/", Expires = DateTimeOffset.MaxValue, HttpOnly = true };
|
|
HttpContext.Response.Cookies.Append("accessToken", tokens.AccessToken, cookieOptions);
|
|
HttpContext.Response.Cookies.Append("refreshToken", tokens.RefreshToken, cookieOptions);
|
|
|
|
return tokens;
|
|
}
|
|
|
|
[HttpPost("renewAccessTokenWithBody")]
|
|
public async Task<TokensModel> RenewAccessTokenWithBody([FromBody] RenewAccessTokenWithBodyCommand command, CancellationToken cancellationToken)
|
|
{
|
|
var tokens = await Mediator.Send(command, cancellationToken);
|
|
|
|
HttpContext.Response.Cookies.Delete("accessToken");
|
|
|
|
var cookieOptions = new CookieOptions() { Path = "/", Expires = DateTimeOffset.MaxValue, HttpOnly = true };
|
|
HttpContext.Response.Cookies.Append("accessToken", tokens.AccessToken, cookieOptions);
|
|
|
|
return tokens;
|
|
}
|
|
|
|
[HttpPost("renewAccessTokenWithCookie")]
|
|
public async Task<TokensModel> RenewAccessTokenWithCookie([FromBody] RenewAccessTokenWithCookieCommand command, CancellationToken cancellationToken)
|
|
{
|
|
var tokens = await Mediator.Send(command, cancellationToken);
|
|
|
|
HttpContext.Response.Cookies.Delete("accessToken");
|
|
|
|
var cookieOptions = new CookieOptions() { Path = "/", Expires = DateTimeOffset.MaxValue, HttpOnly = true };
|
|
HttpContext.Response.Cookies.Append("accessToken", tokens.AccessToken, cookieOptions);
|
|
|
|
return tokens;
|
|
}
|
|
|
|
[HttpPost("revokeRefreshTokenWithBody")]
|
|
public async Task RevokeRefreshTokenWithBody([FromBody] RevokeRefreshTokenWithBodyCommand command, CancellationToken cancellationToken)
|
|
{
|
|
await Mediator.Send(command, cancellationToken);
|
|
}
|
|
|
|
[HttpPost("revokeRefreshTokenWithCookie")]
|
|
public async Task RevokeRefreshTokenWithCookie([FromBody] RevokeRefreshTokenWithCookieCommand command, CancellationToken cancellationToken)
|
|
{
|
|
await Mediator.Send(command, cancellationToken);
|
|
HttpContext.Response.Cookies.Delete("accessToken");
|
|
HttpContext.Response.Cookies.Delete("refreshToken");
|
|
}
|
|
}
|