Logging in to Relativity Server

Logging in to Relativity Server

Finally you need to remove the warning found in the needLogin:password: method of AppDelegate.m.

This is one of the methods belonging to the DataAccessDelegate protocol. When the DataAccess class connects to Relativity Server, it will need to provide login credentials to access the data. The DataAccess class will call this method on its delegate property with the expectation that the login and password parameters will be set with the required login credentials.

When you set up Relativity Server, you configured it to handle access via the DbTableLoginProvider, so access credentials would be stored in the Users table of the database. When a user logs in, they will need to provide credentials that match a record in that table.

Implementing Login

For test purposes, you could cheat right now by hardcoding the login to match one of the test records in the database.

//AppDelegate.m
- (BOOL)needLogin:(NSString **)login password:(NSString **)password {
    // use it as default values (for test purposes)
    *login = @"Daniel";
    *password = @"222";
    return YES;
}

You might get away with this code for testing, but in your final application, you obviously will require a login window to allow users to enter their login credentials.

Login Window

This sample doesn't step through all the stages of implementing the new login window, as it's just normal AppKit/Cocoa stuff and not part of Data Abstract. You will find the required code in the sample code that comes with this tutorial.

Once you have implemented a login window, the needLogin:password: method should look something like this:

//AppDelegate.m
- (BOOL)needLogin:(NSString **)login password:(NSString **)password {
    NSWindow *loginWindow = self.loginWindowController.window;
    self.loginWindowController.userName = *login;
    self.loginWindowController.password = *password;
    
    NSModalResponse result = [NSApp runModalForWindow:loginWindow];
    [NSApp endSheet:loginWindow];
    [loginWindow orderOut: self];
    if (result == NSModalResponseOK) {
        *login = self.loginWindowController.userName;
        *password = self.loginWindowController.password;
        return YES;
    }
    
    return NO;
}

Ensure the user has to log in

By default, the remoteDataAdapterNeedsLogin: method of the DataAccess object will store the details from a successful login for future use. As you will want to test this sample with multiple user credentials, you will need to change the code in remoteDataAdapterNeedsLogin: to prevent it from reading this information, so the user is forced to log in each time. You can do this by commenting out one line of code and and replacing it with another.

//DataAccess.m
- (BOOL)remoteDataAdapterNeedsLogin:(DARemoteDataAdapter *)adapter
{
    ...
    SEL lLoginNeededSelector = @selector(needLogin:password:);
    if ([(id)delegate respondsToSelector:lLoginNeededSelector])
    {
      //NSString *login [[NSUserDefaults standardUserDefaults] stringForKey:USERDEFAULTS_USERNAME_KEY];
      NSString *login = nil;
      NSString *password = nil;
    ...