Beispiel #1
0
        private ProfileModel ConvertToProfileModel(DAL.Entities.Profile profile)
        {
            var profileModel = new ProfileModel
            {
                Friends   = profile.Friends.Count,
                Posts     = profile.Posts.Count,
                Id        = profile.Id,
                UserId    = profile.UserId,
                Status    = profile.Status,
                UserModel = new UserModel
                {
                    Email    = profile.User.Email,
                    UserName = profile.User.UserName,
                    Id       = profile.UserId
                }
            };

            return(profileModel);
        }
        public async Task <UserResponseModel> Apple(AppleLogInModel model)
        {
            User user;

            if (!string.IsNullOrWhiteSpace(model.Email))
            {
                user = await userManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    user.UserName = model.Name;

                    await userManager.UpdateAsync(user);

                    var jwtToken = GenerateJwtToken(user);

                    return(Ok(user, jwtToken));
                }

                user = await userManager.FindByLoginAsync(LogInProviders.Apple, model.AppleId);

                if (user != null)
                {
                    user.UserName = model.Name;
                    user.Email    = model.Email;

                    await userManager.UpdateAsync(user);

                    var jwtToken = GenerateJwtToken(user);

                    return(Ok(user, jwtToken));
                }

                user = new User
                {
                    UserName = model.Name,

                    Email = model.Email
                };
                user.NormalizedEmail    = userManager.NormalizeEmail(model.Email);
                user.NormalizedUserName = userManager.NormalizeName(model.Name);

                var profile = new DAL.Entities.Profile
                {
                    User   = user,
                    UserId = user.Id,
                    Status = "New artist"
                };
                user.Profile = profile;

                var identity = await userManager.CreateAsync(user);

                if (!identity.Succeeded)
                {
                    return(BadRequest(identity.Errors.FirstOrDefault().Description));
                }

                identity = await userManager.AddLoginAsync(user, new UserLoginInfo(LogInProviders.Apple, model.AppleId, "AppleId"));

                if (!identity.Succeeded)
                {
                    return(BadRequest(identity.Errors.FirstOrDefault().Description));
                }

                var jwt = GenerateJwtToken(user);

                return(Ok(user, jwt));
            }
            return(null);
        }