Skip to content

Commit

Permalink
Store username in preferences and make saving/setting credentials work.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Mar 23, 2011
1 parent 7285d7d commit aac9631
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
2 changes: 0 additions & 2 deletions Authentication.h.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
#define API_USER @"bob"
#define API_PASS @"secret"
#define API_KEY @"secret"
4 changes: 4 additions & 0 deletions Classes/Controllers/AuthenticationViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
@property (nonatomic, retain) IBOutlet UITableViewCell *passwordCell;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;

+ (BOOL)signIn;
+ (void)retrieveUsername:(NSString **)username password:(NSString **)password;
+ (void)authenticate:(NSString *)username password:(NSString *)password;

- (IBAction)dismissDialog:(id)sender;
- (IBAction)saveCredentials:(id)sender;
- (IBAction)textDidChange:(id)sender;
Expand Down
52 changes: 42 additions & 10 deletions Classes/Controllers/AuthenticationViewController.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
#import "AuthenticationViewController.h"
#import "Trakt.h"
//#import "SSKeychain.h"
#import "SSKeychain.h"

@implementation AuthenticationViewController


+ (BOOL)signIn {
NSString *username, *password;
[self retrieveUsername:&username password:&password];
if (username && password) {
NSLog(@"User: %@ Password: %@", username, password);
[self authenticate:username password:password];
return YES;
}
return NO;
}

+ (void)retrieveUsername:(NSString **)username password:(NSString **)password {
*password = nil;
*username = [[NSUserDefaults standardUserDefaults] objectForKey:@"Username"];
if (*username) {
*password = [SSKeychain passwordForService:@"iTrakt" account:*username];
}
}

+ (void)authenticate:(NSString *)username password:(NSString *)password {
[[Trakt sharedInstance] setApiUser:username];
[[Trakt sharedInstance] setApiPassword:password];
}


@synthesize usernameField, passwordField;
@synthesize usernameCell, passwordCell;
@synthesize doneButton;


- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"Hier!");
self.usernameField.text = [[Trakt sharedInstance] apiUser];
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

NSString *username, *password;
[AuthenticationViewController retrieveUsername:&username password:&password];
self.usernameField.text = username;
self.passwordField.text = password;
}


Expand Down Expand Up @@ -58,12 +87,15 @@ - (IBAction)dismissDialog:(id)sender {

- (IBAction)saveCredentials:(id)sender {
NSLog(@"Save!");
[[Trakt sharedInstance] setApiUser:self.usernameField.text];
[[Trakt sharedInstance] setApiPassword:self.passwordField.text];
NSString *username = self.usernameField.text;
NSString *password = self.passwordField.text;

[AuthenticationViewController authenticate:username password:password];

// TODO check if the credentials are correct somehow!
// Where do we store the account? prefs??
//[SSKeychain setPassword:self.passwordField.text forService:@"iTrakt" account:self.usernameField.text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:username forKey:@"Username"]; // TODO move to constant
[defaults synchronize];
[SSKeychain setPassword:password forService:@"iTrakt" account:username];

[self dismissDialog:sender];
}
Expand Down
10 changes: 6 additions & 4 deletions Classes/Controllers/iTraktAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
//NSLog(@"[!] Clearing cache");
//[[EGOCache currentCache] clearCache];

// TODO replace this with actual credentials
//[SSKeychain passwordForService:@"iTrakt" account:@""]; TODO hmm, account?
[[Trakt sharedInstance] setApiUser:API_USER];
[[Trakt sharedInstance] setApiPassword:API_PASS];
if ([AuthenticationViewController signIn]) {
NSLog(@"Signed in.");
} else {
NSLog(@"Not signed in.");
}

[[Trakt sharedInstance] setApiKey:API_KEY];

[HTTPDownload setGlobalDelegate:self];
Expand Down

0 comments on commit aac9631

Please sign in to comment.