예제 #1
0
        public async Task <ActionResult <ApiResultViewModel <SessionViewModel> > > Login([FromBody] LoginInputModel inputModel,
                                                                                         CancellationToken cancellationToken)
        {
            var account = await _accountManager.FindByEmailAsync(inputModel.Email, cancellationToken);

            if (account == null || account.IsArchived)
            {
                return(BadRequest("invalid_email", "Account not found"));
            }

            if (!PasswordHash.ValidatePassword(inputModel.Password, account.PasswordHash))
            {
                return(BadRequest("invalid_username_or_password", "Invalid Email or Password!"));
            }

            var token = _tokenGenerator.GenerateToken(TimeSpan.FromDays(365),
                                                      new Claim(JwtRegisteredClaimNames.Jti, account.Id.ToString()),
                                                      new Claim(JwtRegisteredClaimNames.Sub, account.Email),
                                                      new Claim("Timezone", account.Timezone));

            var session = new Session
            {
                AccessToken      = token,
                AccountId        = account.Id,
                CreationDateTime = DateTime.UtcNow,
                StateId          = SessionStateIds.Created,
                SourceAppId      = AppIds.Game
            };

            await _sessionManager.SaveAsync(session, cancellationToken);

            return(CreatedData(SessionViewModel.Map(session)));
        }
예제 #2
0
        public async Task <ActionResult <ApiResultViewModel <AccountViewModel> > > Register(
            [FromBody] RegisterInputModel inputModel, CancellationToken cancellationToken)
        {
            var account = await _accountManager.FindByEmailAsync(inputModel.Email, cancellationToken);

            if (account != null)
            {
                return(BadRequest("invalid_email", "Email already exists"));
            }

            account = new Account
            {
                Email                = inputModel.Email,
                PasswordHash         = PasswordHash.CreateHash(inputModel.Password),
                StatusId             = AccountStatusIds.Active,
                Timezone             = "Asia/Tehran",
                ReceiveNotifications = true,
                SearchableByEmailAddressOrUsername = true,
                FriendsOnlyBattleInvitations       = false
            };
            account.Nickname         = account.Email.Substring(0, account.Email.IndexOf('@'));
            account.RegisterDateTime = DateTime.UtcNow;
            account.GenderId         = GenderIds.Male;
            account = await _accountManager.SaveAsync(account, cancellationToken);

            var accountStats = new AccountStatsSummary
            {
                AccountId = account.Id,
                Level     = 1
            };
            await _statsManager.SaveAsync(accountStats, cancellationToken);

            await SetDefaultAvatar(account, cancellationToken);

            await _dataContext.SaveChangesAsync(cancellationToken);

            var token = _tokenGenerator.GenerateToken(TimeSpan.FromDays(365),
                                                      new Claim(JwtRegisteredClaimNames.Jti, account.Id.ToString()),
                                                      new Claim(JwtRegisteredClaimNames.Sub, account.Email),
                                                      new Claim("Timezone", account.Timezone));

            var session = new Session
            {
                AccessToken      = token,
                AccountId        = account.Id,
                CreationDateTime = DateTime.UtcNow,
                StateId          = SessionStateIds.Created,
                SourceAppId      = AppIds.Game
            };

            await _sessionManager.SaveAsync(session, cancellationToken);

            return(CreatedData(RegisterViewModel.GetRegisterViewModel(AccountViewModel.Map(account),
                                                                      SessionViewModel.Map(session))));
        }
예제 #3
0
        public async Task <ActionResult <ApiResultViewModel <SessionViewModel> > > GetSessionById(string id,
                                                                                                  CancellationToken cancellationToken)
        {
            var session = await _sessionManager.GetAsync(int.Parse(id), cancellationToken);

            if (session == null)
            {
                return(NotFound());
            }

            return(OkData(SessionViewModel.Map(session)));
        }
예제 #4
0
        public async Task <ActionResult <ApiResultViewModel <SessionViewModel> > > Login([FromBody] LoginInputModel model,
                                                                                         CancellationToken cancellationToken)
        {
            var account = await _accountManager.FindByEmailAsync(model.Email, cancellationToken);

            if (account == null)
            {
                return(BadRequest("invalid_username_or_password", "Invalid Username or Password!"));
            }

            if (!PasswordHash.ValidatePassword(model.Password, account.PasswordHash))
            {
                return(BadRequest("invalid_username_or_password", "Invalid Username or Password!"));
            }

            var roles = await _accountManager.GetRolesAsync(account, cancellationToken);

            if (!roles.Contains(RoleIds.Admin))
            {
                return(Forbidden());
            }

            if (!account.IsEmailVerified)
            {
                return(BadRequest("email_not_verified", "Please verify your email to log in."));
            }

            var token = _tokenGenerator.GenerateToken(TimeSpan.FromDays(365),
                                                      new Claim(JwtRegisteredClaimNames.Jti, account.Id.ToString()),
                                                      new Claim(JwtRegisteredClaimNames.Sub, account.Email));

            var session = new Session
            {
                AccessToken      = token,
                AccountId        = account.Id,
                CreationDateTime = DateTime.UtcNow,
                StateId          = SessionStateIds.Created,
                SourceAppId      = AppIds.Admin
            };

            await _sessionManager.SaveAsync(session, cancellationToken);

            return(CreatedData(SessionViewModel.Map(session)));
        }