Changeset a26f6a1 for resTools_backend/backend/Services
- Timestamp:
- 08/09/22 16:09:32 (2 years ago)
- Branches:
- master
- Children:
- a569b7c
- Parents:
- 899b19d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
resTools_backend/backend/Services/UserService.cs
r899b19d ra26f6a1 1 namespace backend.Services;1 namespace backend.Services; 2 2 3 3 using backend.Data; 4 4 using backend.DTOs; 5 using backend.Email; 5 6 using backend.Entities; 6 7 using backend.Helpers; … … 11 12 using System.IdentityModel.Tokens.Jwt; 12 13 using System.Security.Claims; 14 using System.Security.Cryptography; 15 using System.Text; 13 16 14 17 public interface IUserService … … 17 20 Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst); 18 21 Task<User> GetById(int id); 22 Task SendEmailConfirmation(string email); 23 Task SendPasswordReset(string email); 24 Task ConfirmEmail(User user, string checkValid); 25 Task ResetPassword(string checkValid, string password); 19 26 } 20 27 … … 23 30 private readonly AppSettings _appSettings; 24 31 private readonly DataContext _context = null; 32 private readonly IEmailSender _emailSender; 25 33 26 public UserService(IOptions<AppSettings> appSettings, DataContext context )34 public UserService(IOptions<AppSettings> appSettings, DataContext context, IEmailSender emailSender) 27 35 { 28 36 _appSettings = appSettings.Value; 29 37 _context = context; 38 _emailSender = emailSender; 30 39 } 31 40 … … 40 49 var token = generateJwtToken(user); 41 50 42 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin}; 51 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed}; 52 } 53 54 public async Task ConfirmEmail(User user, string checkValid) 55 { 56 if(user.ConfirmationURL != checkValid) 57 { 58 throw new Exception("Invalid check"); 59 } 60 if(user.ConfirmationValidTo < DateTime.UtcNow) 61 { 62 throw new Exception("Link expired"); 63 } 64 65 user.IsConfirmed = true; 66 _context.Users.Update(user); 67 await _context.SaveChangesAsync(); 43 68 } 44 69 … … 50 75 public async Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst) 51 76 { 52 User user = new User() { Email = req.Email, Password = req.Password, IsAdmin = isFirst };77 User user = new User() { Email = req.Email, Password = req.Password, IsAdmin = isFirst, IsConfirmed = false }; 53 78 await _context.Users.AddAsync(user); 54 79 await _context.SaveChangesAsync(); 55 80 var token = generateJwtToken(user); 56 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin }; 81 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = false }; 82 } 83 84 public async Task ResetPassword(string checkValid, string password) 85 { 86 var user = await _context.Users.Where(x => x.PasswordResetURL == checkValid).FirstOrDefaultAsync(); 87 if (user == null) 88 { 89 throw new Exception("Invalid check"); 90 } 91 if (user.PasswordResetValidTo < DateTime.UtcNow) 92 { 93 throw new Exception("Link expired"); 94 } 95 96 user.Password = password; 97 _context.Users.Update(user); 98 await _context.SaveChangesAsync(); 99 } 100 101 public async Task SendEmailConfirmation(string email) 102 { 103 User user = await _context.Users.FirstOrDefaultAsync(x => x.Email == email); 104 user.ConfirmationURL = Guid.NewGuid().ToString(); 105 user.ConfirmationValidTo = DateTime.UtcNow.AddHours(24); 106 _context.Users.Update(user); 107 await _context.SaveChangesAsync(); 108 await _emailSender.SendEmailAsync( 109 "Потврдете го вашиот емаил", 110 "Ве молиме кликнете на следниот линк за да го потврдите вашиот емаил: http://localhost:3000/confirm?id=" + user.ConfirmationURL, 111 email); 112 } 113 114 public async Task SendPasswordReset(string email) 115 { 116 User user = await _context.Users.FirstOrDefaultAsync(x => x.Email == email); 117 user.PasswordResetURL = Guid.NewGuid().ToString(); 118 user.PasswordResetValidTo = DateTime.UtcNow.AddHours(24); 119 _context.Users.Update(user); 120 await _context.SaveChangesAsync(); 121 await _emailSender.SendEmailAsync( 122 "Ресетирајте ја лозинката", 123 "Ве молиме кликнете на следниот линк за да ја ресетирате лозинката: http://localhost:3000/reset?id=" + user.PasswordResetURL, 124 email); 57 125 } 58 126 … … 71 139 return tokenHandler.WriteToken(token); 72 140 } 141 142 private string sha256Hash(String value) 143 { 144 using (SHA256 hash = SHA256.Create()) 145 { 146 return String.Concat(hash 147 .ComputeHash(Encoding.UTF8.GetBytes(value)) 148 .Select(item => item.ToString("x2"))); 149 } 150 } 73 151 }
Note:
See TracChangeset
for help on using the changeset viewer.