mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2024-09-20 01:54:20 +02:00
56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using SharpRSS.API.Cryptography;
|
|
using SharpRSS.API.Models;
|
|
using SharpRSS.API.Models.Auth;
|
|
using ToolQit;
|
|
using ToolQit.Logging;
|
|
|
|
namespace SharpRSS.API.Data
|
|
{
|
|
public class AuthService
|
|
{
|
|
public AuthService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
_log = LogManager.CreateLogger(typeof(AuthService));
|
|
_log.Information("Setting up service...");
|
|
}
|
|
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILog _log;
|
|
|
|
public async Task<Result<User>> CreateUser(UserRequest userRequest)
|
|
{
|
|
bool result = false;
|
|
await using DbAccess access = new DbAccess(_configuration);
|
|
|
|
var user = access.Users.FirstOrDefault(u => u.UserName == userRequest.UserName);
|
|
if (user != null)
|
|
return new Result<User>(user, message:"User name already exists!");
|
|
byte[] hashedPwdBytes = Hasher.HashPassword(userRequest.Password, out byte[] salt);
|
|
user = new User()
|
|
{
|
|
UserName = userRequest.UserName,
|
|
Mail = userRequest.EMail,
|
|
Password = hashedPwdBytes,
|
|
Salt = salt
|
|
};
|
|
access.Users.Add(user);
|
|
try
|
|
{
|
|
int entries = await access.SaveChangesAsync();
|
|
result = entries > 0;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_log.Error(e, "Error creating user: {UserName}", user.UserName);
|
|
return new Result<User>(user, message: "Could not create user!");
|
|
}
|
|
return new Result<User>(user, result);
|
|
}
|
|
}
|
|
} |