Exemple #1
0
        public async Task <LoginUserDto> IssueLoginTokenWithFacebookSignInAsync(FacebookOAuthRequestDto request)
        {
            // 1: verify the token
            var accessTokenVerifyUrl             = string.Format(_facebookOAuthConfigs.TokenVerifyUrl, request.Token, _facebookOAuthConfigs.Access_token);
            var facebookOAuthAccessTokenResponse = await IdentityHelper.CheckFacebookOAuthAccessToken(accessTokenVerifyUrl);

            if (facebookOAuthAccessTokenResponse.Data.User_id != request.UserId)
            {
                throw new BadRequestException("facebook login fail");
            }

            // 2. create user if user not exsist
            var user = await _userRepository.GetUserByUserNameAsync(UserNameHelper.GenerateUserNameFromEmail(request.Email));

            if (user == null)
            {
                user = await ApplicationUser.CreateAsync(_userManager, request.Email, null, request.FirstName, request.LastName, request.ImageUrl);

                user.ManullActivate();
            }
            // 3. reset google login
            await _userManager.RemoveLoginAsync(user, "Facebook", request.UserId);

            await _userManager.AddLoginAsync(user, new UserLoginInfo("Facebook", request.UserId, request.Token));

            // 4. if user data not match with Google
            if (request.FirstName != user.FirstName || request.LastName != user.LastName || request.ImageUrl != user.AvatarUrl)
            {
                user.UpdateProfile(request.FirstName, request.LastName, request.ImageUrl);
            }
            // 5.issue a token to the user
            return(await GenerateLoginUserDto(user));
        }