Created
August 10, 2014 01:31
-
-
Save pnicholls/3d9cc6ce06591587ea91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SSDataKit.h" | |
@class CDKList; | |
@class CDKTask; | |
extern NSString *const kCDKCurrentUserChangedNotificationName; | |
@interface CDKUser : SSRemoteManagedObject | |
@property (nonatomic, strong) NSString *firstName; | |
@property (nonatomic, strong) NSString *lastName; | |
@property (nonatomic, strong) NSString *username; | |
@property (nonatomic, strong) NSString *email; | |
@property (nonatomic, strong) NSNumber *hasPlus; | |
@property (nonatomic, strong) NSSet *tasks; | |
@property (nonatomic, strong) NSSet *lists; | |
@property (nonatomic, strong) NSString *accessToken; | |
+ (CDKUser *)currentUser; | |
+ (void)setCurrentUser:(CDKUser *)user; | |
@end | |
NSString *const kCDKCurrentUserChangedNotificationName = @"CHCurrentUserChangedNotification"; | |
static NSString *const kCDKUserIDKey = @"CDKUserID"; | |
static CDKUser *__currentUser = nil; | |
@implementation CDKUser | |
@dynamic firstName; | |
@dynamic lastName; | |
@dynamic username; | |
@dynamic email; | |
@dynamic tasks; | |
@dynamic lists; | |
@dynamic hasPlus; | |
@synthesize accessToken = _accessToken; | |
+ (NSString *)entityName { | |
return @"User"; | |
} | |
+ (CDKUser *)currentUser { | |
if (!__currentUser) { | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
NSNumber *userID = [userDefaults objectForKey:kCDKUserIDKey]; | |
if (!userID) { | |
return nil; | |
} | |
NSError *error = nil; | |
NSString *accessToken = [SSKeychain passwordForService:kCDKKeychainServiceName account:userID.description error:&error]; | |
if (!accessToken) { | |
NSLog(@"[CheddarKit] Failed to get access token: %@", error); | |
return nil; | |
} | |
__currentUser = [self existingObjectWithRemoteID:userID]; | |
__currentUser.accessToken = accessToken; | |
} | |
return __currentUser; | |
} | |
+ (void)setCurrentUser:(CDKUser *)user { | |
if (__currentUser) { | |
[SSKeychain deletePasswordForService:kCDKKeychainServiceName account:__currentUser.remoteID.description]; | |
} | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
if (!user.remoteID || !user.accessToken) { | |
__currentUser = nil; | |
[userDefaults removeObjectForKey:kCDKUserIDKey]; | |
} else { | |
NSError *error = nil; | |
[SSKeychain setPassword:user.accessToken forService:kCDKKeychainServiceName account:user.remoteID.description error:&error]; | |
if (error) { | |
NSLog(@"[CheddarKit] Failed to save access token: %@", error); | |
} | |
__currentUser = user; | |
[userDefaults setObject:user.remoteID forKey:kCDKUserIDKey]; | |
} | |
[userDefaults synchronize]; | |
[[NSNotificationCenter defaultCenter] postNotificationName:kCDKCurrentUserChangedNotificationName object:user]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment