public async Task <ICommandResult <LoginUserDTO> > HandleAsync(AuthenticationLoginCommand command)
        {
            var user = await _context
                       .User
                       .Include(p => p.UserPrivilege)
                       .Include(p => p.UserSettings)
                       .Include(p => p.ImageFile)
                       .Include(p => p.UserCoursePrivilege)
                       .Include(p => p.Subscription)
                       .FirstOrDefaultAsync(x => x.Username == command.Username);

            if (user == null || !verifyPasswordHash(command.Password, user.PasswordHash, user.PasswordSalt))
            {
                throw new Exception("Invalid login!");
            }

            UserPrivilegeDTO privileges = new UserPrivilegeDTO()
            {
                GeneralPrivileges = user.UserPrivilege.Select(x => x.PrivilegeId).ToList(),
                Courses           = user.UserCoursePrivilege
                                    .GroupBy(x => x.CourseId)
                                    .Select(x => new UserCoursePrivilegeDTO()
                {
                    Id         = x.FirstOrDefault().CourseId,
                    Privileges = user
                                 .UserCoursePrivilege
                                 .Where(y => y.CourseId == x.FirstOrDefault().CourseId)
                                 .Select(y => y.PrivilegeId)
                                 .ToList()
                })
                                    .ToList()
            };

            List <int> subscriptions = user.Subscription.Select(x => (int)x.CourseId).ToList();

            var claims = new[] {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.Username),
                new Claim("Privileges", JsonConvert.SerializeObject(privileges)),
                new Claim("Subscriptions", JsonConvert.SerializeObject(subscriptions))
            };

            // In order to make sure the claims are valid, created a key and hash it
            var key         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            // Create the token
            var tokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(3),
                SigningCredentials = credentials
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            var userSettings = user.UserSettings.FirstOrDefault();
            var settings     = new UserSettingsQueryModel()
            {
                DarkMode = userSettings.DarkMode,
                Locale   = userSettings.Locale,
                Popups   = userSettings.Popups
            };

            return(CommandResult <LoginUserDTO> .Success(new LoginUserDTO()
            {
                Id = user.Id,
                Name = user.Name,
                Surname = user.Surname,
                Username = user.Username,
                Picture = user.ImageFile != null ? Convert.ToBase64String(user.ImageFile.Data) : null,
                Settings = settings,
                Privileges = privileges,
                Token = tokenHandler.WriteToken(token)
            }));
        }
Example #2
0
        public async Task <IActionResult> Register(AuthenticationLoginCommand command)
        {
            var userInfo = await _commandBus.ExecuteAsync <LoginUserDTO>(command);

            return(Ok(userInfo));
        }