Beispiel #1
0
    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)
Beispiel #2
0
 def post(self, request):
     set_users_google_email(request.user, None)
     messages.success(request, _('Your account has been disconnected'))
     return HttpResponseRedirect(reverse('account'))