Changeset 057037b for backend/Services


Ignore:
Timestamp:
04/11/22 15:29:19 (3 years ago)
Author:
Danilo <danilo.najkov@…>
Branches:
master
Children:
7a983b0
Parents:
b66b3ac
Message:

backend full

Location:
backend/Services
Files:
3 added
1 edited

Legend:

Unmodified
Added
Removed
  • backend/Services/UserService.cs

    rb66b3ac r057037b  
    11namespace backend.Services;
    22
     3using backend.Data;
     4using backend.DTOs;
    35using backend.Entities;
    46using backend.Helpers;
    57using backend.Models;
     8using Microsoft.EntityFrameworkCore;
    69using Microsoft.Extensions.Options;
    710using Microsoft.IdentityModel.Tokens;
    811using System.IdentityModel.Tokens.Jwt;
    912using System.Security.Claims;
    10 using System.Text;
    1113
    1214public interface IUserService
    1315{
    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);
    1619}
    1720
    1821public class UserService : IUserService
    1922{
    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;
    2525
    26     private readonly AppSettings _appSettings;
    27 
    28     public UserService(IOptions<AppSettings> appSettings)
     26    public UserService(IOptions<AppSettings> appSettings, DataContext context)
    2927    {
    3028        _appSettings = appSettings.Value;
     29        _context = context;
    3130    }
    3231
    33     public AuthenticateResponse Authenticate(AuthenticateRequest model)
     32    public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model)
    3433    {
    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);
    3635
    3736        // return null if user not found
     
    4140        var token = generateJwtToken(user);
    4241
    43         return new AuthenticateResponse(user, token);
     42        return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token};
    4443    }
    4544
    46     public User GetById(int id)
     45    public async Task<User> GetById(int id)
    4746    {
    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 };
    4957    }
    5058
     
    5361        // generate token that is valid for 7 days
    5462        var tokenHandler = new JwtSecurityTokenHandler();
    55         var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
     63        var key = System.Text.Encoding.ASCII.GetBytes(_appSettings.Secret);
    5664        var tokenDescriptor = new SecurityTokenDescriptor
    5765        {
Note: See TracChangeset for help on using the changeset viewer.