def METHOD_NAME(request):
    state = request.session['discourse_state']
    url = request.session['discourse_url']
    private_key = RSA.import_key(request.session['private_key'])
    payload_encrypted = request.GET['payload']
    cipher_rsa = PKCS1_v1_5.new(private_key)
    sentinel = Random.new().read(100)
    payload = cipher_rsa.decrypt(base64.b64decode(payload_encrypted), sentinel).decode('utf-8', 'ignore')
    payload_body = payload[payload.index('{"key":'):] # Removes gobbledy-gook heading and returns json string
    payload_body_json = json.loads(payload_body)
    api_key = payload_body_json['key']
    request.session['discourse_api_key'] = api_key
    if state == 'policykit_discourse_user_login':
        user = authenticate(request, platform='discourse')
        if user:
            login(request, user)
            return redirect('/main')
        else:
            return redirect('/login?error=invalid_login')
    elif state == 'policykit_discourse_mod_install':
        community = None
        s = DiscourseCommunity.objects.filter(team_id=url)
        req = urllib.request.Request(url + '/about.json')
        req.add_header("User-Api-Key", api_key)
        resp = urllib.request.urlopen(req)
        res = json.loads(resp.read().decode('utf-8'))
        title = res['about']['title']
        if s.exists():
            community = s[0]
            community.community_name = title
            community.team_id = url
            community.api_key = api_key
            community.save()
            response = redirect('/login?success=true')
            return response
        else:
            community = DiscourseCommunity.objects.create(
                community_name=title,
                team_id=url,
                api_key=api_key,
            )
            # Get the list of users and create a DiscourseUser object for each user
            req = urllib.request.Request(url + '/admin/users/list.json')
            req.add_header("User-Api-Key", api_key)
            resp = urllib.request.urlopen(req)
            users = json.loads(resp.read().decode('utf-8'))
            for u in users:
                user_fields = get_discourse_user_fields(u, community)
                DiscourseUser.objects.update_or_create(
                    community=community, username=u['username'],
                    defaults=user_fields,
                )
            return render_starterkit_view(request, community.community.pk)
    return redirect('/login?error=no_community_found')