Search code examples
iosquickblox

Quicklox token required


I am using quickblox in my app. I did my user signup, user login and chat. Everything works fine when i log in for the first time. if I press home and enter the app again, I come to my viewController page. The user is logged out automatically. And when I try to log in it says, token is required.

I have put the session authentication in appdelegate

 [QBAuth createSessionWithDelegate:self]
    -(void)viewDidAppear:(BOOL)animated
    {

    if([LocalStorageService shared].currentUser == nil)// check if user is logged in
    {
        NSCharacterSet *nonalphanumericSet = [[ NSCharacterSet alphanumericCharacterSet] invertedSet];

        [QBUsers logInWithUserLogin:[[[PFUser currentUser].username componentsSeparatedByCharactersInSet:nonalphanumericSet ] componentsJoinedByString:@"1"] password:@"password" delegate:self];
    }
    }

    - (void)completedWithResult:(Result *)result
    {
    if(result.success && [result isKindOfClass:QBUUserLogInResult.class])
    {
        // Success, do something
        QBUUserLogInResult *userResult = (QBUUserLogInResult *)result;
        NSLog(@"Logged In user=%@", userResult.user);


            userResult.user.password =@"password";

        // Save current user
        //
        [[LocalStorageService shared] setCurrentUser: userResult.user];


        // Login to QuickBlox Chat
        //
        [[ChatService instance] loginWithUser:[LocalStorageService shared].currentUser completionBlock:^{

            NSLog(@"chat logged successfully");


        }];

        // Errors
    }
    else
    {
        NSString *errorMessage = [[result.errors description] stringByReplacingOccurrencesOfString:@"(" withString:@""];
        errorMessage = [errorMessage stringByReplacingOccurrencesOfString:@")" withString:@""];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Errors"
                                                        message:errorMessage
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles: nil];
        [alert show];

    }

}

Solution

  • token is required means that you are trying to perform request, but you didn't create a session still

    You can reproduce this issue just perform 2 requests one by one:

    [QBAuth createSessionWithDelegate:self];
    [QBUsers logInWithUserLogin:... delegate:self];
    

    After this sequence you will got 'token is required'

    To get rid of this you should wait when create session requests will be finished and perform next request:

    [QBAuth createSessionWithDelegate:self];
    

    ...

    - (void)completedWithResult:(Result *)result{
         if(result.success && [result isKindOfClass:QBAAuthSessionCreationResult.class]){
    
             // you got token here - perform any other requests after this
    
             [QBUsers logInWithUserLogin:... delegate:self];
    
         }
    }
    

    It's not a solution for your issue, but it's an explanation hy it happenes and how to resolve it