Using Login Screenlet for Authentication

For the app to retrieve data from the Guestbook portlet, the user must first authenticate to the Liferay DXP instance. You can implement authentication using the Liferay Mobile SDK, but it takes time to write. Using Liferay Screens to authenticate takes about ten minutes. In this article, you’ll use Login Screenlet to implement authentication in your app.

Adding Login Screenlet to the App

To use any Screenlet, you must follow two steps:

  1. Insert the Screenlet’s XML in the layout of the activity or fragment where you want the Screenlet to appear.

  2. Implement the Screenlet’s listener in the activity or fragment class.

In this app, you’ll use Login Screenlet in MainActivity. This means you must insert the Screenlet’s XML in MainActivity’s layout, activity_main.xml. You’ll then implement Login Screenlet’s listener, LoginListener, in the MainActivity class.

Insert the Screenlet’s XML

Follow these steps to insert Login Screenlet’s XML in activity_main.xml:

  1. Open activity_main.xml from the res/layout folder and delete the TextView generated by Android Studio when you created the project. Insert Login Screenlet’s XML in its place:

    <com.liferay.mobile.screens.auth.login.LoginScreenlet
        android:id="@+id/login_screenlet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:basicAuthMethod="screen_name"
        app:layoutId="@layout/login_default"
        />
    

    Note the two app attributes in the Login Screenlet’s XML. The app:basicAuthMethod attribute tells the Screenlet to use basic authentication instead of OAuth. The screen_name value tells the Screenlet to authenticate with the user’s screen name. You can alternatively set this to email or userId. This Learning Path uses screen_name only because it’s much faster to type a screen name than a full email address in the emulator. Also, this value must match the authentication setting in the Liferay DXP instance. By default, Liferay DXP instances use email address for authentication. For this Learning Path, you need to set your Liferay DXP instance to authenticate by screen name instead. Click here for instructions on changing your Liferay DXP instance’s authentication setting.

    The second app attribute in Login Screenlet’s XML is app:layoutId. This attribute sets the View to display the Screenlet with. Views in Liferay Screens set a Screenlet’s look and feel independent of the Screenlet’s core functionality. You can think of them as a sort of skin or theme for a Screenlet. The value @layout/login_default specifies Login Screenlet’s Default View, which is part of the Default View Set. A View Set is a collection of Views for several Screenlets. Using a View Set lets you apply a consistent look and feel across multiple Screenlets. To use a View that is part of a View Set, like Login Screenlet’s Default View, the theme of the app or activity must inherit the theme that sets the View Set’s styles. For the Default View Set, this is default_theme.

  2. To set the app’s theme to inherit from default_theme, open res/values/styles.xml and set the base app theme’s parent to default_theme. In this app, the base app theme is AppTheme. The theme declaration should now look like this:

    <style name="AppTheme" parent="default_theme">
        ...
    

Click here for more information on using Views in Liferay Screens. For more information on Login Screenlet’s available attributes, click here.

Next, you’ll implement LoginListener in the MainActivity class.

Implement the Screenlet’s Listener

To use a Screenlet in an activity or fragment, you must also implement the Screenlet’s listener in that activity or fragment’s class. You’ll do this now to use Login Screenlet in MainActivity:

  1. Open MainActivity and change its declaration to implement LoginListener. The class declaration should now look like this:

    public class MainActivity extends AppCompatActivity implements LoginListener {...
    

    You must also import com.liferay.mobile.screens.auth.login.LoginListener.

  2. Implementing LoginListener requires you to implement the onLoginSuccess and onLoginFailure methods. Add them to the class as follows:

    @Override
    public void onLoginSuccess(User user) {
        Toast.makeText(this, "Login successful!", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onLoginFailure(Exception e) {
        Toast.makeText(this, "Couldn't log in " + e.getMessage(), Toast.LENGTH_LONG).show();
    }
    

    When you add these methods, you must import android.widget.Toast and com.liferay.mobile.screens.context.User.

    These are listener methods called when login succeeds or fails, respectively. Using them lets your app respond the Screenlet’s actions. For the moment, they each only do one thing: display a success or failure message to the user. You’ll change this shortly. Note that each Screenlet has different listener methods; they’re listed in the Screenlet reference documentation.

  3. Now you must get a reference to the Screenlet and set the MainActivity class as its listener. To do so, add the following code to the end of the onCreate method:

    LoginScreenlet loginScreenlet = (LoginScreenlet) findViewById(R.id.login_screenlet);
    loginScreenlet.setListener(this);
    

    This requires you to import com.liferay.mobile.screens.auth.login.LoginScreenlet.

    The findViewById method uses the Screenlet’s ID from the layout to create the reference. The setListener method then sets MainActivity as Login Screenlet’s listener.

Now run the app by clicking the green play button in the toolbar, or by selecting Run ‘app’ from the Run menu. If you’ve never run the emulator, you must first create and choose an Android Virtual Device (AVD) to run your app. For more information on this and running the emulator in general, click here. Once the emulator launches, unlock it if necessary. Your app automatically opens to Login Screenlet. Enter your credentials and click SIGN IN. The toast message pops up saying that the login succeeded.

Figure 1: Login Screenlet successfully authenticated you with the Liferay DXP instance.

Figure 1: Login Screenlet successfully authenticated you with the Liferay DXP instance.

The toast message goes away and you remain on the login screen. Nothing else happens. Don’t worry, this is supposed to happen; you haven’t added any other functionality yet. You’ll fix this next.

When login succeeds, the app should open GuestbooksActivity. You’ll do this by using an Android intent in MainActivity’s onLoginSuccess method:

  1. Replace the contents of onLoginSuccess with this code:

    Intent intent = new Intent(this, GuestbooksActivity.class);
    startActivity(intent);
    

    When login succeeds, this code creates an Intent and uses it to start GuestbooksActivity. If you haven’t already, make sure to import android.content.Intent in MainActivity.

  2. Now you’re ready to see the intent in action! Run the app in the emulator and log in when prompted. When login succeeds, GuestbooksActivity opens.

Figure 2: Upon login, the app takes you to the new activity.

Figure 2: Upon login, the app takes you to the new activity.

Nice work! You successfully implemented Liferay DXP authentication in your Android app. It didn’t take you that long, either. So far, however, that’s all your app does; it doesn’t display any content. Next, you’ll rectify this by developing Guestbook List Screenlet.

« Designing Your AppCreating Guestbook List Screenlet »
Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0