Esempio n. 1
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))));
        }