feat(AuthService): assign admin role to the first registered user and clean up admin user creation logic

This commit is contained in:
shiyu
2025-05-23 20:22:13 +08:00
parent c9576d510e
commit 7932240bbb
2 changed files with 7 additions and 29 deletions

View File

@@ -27,7 +27,6 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
{
return (false, "该用户名已被使用", null);
}
var user = new User
{
UserName = request.UserName,
@@ -36,6 +35,11 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
var userCount = await context.Users.CountAsync();
if (userCount == 0)
{
user.RoleId = 1;
}
context.Users.Add(user);
await context.SaveChangesAsync();
return (true, "用户注册成功", user);
@@ -277,7 +281,7 @@ public class AuthService(IDbContextFactory<MyDbContext> dbContextFactory, IConfi
}
var (isSuccess, message, user) = await FindOrCreateGitHubUserAsync(githubUserId, name ?? loginName, email);
if (!isSuccess || user == null)
{
Console.WriteLine($"创建或查找GitHub用户失败: {message}");