ofbapi/OFBButte.Application/Users/ResetPassword.cs

35 lines
1.1 KiB
C#

using OFBButte.Application.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OFBButte.Application.Users
{
public class ResetPassword
{
private readonly IOFBContext context;
private readonly IPasswordHasher hasher;
public ResetPassword(IOFBContext context, IPasswordHasher hasher)
{
this.context = context;
this.hasher = hasher;
}
public bool Handle(int codeId, string code, string newPassword)
{
// Get user
var dte = DateTime.Now.AddHours(-1);
var user = context.Users.FirstOrDefault(u => u.PassswordResetCode.Id == codeId && u.PassswordResetCode.Code == code && u.PassswordResetCode.CreatedDate >= dte);
if (user == null)
return false;
user.Password = hasher.HashPassword(newPassword);
context.PasswordResetCodes.Remove(user.PassswordResetCode);
user.PassswordResetCode = null;
context.SaveChanges();
return true;
}
}
}