示例#1
0
        public async Task <LoginUserDto> IssueLoginTokenWithGoogleSignInAsync(GoogleOAuthRequestDto request)
        {
            // 1: verify the token
            var accessTokenVerifyUrl           = string.Format(_googleOAuthConfigs.AccessTokenVerifyUrl, request.Access_token);
            var googleOAuthAccessTokenResponse = await IdentityHelper.CheckGoogleOAuthAccessToken(accessTokenVerifyUrl);

            // 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.GivenName, request.FamilyName, request.ImageUrl);

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

            await _userManager.AddLoginAsync(user, new UserLoginInfo("Google", request.GoogleId, request.Id_token));

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