django-allauth logging in with Facebook token from iOS Device

前端 未结 2 792
轻奢々
轻奢々 2021-02-02 01:43

I am using the Facebook iOS SDK to POST the Facebook Access Token to my Django server URI. The corresponding views.py function is shown below and I get a 200 Response code when

2条回答
  •  迷失自我
    2021-02-02 02:02

    I have run into a very similar problem as yours, in implementing Facebook login from an iOS app to a server running django-allauth. I noticed in the successful POST response in iOS that the sessionid cookie was not being automatically saved as it normally is. I believe that's the reason your subsequent calls are being denied and redirected to your main page.

    Adding the following line seemed to solve it for me, but I admit that I do not have a complete understanding of why it works. Something to do with refreshing the session key, perhaps. Anyway, since there were no other answers, thought this might be helpful for you to try:

    user = User.objects.get(email=email) # Get User
    # Login the user from Django's perspective
    user.backend = 'django.contrib.auth.backends.ModelBackend'
    auth_login(request,user)
    request.session.cycle_key() #Refresh session key
    

    Then, on the iOS app side, I check whether there exists a session cookie:

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:WEB_APP_BASE_URL]];
    for (NSHTTPCookie *cookie in cookies)
    {
        if ([cookie.name isEqualToString:@"sessionid"]) {
            NSLog(@"found session cookie: %@",cookie.value);
        }
    }
    

提交回复
热议问题