fix(auth): handles refresh token flow correctly
This commit is contained in:
@@ -39,11 +39,7 @@ public static class DependencyInjection
|
||||
.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddCookie("Identity.Application", options =>
|
||||
{
|
||||
options.LoginPath = "/api/Users/login";
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
});
|
||||
|
||||
var authJwt = configuration.GetSection("Authentication:Jwt");
|
||||
|
||||
@@ -30,7 +30,6 @@ public static class DependencyInjection
|
||||
.AddRoles<IdentityRole>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddApiEndpoints()
|
||||
.AddSignInManager<SignInManager<IdentityUser>>()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
// Singleton services
|
||||
|
||||
@@ -42,7 +42,6 @@ public record LoginWithFacebookResponse(
|
||||
public class LoginWithFacebookHandler(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IdentityUserManager userManager,
|
||||
SignInManager<IdentityUser> signInManager,
|
||||
IOptionsSnapshot<JwtOptions> jwtOptions)
|
||||
: Endpoint<LoginWithFacebookRequest, LoginWithFacebookResponse>
|
||||
{
|
||||
@@ -116,8 +115,6 @@ public class LoginWithFacebookHandler(
|
||||
user = generatedUser;
|
||||
}
|
||||
|
||||
await signInManager.SignInAsync(user, isPersistent: false);
|
||||
|
||||
// Generate refresh token
|
||||
var refreshToken = RefreshTokenGenerator.Next();
|
||||
|
||||
|
||||
@@ -42,7 +42,6 @@ public record LoginWithGoogleResponse(
|
||||
public class LoginWithGoogleHandler(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IdentityUserManager userManager,
|
||||
SignInManager<IdentityUser> signInManager,
|
||||
IOptionsSnapshot<JwtOptions> jwtOptions)
|
||||
: Endpoint<LoginWithGoogleRequest, LoginWithGoogleResponse>
|
||||
{
|
||||
@@ -123,16 +122,10 @@ public class LoginWithGoogleHandler(
|
||||
user = generatedUser;
|
||||
}
|
||||
|
||||
await signInManager.SignInAsync(user, isPersistent: false);
|
||||
|
||||
// Generate refresh token for existing users
|
||||
if (user.RefreshToken == null)
|
||||
{
|
||||
var refreshToken = RefreshTokenGenerator.Next();
|
||||
user.RefreshToken = refreshToken;
|
||||
// Generate new refresh token
|
||||
user.RefreshToken = RefreshTokenGenerator.Next();
|
||||
user.RefreshTokenExpiryTime = DateTime.UtcNow.Add(jwtOptions.Value.RefreshTokenLifetime);
|
||||
await userManager.UpdateAsync(user);
|
||||
}
|
||||
|
||||
var accessToken = JwtTokenHelper.GenerateJwtToken(
|
||||
expiresIn: jwtOptions.Value.Lifetime,
|
||||
|
||||
Reference in New Issue
Block a user