Ejemplo n.º 1
0
        public async Task <string> SocialSignIn(SocialSignInViewModel model)
        {
            await _authValidationService.ValidateSocialSignInViewModel(model);

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                user = new OmdleUser
                {
                    UserName  = model.UserName,
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    Email     = model.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    throw new SocialSignInFailedException(
                              $"An error occured while signing in Facebook user: {result.Errors.Select(e => e.Description).Join(", ")}");
                }

                await _userManager.AddToRoleAsync(user, "Student");
            }

            await _signInManager.SignInAsync(user, false);

            var token = await GetToken(user);

            return(token);
        }
Ejemplo n.º 2
0
        /// <summary>Validates the social sign in view model.</summary>
        /// <param name="model">The model.</param>
        /// <returns>Task.</returns>
        /// <exception cref="SocialSignInFailedException">Email cannot be null!
        /// or
        /// UserName cannot be null!
        /// or
        /// First name cannot be null!
        /// or
        /// Last name cannot be null!</exception>
        public Task ValidateSocialSignInViewModel(SocialSignInViewModel model)
        {
            if (string.IsNullOrEmpty(model.Email))
            {
                throw new SocialSignInFailedException(
                          $"Email cannot be null!");
            }

            if (string.IsNullOrEmpty(model.UserName))
            {
                throw new SocialSignInFailedException(
                          $"UserName cannot be null!");
            }
            if (string.IsNullOrEmpty(model.FirstName))
            {
                throw new SocialSignInFailedException(
                          $"First name cannot be null!");
            }
            if (string.IsNullOrEmpty(model.LastName))
            {
                throw new SocialSignInFailedException(
                          $"Last name cannot be null!");
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 3
0
        public void SocialSignIn_CreateAsyncFailed_SocialSignInFailedException()
        {
            var userManager           = new FakeUserManager();
            var signInManager         = new FakeSignInManager();
            var authValidationService = new Mock <IAuthValidationService>().Object;
            var configuration         = new Mock <IConfiguration>().Object;
            var dataService           = new Mock <IDataService>().Object;

            var authService = new AuthService(userManager, signInManager, authValidationService,
                                              configuration, dataService);

            var model = new SocialSignInViewModel
            {
                UserName  = "******",
                Email     = "email",
                FirstName = "firstName",
                LastName  = "lastName"
            };

            //Act
            Func <Task> result = async() =>
            {
                await authService.SocialSignIn(model);
            };

            result.Should().Throw <SocialSignInFailedException>();
        }
Ejemplo n.º 4
0
        public async Task <JsonResult> SocialLogin([FromBody] SocialSignInViewModel model)
        {
            try
            {
                var token = await _authService.SocialSignIn(model);

                return(Json(token));
            }
            catch (SignInFailedException ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(ex.Message));
            }
        }
Ejemplo n.º 5
0
        public void ValidateSocialSignInViewModel_InvalidCredentials_ThrowsSocialSignInFailedException(string userName, string email, string firstName, string lastName)
        {
            var authValidationService = new AuthValidationService();

            var model = new SocialSignInViewModel
            {
                UserName  = userName,
                Email     = email,
                FirstName = firstName,
                LastName  = lastName
            };

            Action result = () => authValidationService.ValidateSocialSignInViewModel(model);

            result.Should().Throw <SocialSignInFailedException>();
        }