Skip to content

Authentication

javier7ar edited this page Aug 6, 2018 · 13 revisions

Using Authentication

Samplers provides authentication with Google, but you will need to register you Application in Google. There, follow the steps to Configure a Google API Console project. You will need to provide your application's name and package name, and also the SHA-1 hash of your signing certificate.

Once you have registered your application in Google, you have to configure Samplers to enable authentication:

a) If you are using gradle class generator:

You need to configure the params in the application object:

  • Set the authenticationEnabled param to true to enable authentication.
  • Set the authenticationOptional param to true if you want the authentication to be optional, or to false if you want the authentication to be required.
  • In the networkConfiguration you will need to set the paramNameUserId and paramNameAuthenticationType params to the param name of the HTTP POST message used to send the user id and authentication type respectively.

Example:

{
  "application": {
    "title" : "Samplers Hello World App",
    "welcomeMessage" : "Welcome to your first Samplers App!",
    "networkConfiguration" : {
      "url" : "http://192.168.1.10/samplers/upload.php",
      "paramName" : "sample",
      "paramNameUserId" : "user_id",
      "paramNameAuthenticationType" : "authentication_type"
    },
    "authenticationEnabled" : true,
    "authenticationOptional" : true
  } 
}

b) If you are instantiating Samplers manually:

You will need to set the Network and Authentication configuration on the onCreate() method of your MainActivity

NetworkConfiguration.setURL("http://192.168.1.10/samplers/upload.php");
NetworkConfiguration.setPARAM_NAME_SAMPLE("sample");
// Set the authentication params of the Network Configuration
NetworkConfiguration.setPARAM_NAME_USER_ID("user_id");
NetworkConfiguration.setPARAM_NAME_AUTHENTICATION_TYPE("authentication_type");

// Set the authenticationconfiguration
AuthenticationManager.setAuthenticationEnabled(true);
AuthenticationManager.setAuthenticationOptional(true);

Once you have configured the Authentication params, Samplers will show a Login Fragment the first time the user tries to take a sample, to prompt him to login with Google. If the authentication is optional, Samplers shows a button to skip the login and continue with the taking of the sample. When the sample is sent, the user id and the authentication type ('google' by default) is sent with it.

Using a custom authentication method

You can also use a custom authentication method by defining your own Login Fragment and Custom User class (or classes if you will provide login with several APIs, like Facebook, Google, Yahoo, etc.) and then Samplers will send with the sample the user id and authentication type selected to login.

1. Defining your own Login Fragment:

You need to create a fragment, by extending the LoginFragment class and tell the AuthenticationManager to use it like this:

AuthenticationManager.setLoginFragmentClass(MyCustomLoginFragment.class);

The login process and the interaction with the APIs are your responsibility, but after the user is logged in your selected API you need to call the login() method on the AuthenticationManager and the onLogin() method on the mListener like this:

if (loginOK) {
  AuthenticationManager.login(user, getActivity().getApplicationContext());
  mListener.onLogin(user);
}

2. Defining a Custom User class:

You will need to create a custom user class that must implements the User interface. This is the user you will use to login on the AuthenticationManager.

Example:

public class EMailUser implements User {

    public static final String AUTHENTICATION_TYPE = "email";

    private String userName;
    private String email;

    public GoogleUser(String userName, String email) {
        this.userName = userName;
        this.email = email;
    }

    @Override
    public String getAuthenticationType() {
        return AUTHENTICATION_TYPE;
    }

    @Override
    public String getUserName() {
        return userName;
    }

    @Override
    public String getUserId() {
        return email;
    }

}