Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Request: maintaining a session? #66

Closed
utneon opened this issue Oct 10, 2012 · 9 comments
Closed

Support Request: maintaining a session? #66

utneon opened this issue Oct 10, 2012 · 9 comments

Comments

@utneon
Copy link

utneon commented Oct 10, 2012

How do I verify if a session is open in Drupal using the DIOS SDK? I don't find it in the documentation. I think that the documentation is also a bit outdated. Thank you.

@utneon
Copy link
Author

utneon commented Oct 10, 2012

I think I have found the solution:

Save the cookie session with login:

[DIOSUser userLoginWithUsername:[NSString stringWithFormat:@"%@", username.text] andPassword:[NSString stringWithFormat:@"%@", password.text] success:^(AFHTTPRequestOperation *operation, id responseObject) {
//successful
#ifdef DEBUG
NSLog(@"login successful: %@", responseObject);
#endif

    //If you have logged in, this object is not nil
    [session setUser:responseObject];

    // Guardar o cookie da sessão na aplicação.
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [cookieStorage cookies];
    for (NSHTTPCookie *cookie in cookies) {
        if ([cookie.name hasPrefix:@"SESS"]) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults removeObjectForKey:@"cookiename"];
            [defaults removeObjectForKey:@"cookievalue"];
            [defaults setObject:cookie.name forKey:@"cookiename"];
            [defaults setObject:cookie.value forKey:@"cookievalue"];
            [defaults synchronize];
        }
    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failed

#ifdef DEBUG
NSLog(@"login failed");
#endif
}];

When app loads send the session cookie in the Header:

// Ir buscar os dados da última sessão através do NSUserDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *sessionName = [defaults objectForKey:@"cookiename"];
NSString *sessionValue = [defaults objectForKey:@"cookievalue"];

// Enviar os cookies da sessão no header da sessão.
if (![sessionName isEqualToString:@""] && ![sessionValue isEqualToString:@""]) {
    [session setDefaultHeader:@"Cookie" value:[NSString stringWithFormat:@"%@=%@", sessionName, sessionValue]];
}

// Testar a conexão ao services Drupal
[DIOSSystem systemConnectwithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

#ifdef DEBUG
NSLog(@"success: %@", responseObject);
#endif
// Definir o user da sessão.
[session setUser:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
#ifdef DEBUG
NSLog(@"not success %@", operation);
#endif
}];

Is this correct? -> [session setUser:responseObject]; or should it be [session setUser:[responseObject objectForKey:@"user"]];?

@kylebrowning
Copy link
Owner

You shouldnt have to sore the cookies at all, AFNetworking handles all of that and will resume the session until the cookies expire.

@m1n0
Copy link

m1n0 commented Mar 26, 2013

@kylebrowning
Ok you say that we shouldnt take care of cookies...but would you care to show an example how to solve the issue above?

@kylebrowning
Copy link
Owner

@m1n0 What do you mean? Basically you can store the user information returned from user Login by doing this.

[session setUser:userDictInfo];

But you must keep in mind if you used responseObject it wont be retained properly.

If you dont want to store that information thats okay, AFNetworking will keep the cookie until it expires. If you want to validate a user is logged in, you can use the system Resource and call 'connect'

Does that help?

@m1n0
Copy link

m1n0 commented Mar 26, 2013

Thanks, yes that is exactly what I need, I am just not sure if I know what you mean by "...you can use the system Resource and call 'connect'"

@kylebrowning
Copy link
Owner

https://github.com/workhabitinc/drupal-ios-sdk/blob/master/DIOSSystem.m

You dont HAVE to do it with System, if you wanted you could use userLogin again and you should get a failure saying the user is already logged in.

@m1n0
Copy link

m1n0 commented Mar 26, 2013

Ok, thanks a lot!

@paewrblue
Copy link

How can I apply session to next ViewController after logging ?
I use this method, and I got null out of every ViewController, and please tell me where to put the code, I'm very new to IOS.
DIOSSession *session = [DIOSSession sharedSession];
//If you have logged in, this object is not nil
[session user];
//This can also be referenced as
[[DIOSSession sharedSession] user];

@andrewjlow
Copy link

@paewrblue You should be able to just check the session - [DIOSSession sharedSession]. There is no need to check for the user.

I too got confused. I think perhaps the documentation needs to be updated as based on the example in the documentation:

DIOSSession *session = [DIOSSession sharedSession];
//If you have logged in, this object is not nil
[session user];

I too thought the user is implicitly set to the session after logging whereas (from the above comments) I believe you will need to explicitly set the user to the session - [session setUser:userDictInfo];. This is most likely why you are probably nil when you just call [[DIOSSession sharedSession] user];

I hope it helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants