Exemple #1
0
        public ICommandResult Login([FromBody] LoginCommand command)
        {
            ICommandResult loginResult = _handler.Handle(command);

            if (loginResult.Status == false)
            {
                return(loginResult);
            }

            UserAuthQuery user  = (UserAuthQuery)loginResult.Data;
            string        token = TokenService.GenerateToken(user);
            object        data  = new { token = token, user = user };

            return(new CommandResult(loginResult.Status, loginResult.Message, data: data));
        }
        public static string GenerateToken(UserAuthQuery user)
        {
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(AppSettings.TokenKey);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim("Id", user.Id.ToString()),
                    new Claim("FirstName", user.FirstName.ToString()),
                    new Claim("LastName", user.FirstName.ToString()),
                    new Claim(ClaimTypes.Email, user.Email.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
        public ICommandResult Handle(LoginCommand command)
        {
            Email    email    = new Email(command.Email);
            Password password = new Password(command.Password);

            AddNotifications(email.Notifications);

            if (Invalid)
            {
                return(new CommandResult(false, MessagesUtil.FormFail, Notifications));
            }

            UserAuthQuery user = _repository.Login(email.Address, password.Value);

            if (user == null)
            {
                return(new CommandResult(false, MessagesUtil.UserNotFound));
            }

            return(new CommandResult(true, MessagesUtil.Welcome, data: user));
        }