Josh Kahane
Josh Kahane

Reputation: 17169

Google OAuth2 remembering user

I have been dabbling with the OAuth2 api Google offer in their Objective-C library.

Anyway, I am having troubles understanding how I get the app to remember who logged in. I have successfully gotten authentication working using the following.

I Call the below method to present the OAuth view controller from Google, the user signs in, and I'm authenticated. However, every time I relaunch the app, its runs the login screen again, as though I am not authenticated. As I understand it, I need to have some sort of keychain/token remembrance in place so that it recognises the user recently signed in.

But how? I can't work it out from the little documentation available.

-(void)authenticateUserFromViewController:(UIViewController *)viewController
{
    GTMOAuth2ViewControllerTouch *authViewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeYouTube
                                                                clientID:kClientID
                                                            clientSecret:kClientSecret
                                                        keychainItemName:kKeychainItemName
                                                                delegate:self
                                                        finishedSelector:@selector(viewController:finishedWithAuth:error:)];

    [viewController.navigationController pushViewController:authViewController animated:YES];
}

-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
{
    if (error != nil)
    {
        // Authentication failed
        NSLog(@"Failed to authorise");
    }
    else
    {
        // Authentication succeeded
        NSLog(@"Authorised");
    }
}

Upvotes: 1

Views: 290

Answers (1)

Cyril Kamowski
Cyril Kamowski

Reputation: 21

I am not familiar with the Objective-C library, but maybe this part of the Google Reference could be useful. It explains how to use the authentication tokens and how to handle the Keychain when the user relaunches the app.

Upvotes: 1

Related Questions