问题
I am trying to bypass the login page to welcome page if the person already have a token. I am using basic auth and afnetworking for the api call. Once the user logs the username&password, I base 64 the info and get a token. I save the token in a nsobject class where I create a token singleton and a simple is logged in method where it checks if the user has the token or not. But for some reason I always see the login page(meaning it skips my condition method). If any one can point out where I am making the mistake, it would be great.
This is class where I create the login singleton
CredentialStore.h
#import <Foundation/Foundation.h>
#import "LoginInfo.h"
@interface CredentialStore : NSObject
-(BOOL)isLoggedIn;
@property (nonatomic) LoginInfo *loginInfo;
+(id)sharedStore;
@end
CredentialStore.m
#import "CredentialStore.h"
static CredentialStore *sharedInsance;
@implementation CredentialStore
+(id)sharedStore
{
if (!sharedInsance) {
sharedInsance = [[CredentialStore alloc] init];
}
return sharedInsance;
}
- (BOOL)isLoggedIn {
return (self.loginInfo.authToken != nil);
}
@end
this is my authenticationapimanager class
AuthAPIManager.h
#import "AFHTTPSessionManager.h"
@interface AuthAPIManager : AFHTTPSessionManager
+ (id)sharedManager;
@end
AuthAPIManager.m
#import "AuthAPIManager.h"
#import "CredentialStore.h"
#define BASE_URL @"http://Url"
#define Base_Proxy @"http://Url"
static AuthAPIManager *sharedManager;
@implementation AuthAPIManager
//Setup the singleton to use throught the life of the application
+(id)sharedManager
{
if (!sharedManager) {
sharedManager = [[AuthAPIManager alloc] initWithBaseURL:[NSURL URLWithString:Base_Proxy]];
}
return sharedManager;
}
-(id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
}
return self;
}
@end
this is the class where I save the login information
LoginInfo.h
#import <Foundation/Foundation.h>
@interface LoginInfo : NSObject
@property(nonatomic,copy)NSNumber *AccountId;
@property(nonatomic,copy)NSString *DeviceType;
@property(nonatomic,copy)NSString *HardwareId;
@property(nonatomic,copy)NSString *NickName;
@property(nonatomic,copy)NSString *authToken;
-(id)initWithDictionary:(NSDictionary *)dictionary;
@end
LoginInfo.m
#import "LoginInfo.h"
@implementation LoginInfo
-(id)initWithDictionary:(NSDictionary *)dictionary
{
self =[super init];
if (self) {
self.AccountId = [dictionary objectForKey:@"AccountId"];
self.DeviceType = [dictionary objectForKey:@"DeviceType"];
self.HardwareId = [dictionary objectForKey:@"HardwareId"];
self.NickName = [dictionary objectForKey:@"NickName"];
}
return self;
}
and in my view controller, in viewwillappearmethod, I check if the user has a token or not but this is the part I am having problems with
LoginViewController.m
@interface LoginViewController ()
@property (nonatomic,strong) CredentialStore *credentialStore;
@end
implementation LoginViewController
#pragma mark - UIViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self checkIfThePersonLoggedIn];
}
-(void)checkIfThePersonLoggedIn
{
CredentialStore *credStore = [CredentialStore sharedStore];
// //check to see if the use has the token already
if (credStore.loginInfo.authToken != nil) {
[self.loginButton setHidden:YES];
[self performSegueWithIdentifier:@"welcomeViewSegue" sender:self];
}
else [self.loginButton setHidden:NO];
NSLog(@"checkForToken - loginviewcontroller");
}
and in my getTokenRequest this is how I save the token in the event of success
NSString *authToken = [responseObject objectForKey:@"Token"];
CredentialStore *credStore = [CredentialStore sharedStore];
LoginInfo * loginInfo = credStore.loginInfo;
loginInfo.authToken = authToken;
NSLog(@"this is the token here %@",authToken);
[self performSegueWithIdentifier:@"welcomeViewSegue" sender:self];
I appreciate the help.
this is where I instantiate login info
- (IBAction)login:(id)sender
{
[_usernameTextField resignFirstResponder];
[SVProgressHUD show];
id LoginParams =@{
@"NickName" : self.usernameTextField.text,
@"password" :_StoreIdentifierForVendor,
@"DeviceType" :_DeviceModel
};
[[AuthAPIManager sharedManager]POST:@"/url" parameters:LoginParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response: %@", responseObject);
LoginInfo *loginInfo = [[LoginInfo alloc] initWithDictionary:responseObject];
CredentialStore *credStore = [CredentialStore sharedStore];
credStore.loginInfo = loginInfo;
[self getToken];
}failure....
来源:https://stackoverflow.com/questions/20310831/token-is-saved-but-unable-to-bypass-the-login-page