Beispiel #1
0
        public async Task <IActionResult> GetUserByUsername(string username)
        {
            var user = await _userManager.FindByNameAsync(username);

            if (user == null)
            {
                return(RequestHandler.UserNotFound());
            }

            var accountData = await _authContext.AccountData.FirstOrDefaultAsync(acc => acc.Id == user.AccountId);

            var account = await _authContext.Account.FirstOrDefaultAsync(x => x.Id == user.AccountId);

            var userDto = new WebAccDTO
            {
                Id         = user.Id,
                Username   = user.UserName,
                Firstname  = user.Firstname,
                Lastname   = user.Lastname,
                Email      = user.Email,
                VP         = accountData.Vp,
                DP         = accountData.Dp,
                TotalVotes = user.TotalVotes,
                AccountId  = user.AccountId,
                JoinDate   = user.JoinDate,
                Location   = user.Location,
                Roles      = user.UserRoles?.Select(x => x.Role.Name).ToArray(),
                Account    = account
            };

            return(Ok(userDto));
        }
Beispiel #2
0
        public async Task <IActionResult> Login([FromBody] LoginModel model)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(RequestHandler.WrongCredentials());
            }

            var passwordCheck = await _userManager.CheckPasswordAsync(user, model.Password);

            if (!passwordCheck)
            {
                return(RequestHandler.WrongCredentials());
            }

            var token = TokenHelper.GenerateToken(user, _jwtSecurityKey).SerializeToken();

            var accountData = await _authContext.AccountData.FirstOrDefaultAsync(acc => acc.Id == user.AccountId);

            var account = await _authContext.Account.FirstOrDefaultAsync(x => x.Id == user.AccountId);

            var userDto = new WebAccDTO
            {
                Id         = user.Id,
                Username   = user.UserName,
                Firstname  = user.Firstname,
                Lastname   = user.Lastname,
                Email      = user.Email,
                VP         = accountData.Vp,
                DP         = accountData.Dp,
                TotalVotes = user.TotalVotes,
                AccountId  = user.AccountId,
                JoinDate   = user.JoinDate,
                Location   = user.Location,
                Roles      = user.UserRoles?.Select(x => x.Role.Name).ToArray(),
                Account    = account
            };

            return(Ok(new
            {
                token,
                userDto
            }));
        }
Beispiel #3
0
        public async Task <IActionResult> Register([FromBody] RegisterModel model)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user != null)
            {
                return(RequestHandler.BadRequest("User with that email already exists"));
            }

            var newAccount = await CreateIngameAccount(model.Username, model.Password, model.Email);

            if (newAccount == null)
            {
                return(RequestHandler.BadRequest("Username is already taken"));
            }

            var newUser = new ApplicationUser
            {
                Id        = Guid.NewGuid(),
                AccountId = newAccount.Id,
                UserName  = model.Username,
                Firstname = model.Firstname,
                Lastname  = model.Lastname,
                Email     = model.Email,
                JoinDate  = DateTime.UtcNow
            };

            var result = await _userManager.CreateAsync(newUser, model.Password);

            if (!result.Succeeded)
            {
                return(RequestHandler.BadRequest(result.Errors));
            }

            var token = TokenHelper.GenerateToken(newUser, _jwtSecurityKey).SerializeToken();

            // All good - now we add and save the new entities
            var accountData = new AccountData
            {
                Id        = newAccount.Id,
                Dp        = 0,
                Vp        = 0,
                ExtraMask = 0
            };

            await _authContext.Account.AddAsync(newAccount);

            await _authContext.AccountData.AddAsync(accountData);

            await _authContext.SaveChangesAsync();

            var userDto = new WebAccDTO
            {
                Id         = newUser.Id,
                AccountId  = newUser.AccountId,
                Username   = newUser.UserName,
                Firstname  = newUser.Firstname,
                Lastname   = newUser.Lastname,
                Email      = newUser.Email,
                JoinDate   = DateTime.UtcNow,
                Roles      = new string[] { },
                Location   = "Unknown",
                VP         = 0,
                DP         = 0,
                TotalVotes = 0,
                Account    = newAccount
            };

            // Update Client
            var count = await _userManager.Users.CountAsync();

            var userInformation = new UserInformationResponse
            {
                Id       = newUser.Id,
                Email    = newUser.Email,
                Username = newUser.UserName
            };

            await _signalRHub.Clients.All.UpdateUserInformations(userInformation, count);

            return(Ok(new
            {
                token,
                userDto
            }));
        }