Esempio n. 1
0
        public async Task <IActionResult> AppRegister(AppRegisterAddressModel model)
        {
            var  appId  = _tokenManager.ValidateAccessToken(model.AccessToken);
            bool exists = _dbContext.UserEmails.Any(t => t.EmailAddress == model.Email.ToLower());

            if (exists)
            {
                return(this.Protocol(ErrorType.NotEnoughResources, $"A user with email '{model.Email}' already exists!"));
            }
            var user = new APIUser
            {
                UserName         = model.Email,
                Email            = model.Email,
                NickName         = model.Email.Split('@')[0],
                PreferedLanguage = "en",
                IconFilePath     = Values.DefaultImagePath
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var primaryMail = new UserEmail
                {
                    EmailAddress  = model.Email.ToLower(),
                    OwnerId       = user.Id,
                    ValidateToken = Guid.NewGuid().ToString("N")
                };
                _dbContext.UserEmails.Add(primaryMail);
                await _dbContext.SaveChangesAsync();

                // Send him an confirmation email here:
                try
                {
                    await _emailSender.SendConfirmation(user.Id, primaryMail.EmailAddress, primaryMail.ValidateToken);
                }
                // Ignore smtp exception.
                catch (SmtpException) { }
                // Grant this app.
                if (!await user.HasAuthorizedApp(_dbContext, appId))
                {
                    await user.GrantTargetApp(_dbContext, appId);
                }
                return(this.Protocol(ErrorType.Success, "Successfully created your account."));
            }
            return(this.Protocol(ErrorType.NotEnoughResources, result.Errors.First().Description));
        }