Changeset a26f6a1 for resTools_backend/backend
- Timestamp:
- 08/09/22 16:09:32 (2 years ago)
- Branches:
- master
- Children:
- a569b7c
- Parents:
- 899b19d
- Location:
- resTools_backend/backend
- Files:
-
- 5 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
resTools_backend/backend/Controllers/UsersController.cs
r899b19d ra26f6a1 41 41 }catch (Exception ex){ return null; } 42 42 User user = await _userService.GetById(userId); 43 return new AuthenticateResponse() { Email=user.Email, Id = user.Id}; 43 return new AuthenticateResponse() { Email=user.Email, Id = user.Id, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed}; 44 } 45 46 [HttpPost("confirm")] 47 public async Task ConfirmEmail() 48 { 49 int userId = 0; 50 try 51 { 52 userId = (int)this.HttpContext.Items["User"]; 53 } 54 catch (Exception ex) { return; } 55 User user = await _userService.GetById(userId); 56 await _userService.SendEmailConfirmation(user.Email); 57 } 58 59 [HttpPost("reset")] 60 public async Task ResetPassword(string email) 61 { 62 await _userService.SendPasswordReset(email); 63 } 64 65 [HttpPost("confirmed")] 66 public async Task ConfirmedEmail(string validityString) 67 { 68 int userId = 0; 69 try 70 { 71 userId = (int)this.HttpContext.Items["User"]; 72 } 73 catch (Exception ex) { return; } 74 User user = await _userService.GetById(userId); 75 await _userService.ConfirmEmail(user, validityString); 76 } 77 78 [HttpPost("reseted")] 79 public async Task ResetedPassword(string validityString, string newPassword) 80 { 81 await _userService.ResetPassword(validityString, newPassword); 44 82 } 45 83 -
resTools_backend/backend/DTOs/AuthenticateResponse.cs
r899b19d ra26f6a1 15 15 [JsonProperty] 16 16 public bool IsAdmin { get; set; } 17 [JsonProperty] 18 public bool IsConfirmed { get; set; } 17 19 } -
resTools_backend/backend/Entities/User.cs
r899b19d ra26f6a1 9 9 public string Password { get; set; } 10 10 public bool IsAdmin { get; set; } 11 public bool IsConfirmed { get; set; } 12 public string? ConfirmationURL { get; set; } 13 public DateTime? ConfirmationValidTo { get; set; } 14 public string? PasswordResetURL { get; set; } 15 public DateTime? PasswordResetValidTo { get; set; } 11 16 public virtual Restaurant Restaurant { get; set; } 12 17 } -
resTools_backend/backend/Migrations/DataContextModelSnapshot.cs
r899b19d ra26f6a1 176 176 NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id")); 177 177 178 b.Property<string>("ConfirmationURL") 179 .HasColumnType("text"); 180 181 b.Property<DateTime?>("ConfirmationValidTo") 182 .HasColumnType("timestamp with time zone"); 183 178 184 b.Property<string>("Email") 179 185 .IsRequired() … … 183 189 .HasColumnType("boolean"); 184 190 191 b.Property<bool>("IsConfirmed") 192 .HasColumnType("boolean"); 193 185 194 b.Property<string>("Password") 186 195 .IsRequired() 187 196 .HasColumnType("text"); 197 198 b.Property<string>("PasswordResetURL") 199 .HasColumnType("text"); 200 201 b.Property<DateTime?>("PasswordResetValidTo") 202 .HasColumnType("timestamp with time zone"); 188 203 189 204 b.HasKey("Id"); -
resTools_backend/backend/Program.cs
r899b19d ra26f6a1 1 1 using backend.Data; 2 using backend.Email; 2 3 using backend.Helpers; 3 4 using backend.Services; … … 50 51 builder.Services.AddScoped<ISmsService, SmsService>(); 51 52 53 builder.Services.AddTransient<IEmailSender, EmailSender>(); 54 52 55 builder.Services.AddDbContext<DataContext>(p => p.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); 53 56 -
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 } -
resTools_backend/backend/backend.csproj
r899b19d ra26f6a1 1 <Project Sdk="Microsoft.NET.Sdk.Web">1 <Project Sdk="Microsoft.NET.Sdk.Web"> 2 2 3 3 <PropertyGroup> … … 22 22 <PackageReference Include="Npgsql" Version="6.0.3" /> 23 23 <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" /> 24 <PackageReference Include="SendGrid" Version="9.28.0" /> 24 25 <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> 25 26 <PackageReference Include="Swashbuckle.Core" Version="5.6.0" />
Note:
See TracChangeset
for help on using the changeset viewer.