kylebrowning / waterwheel.swift

The Waterwheel Swift SDK provides classes to natively connect iOS, macOS, tvOS, and watchOS applications to Drupal 7 and 8.
MIT License
411 stars 93 forks source link

session trouble #30

Closed meSte closed 12 years ago

meSte commented 12 years ago

I'm trying to create an app that should be accessible only to logged users. Everything I tried works fine, but I can't figure out how to properly access the session once the user log in with the app. As a result I have to delete the app or the second login attempt fails with 406 http error. So my question is: what is the best way to "save" the logged session and reuse it later? (That should be connected to the question: how to properly detect if a user is already logged?) I'm using the latest dev and [DIOSSession sharedSession] is not working as I expect and gives me back a nil object.

voidberg commented 12 years ago

Same issue here.

voidberg commented 12 years ago

Here's how to fix this:

On the first login you'll need to save the session name and value from the authenticated call (I'm using the keychain). What you do is iterate the cookies in the success callback and when you find the session one you save the values.

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 
NSArray *cookies = [cookieStorage cookies]; 
for (NSHTTPCookie *cookie in cookies) { 
  if ([cookie.name hasPrefix:@"SESS"]) {
    [keychainS setObject:cookie.name forKey:(__bridge id)kSecAttrAccount];
    [keychainS setObject:cookie.value forKey:(__bridge id)kSecValueData];
  }
}

Now, next time the app starts you check if you have saved session values and if you do you add them to the header of the sharedSession and make a call to system.connect. If the old session didn't expire you'll get back the logged in user object.

NSString *sessionName = [keychainS objectForKey:(__bridge id)kSecAttrAccount];
NSString *sessionValue = [keychainS objectForKey:(__bridge id)kSecValueData];

if (![sessionName isEqualToString:@""] && ![sessionValue isEqualToString:@""]) {
  DIOSSession *session = [DIOSSession sharedSession];
  [session setDefaultHeader:@"Cookie" value:[NSString stringWithFormat:@"%@=%@", sessionName, sessionValue]];

  DIOSSystem *system = [[DIOSSystem alloc] init];
  [system systemConnectwithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {
  }
  failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  }];
}

There isn't any system.connect support in drupal-ios-sdk so I added it. If it hasn't been merged when you read this check out my fork for the code.

meSte commented 12 years ago

Perfect. Thanks a lot!

omero commented 12 years ago

will have an example of how to do i'm newbie and i have to make an app for the next monday u_u

regards¡¡¡

omero commented 12 years ago

excuse my bad English

ianmen commented 11 years ago

Is this issue still open ?

I seem to have the same problem with the following code : [[DIOSSession sharedSession] user]

After login this function always returns null, well if the user is logged in it should not return null.

Bob

brittanyblair commented 9 years ago

Hey @voidberg - Trying out your method above - but these lines are giving me grief:

[session setDefaultHeader:@"Cookie" value:[NSString stringWithFormat:@"%@=%@", sessionName, sessionValue]];

Error: No visible @interface for DIOSSession declares the selector 'setDefaultHeader:value'

and

[system systemConnectwithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {

same error, but with no visible @interface for DIOSSystem declares the selector systemConnectwithSuccess:failure.

Any help is appreciated - cheers.