Skip to content

Commit

Permalink
Fix: Save user will reset password and display new password
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Apr 22, 2024
1 parent 39f1fa3 commit c7c7923
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/Frontend/src/views/UsersView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ const save = async (data) => {
if (!confirm(t("please_confirm"))) {
return;
}
await service.post('api/saveUser', data).then(async () => {
await service.post('api/saveUser', data).then(async (res) => {
alert(t("updated_successfully"))
if (data.Id == null || data.Id == undefined) {
await resetPassword(data);
alert(t("new_password", [res.newPassword]))
navigator.clipboard.writeText(res.newPassword)
}
await fetchData();
})
Expand Down
24 changes: 17 additions & 7 deletions src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using MiniAuth.IdentityAuth.Helpers;
using MiniAuth.IdentityAuth.Models;
using System.Collections.Concurrent;
using System.Reflection.PortableExecutable;
using System.Security.Claims;
using System.Text;
using System.Text.Json;

Expand Down Expand Up @@ -92,7 +90,7 @@ MiniAuthIdentityDbContext _dbContext
, UserManager<MiniAuthIdentityUser> userManager
) =>
{
await SaveUser(context, _dbContext);
await SaveUser(context, _dbContext, userManager);
}).RequireAuthorization("miniauth-admin");

// /api/resetPassword
Expand Down Expand Up @@ -149,7 +147,7 @@ private async Task ResetPassword(HttpContext context, MiniAuthIdentityDbContext
}
}

private async Task SaveUser(HttpContext context, MiniAuthIdentityDbContext _dbContext)
private async Task SaveUser(HttpContext context, MiniAuthIdentityDbContext _dbContext, UserManager<MiniAuthIdentityUser> userManager)
{
JsonDocument bodyJson = await GetBodyJson(context);
var root = bodyJson.RootElement;
Expand All @@ -163,7 +161,8 @@ private async Task SaveUser(HttpContext context, MiniAuthIdentityDbContext _dbCo
var roles = root.GetProperty<string[]>("Roles");
var type = root.GetProperty<string>("Type");
var user = await _dbContext.Users.FindAsync(id);
if (user == null)
var isUserExist = user == null;
if (isUserExist)
{
user = new MiniAuthIdentityUser
{
Expand Down Expand Up @@ -202,8 +201,19 @@ private async Task SaveUser(HttpContext context, MiniAuthIdentityDbContext _dbCo
};
await _dbContext.UserRoles.AddAsync(userRole);
}
await _dbContext.SaveChangesAsync();
await OkResult(context, "".ToJson(code: 200, message: ""));

if (isUserExist)
{
string newPassword = GetNewPassword();
await userManager.AddPasswordAsync(user, newPassword);
await _dbContext.SaveChangesAsync();
await OkResult(context, new { newPassword }.ToJson(code: 200, message: ""));
}
else
{
await _dbContext.SaveChangesAsync();
await OkResult(context, "".ToJson(code: 200, message: ""));
}
}


Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c7c7923

Please sign in to comment.