Ejemplo n.º 1
0
        public Task <TokenResponse> Sign(SignRequest signRequest)
        {
            var user = _userRepository.GetByEmail(signRequest.Email);

            if (user != null)
            {
                throw new UnprocessableException("Email ja cadastrado");
            }

            user = new User
            {
                Email     = signRequest.Email,
                Name      = signRequest.Name,
                Birthday  = signRequest.Birthday,
                Height    = signRequest.Height,
                Password  = PasswordService.GeneratePassword(signRequest.Password),
                CreatedAt = DateTime.UtcNow,
                Role      = "user"
            };

            _userRepository.Insert(user);
            return(Task.FromResult(new TokenResponse {
                Token = JwtService.GetToken(user)
            }));
        }
        public Task <TokenResponse> Login(LoginRequest loginRequest)
        {
            var user = _userRepository.GetByEmail(loginRequest.Email);

            if (user != null && loginRequest.Password != null && PasswordService.PasswordIsCorrect(user.Password.Split(".")[1], user.Password.Split(".")[2], loginRequest.Password))
            {
                return(Task.FromResult(new TokenResponse {
                    Token = JwtService.GetToken(user)
                }));
            }
            else
            {
                throw new UnauthorizedException("Credenciais incorretas");
            }
        }