Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Streetcode.Auth.Api.Extensions;
using Streetcode.Auth.Application.Dtos.Auth;
using Streetcode.Auth.Application.Dtos.Users;
using Streetcode.Auth.Application.MediatR.Login;
Expand All @@ -19,7 +20,14 @@ public async Task<IActionResult> Register([FromBody] RegisterUserDto user)
[HttpPost]
public async Task<IActionResult> Login([FromBody] LoginRequestDto request)
{
return HandleResult(await Mediator.Send(new LoginCommand(request)));
var result = await Mediator.Send(new LoginCommand(request));

if (result.IsSuccess && result.Value != null)
{
HttpContext.AppendTokensToCookies(result.Value.AccessToken, result.Value.RefreshToken);
}

return HandleResult(result);
}

[Authorize]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public static IServiceCollection AddJwtAuthentication(this IServiceCollection se

return services;
}

public static void AppendTokensToCookies(this HttpContext context, string accessToken, string refreshToken)
{
var cookieOptions = new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Strict,
Expires = DateTimeOffset.UtcNow.AddDays(7)
Comment thread
chups98 marked this conversation as resolved.
};

context.Response.Cookies.Append("accessToken", accessToken, cookieOptions);
context.Response.Cookies.Append("refreshToken", refreshToken, cookieOptions);
}

public static IServiceCollection AddSwaggerWithJwt(this IServiceCollection services)
{
Expand Down
Loading