コード例 #1
0
        /// <summary>
        /// Externals the login.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="ipAddress">The ip address.</param>
        /// <returns></returns>
        public async Task <object> ExternalLogin(GoogleAuthenticationRequest request, string ipAddress)
        {
            var internalUserFilter = Builders <User> .Filter.Eq("email", request.User.Email);

            var existInternalUser = await userRepository.FindAsync(internalUserFilter);

            if (existInternalUser != null)
            {
                var finder = Builders <ExternalLogin> .Filter.Eq("email", request.User.Email);

                var existExternalLogin = await externalLoginRepository.FindAsync(finder);

                if (existExternalLogin == null)
                {
                    var externalLogin = new ExternalLogin()
                    {
                        Email         = request.User.Email,
                        LoginProvider = Constants.ExternalLoginConstants.GOOGLE_EXTERNAL_PROVIDER
                    };

                    await externalLoginRepository.AddAsync(externalLogin);
                }

                Account account = accountRepository.GetAll().FirstOrDefault(x => x.Email == existExternalLogin.Email);

                string       jwtToken     = accountService.generateJwtToken(account);
                RefreshToken refreshToken = accountService.generateRefreshToken(ipAddress);
                account.RefreshTokens.Add(refreshToken);
                accountService.removeOldRefreshTokens(account);

                accountRepository.Update(account, account.Id);

                AuthenticateResponse response = mapper.Map <AuthenticateResponse>(account);
                response.JwtToken     = jwtToken;
                response.RefreshToken = refreshToken.Token;
                return(response);
            }
            else
            {
                return(request);
            }
        }