Changeset 057037b for backend/Services
- Timestamp:
- 04/11/22 15:29:19 (3 years ago)
- Branches:
- master
- Children:
- 7a983b0
- Parents:
- b66b3ac
- Location:
- backend/Services
- Files:
-
- 3 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/Services/UserService.cs
rb66b3ac r057037b 1 1 namespace backend.Services; 2 2 3 using backend.Data; 4 using backend.DTOs; 3 5 using backend.Entities; 4 6 using backend.Helpers; 5 7 using backend.Models; 8 using Microsoft.EntityFrameworkCore; 6 9 using Microsoft.Extensions.Options; 7 10 using Microsoft.IdentityModel.Tokens; 8 11 using System.IdentityModel.Tokens.Jwt; 9 12 using System.Security.Claims; 10 using System.Text;11 13 12 14 public interface IUserService 13 15 { 14 AuthenticateResponse Authenticate(AuthenticateRequest model); 15 User GetById(int id); 16 Task<AuthenticateResponse> Authenticate(AuthenticateRequest model); 17 Task<AuthenticateResponse> Register(CreateUserRequest req); 18 Task<User> GetById(int id); 16 19 } 17 20 18 21 public class UserService : IUserService 19 22 { 20 // users hardcoded for simplicity, store in a db with hashed passwords in production applications 21 private List<User> _users = new List<User> 22 { 23 new User { Id = 1, Username = "test", Password = "test" } 24 }; 23 private readonly AppSettings _appSettings; 24 private readonly DataContext _context = null; 25 25 26 private readonly AppSettings _appSettings; 27 28 public UserService(IOptions<AppSettings> appSettings) 26 public UserService(IOptions<AppSettings> appSettings, DataContext context) 29 27 { 30 28 _appSettings = appSettings.Value; 29 _context = context; 31 30 } 32 31 33 public AuthenticateResponseAuthenticate(AuthenticateRequest model)32 public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model) 34 33 { 35 var user = _users.SingleOrDefault(x => x.Username == model.Username&& x.Password == model.Password);34 User user = await _context.Users.FirstOrDefaultAsync(x => x.Email == model.Email && x.Password == model.Password); 36 35 37 36 // return null if user not found … … 41 40 var token = generateJwtToken(user); 42 41 43 return new AuthenticateResponse (user, token);42 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token}; 44 43 } 45 44 46 public UserGetById(int id)45 public async Task<User> GetById(int id) 47 46 { 48 return _users.FirstOrDefault(x => x.Id == id); 47 return await _context.Users.FindAsync(id); 48 } 49 50 public async Task<AuthenticateResponse> Register(CreateUserRequest req) 51 { 52 User user = new User() { Email = req.Email, Password = req.Password }; 53 await _context.Users.AddAsync(user); 54 await _context.SaveChangesAsync(); 55 var token = generateJwtToken(user); 56 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token }; 49 57 } 50 58 … … 53 61 // generate token that is valid for 7 days 54 62 var tokenHandler = new JwtSecurityTokenHandler(); 55 var key = Encoding.ASCII.GetBytes(_appSettings.Secret);63 var key = System.Text.Encoding.ASCII.GetBytes(_appSettings.Secret); 56 64 var tokenDescriptor = new SecurityTokenDescriptor 57 65 {
Note:
See TracChangeset
for help on using the changeset viewer.