Skip to content

Instantly share code, notes, and snippets.

@boucher
Created December 14, 2008 19:08
Show Gist options
  • Save boucher/35766 to your computer and use it in GitHub Desktop.
Save boucher/35766 to your computer and use it in GitHub Desktop.
var SharedLoginController = nil;
@implementation LoginController : CPObject
{
CPString _email;
CPString _sessionID;
int _userID;
BOOL _finishedLoading;
CPURLConnection _logoutConnection;
CPURLConnection _testConnection;
CPURLConnection _loginConnection;
}
+(LoginController)sharedLoginController
{
if(!SharedLoginController)
{
SharedLoginController = [[self alloc] init];
[SharedLoginController checkStatus];
}
return SharedLoginController;
}
-(id)init
{
self = [super init];
_email = nil;
_sessionID = nil;
_userID = 0;
_finishedLoading = NO;
[CPURLConnection setClassDelegate:self];
return self;
}
-(BOOL)finishedLoading
{
return _finishedLoading;
}
-(CPString)email
{
return _email;
}
-(void)setEmail:(CPString)aString
{
_email = aString;
[[CPUserSessionManager defaultManager] setUserIdentifier: aString];
}
-(CPString)sessionID
{
return _sessionID;
}
-(void)setSessionID:(CPString)aString
{
_sessionID = aString;
}
-(int)userID
{
return _userID;
}
-(void)setUserID:(int)anID
{
_userID = anID;
}
-(BOOL)finishedLoading
{
return _finishedLoading;
}
- (void)checkStatus
{
_testConnection = [CPURLConnection connectionWithRequest: [CPURLRequest requestWithURL: BASE_URL + "checklogin.php"] delegate: self];
}
- (void)logout:(id)sender
{
_logoutConnection = [CPURLConnection connectionWithRequest: [CPURLRequest requestWithURL: BASE_URL + "logout.php"] delegate: self];
}
- (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
{
var response = CPJSObjectCreateWithJSON(data);//.response;
if(aConnection == _logoutConnection)
{
if(!response.error)
{
[[CPUserSessionManager defaultManager] setStatus: CPUserSessionLoggedOutStatus];
}
}
else if(aConnection == _testConnection)
{
if(response.error)
{
[[CPUserSessionManager defaultManager] setStatus: CPUserSessionLoggedOutStatus];
}
else if(response.result)
{
[[CPUserSessionManager defaultManager] setStatus: CPUserSessionLoggedInStatus];
var result = response.result;
[self setEmail: result.email];
[self setUserID: result.user_id];
[self setSessionID: result.session_id];
}
_doneLoading = YES;
}
}
- (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)anError
{
[self connectionDidFinishLoading:aConnection];
}
- (void)connectionDidFinishLoading:(CPURLConnection)aConnection
{
}
- (void)connectionDidReceiveAuthenticationChallenge:(CPURLConnection)aConnection
{
_loginConnection = aConnection;
[[LoginPanel sharedLoginPanel] beginWithModalDelegate:self
didEndSelector:@selector(processLogin:result:context:)
contextInfo:nil];
}
- (void)processLogin:(id)sender result:(const)result context:(id)contextInfo
{
if(result == LOGIN_SUCCESS)
{
[_loginConnection cancel];
[_loginConnection start];
}
else
[[_loginConnection delegate] connection:_loginConnection didFailWithError: [_loginConnection _XMLHTTPRequest].responseText];
_loginConnection = nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment