def get(self, request): if request.GET.get('error'): messages.error(request, _('Login failed.')) return HttpResponseRedirect(reverse('login')) code = request.GET.get('code') if not code: messages.error(request, _('Login failed.')) return HttpResponseRedirect(reverse('login')) flow = get_google_oauth_flow(request) try: credentials = flow.step2_exchange(code) except FlowExchangeError: messages.error(request, _('Login with Google is currently not possible.')) logger.exception('Login with Google failed') return HttpResponseRedirect(reverse('login')) email = self._get_email(credentials.token_response) # Connect account if request.user.is_authenticated: request.user.google_email = email request.user.save() messages.success( request, _('Your account has been connected with ' '{google}. Open Settings to change this.'.format( google=email)), ) return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT) # Check if Google account is connected User = get_user_model() try: user = User.objects.get(profile__google_email=email) except User.DoesNotExist: # Connect account messages.error( request, _('No account connected with your Google ' 'account %s. Please log in to connect.' % email), ) return HttpResponseRedirect('{login}?next={connect}'.format( login=reverse('login'), connect=reverse('login-google'))) # Log in user # TODO: this should probably be replaced with a call to authenticate() # http://stackoverflow.com/questions/6034763/django-attributeerror-user-object-has-no-attribute-backend-but-it-does user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)
def get(self, request): if request.GET.get("error"): messages.error(request, _("Login failed.")) return HttpResponseRedirect(reverse("login")) code = request.GET.get("code") if not code: messages.error(request, _("Login failed.")) return HttpResponseRedirect(reverse("login")) flow = get_google_oauth_flow(request) try: credentials = flow.step2_exchange(code) except FlowExchangeError: messages.error(request, _("Login with Google is currently not possible.")) logger.exception("Login with Google failed") return HttpResponseRedirect(reverse("login")) email = self._get_email(credentials.token_response) # Connect account if request.user.is_authenticated(): request.user.google_email = email request.user.save() messages.success( request, _( "Your account has been connected with " "{google}. Open Settings to change this.".format(google=email) ), ) return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT) # Check if Google account is connected User = get_user_model() try: user = User.objects.get(profile__google_email=email) except User.DoesNotExist: # Connect account messages.error( request, _("No account connected with your Google " "account %s. Please log in to connect." % email) ) return HttpResponseRedirect( "{login}?next={connect}".format(login=reverse("login"), connect=reverse("login-google")) ) # Log in user # TODO: this should probably be replaced with a call to authenticate() # http://stackoverflow.com/questions/6034763/django-attributeerror-user-object-has-no-attribute-backend-but-it-does user.backend = "django.contrib.auth.backends.ModelBackend" login(request, user) return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)
def get(self, request): if request.GET.get('error'): messages.error(request, _('Login failed.')) return HttpResponseRedirect(reverse('login')) code = request.GET.get('code') if not code: messages.error(request, _('Login failed.')) return HttpResponseRedirect(reverse('login')) flow = get_google_oauth_flow(request) try: credentials = flow.step2_exchange(code) except FlowExchangeError: messages.error(request, _('Login with Google is currently not possible.')) logger.exception('Login with Google failed') return HttpResponseRedirect(reverse('login')) email = credentials.token_response['id_token']['email'] # Connect account if request.user.is_authenticated(): set_users_google_email(request.user, email) messages.success(request, _('Your account has been connected with ' '{google}. Open Settings to change this.'.format( google=email))) return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT) # Check if Google account is connected user = user_by_google_email(email) if not user: # Connect account messages.error(request, _('No account connected with your Google ' 'account %s. Please log in to connect.' % email)) return HttpResponseRedirect('{login}?next={connect}'.format( login=reverse('login'), connect=reverse('login-google'))) # Log in user login(request, user) return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)
def get(self, request): flow = get_google_oauth_flow(request) auth_uri = flow.step1_get_authorize_url() return HttpResponseRedirect(auth_uri)