DEV Community

Robertino
Robertino

Posted on

📲 Get Started with Android Authentication using Kotlin

📓 Learn how to implement login, logout, and user profiles in Android apps using Kotlin and Auth0.


If you’re planning to build a native Android app that requires users to log in, you should use Auth0.

Implementing user login/logout often turns into its own project. Just implementing the many ways users want to log in can easily take a lot of time. You’d also have to deal with issues such as user management, scaling, and security, each of which could have its own article.

With Auth0 and a few lines of code, your app can have a full-featured system that supports logging in with a basic username/password combination, single sign-on and social accounts, passwordless login, biometrics, and more. You won’t have to handle the “behind the scenes” issues, either!

This tutorial covers the basics of using Auth0 to implement login/logout in an Android app written in Kotlin. It also provides an introduction to reading and writing information into user profiles so that your app can customize itself for each user. Along the way, you’ll become familiar with the Auth0 dashboard and learn how to use it to register applications and users.

Look for the 🛠 emoji if you’d like to skim through the content while focusing on the build and execution steps.

What you’ll build

You’ll build a simple, single-screen Android app that will allow the user to log in and log out using Auth0. While logged in, the user will be able to see the following information from their user profile, namely:

  • Information that is part and parcel of the user account, namely:
    • The user’s name
    • The user’s email address
  • Metadata attached to the user account, namely:
    • The user’s country

The user will also be able to edit their country, and the result of that edit will be stored in the user account.

When you launch the completed app, you’ll see a greeting, a Log in button, and a disabled Log out button:

The finished app’s screen when launched. The greeting “Welcome to the app!” appears above the “Log in” and “Log out” buttons.

Tapping the Log in button takes the user to the login screen, which appears in a custom Chrome tab:

The default Auth0 login web page. It lets the user enter username and password or log in using a Google account.

When you use Auth0 in your apps, you delegate authentication to a centralized login page in the same way that Google properties such as Gmail and YouTube redirect you to accounts.google.com when you log in. In this exercise, you’ll use the default “look and feel” for the login page, but you can customize it to match your app or organization’s branding.

Logging in successfully returns you to the app, where you’ll see the following changes:

The finished app’s screen when the user is logged in. It displays information about the logged-in user.

  • The greeting will have changed to a notification telling you that you’re now logged into the app.
  • The Log in button will now be disabled, and the Log out button will now be enabled.
  • The name and email address for the user account that you used to log in will be displayed.
  • There will now be a section for metadata, complete with an editable text box (an EditText in Android parlance) and Get and Set buttons that will allow you to read and write to the country field of the metadata for the user account.

As you might expect, logging out is done by tapping the Log out button. Once logged out, you see the following changes onscreen:

The finished app’s screen when the user is logged out. The title bar reads “You’re logged out”.

  • The notification at the top of the screen will now inform you that you’re logged out of the app.
  • Logging out enables the Log in button and disables the Log out button.
  • The other controls that were visible when you were logged in will no longer be visible.

Read more...

Discussion (0)