Exemplo n.º 1
0
        void Login(Views.LoginRequest request)
        {
            var player = RtaDb.ReadDocument <Views.PlayerInfo>("clients", request.token);

            if (player != null && player.password == request.password)
            {
                IsAuthnticated = true;

                SendAsync("login", player);
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Login([FromBody] Views.LoginRequest input)
        {
            try
            {
                var res = await _authService.Login(input);

                return(Ok(res));
            }
            catch
            {
                return(BadRequest());
            }
        }
Exemplo n.º 3
0
        public async Task <Views.LoginResponse> Login(Views.LoginRequest input)
        {
            // Check if data has been provided
            if (string.IsNullOrEmpty(input.Email) || string.IsNullOrEmpty(input.Password))
            {
                throw new Exception();
            }

            // Find user in context
            var user = _context.Users.SingleOrDefault(x => x.Email == input.Email);

            // Check if user exists
            if (user == null)
            {
                throw new Exception();
            }

            // Check if password is correct
            if (user.Password != input.Password)
            {
                throw new Exception();
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var validUntil      = DateTime.UtcNow.AddDays(3);
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString())
                }),
                Expires            = validUntil,
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            user.Token      = tokenHandler.WriteToken(token);
            user.TokenValid = validUntil;

            await _context.SaveChangesAsync();

            return(new Views.LoginResponse
            {
                Email = user.Email,
                Token = user.Token,
                ValidUntil = validUntil
            });
        }