JS Framework
Ionic

User Registration

15min

How to add a login screen to your Ionic framework project

Introduction

In this section you learn how to create a page, implement sign up, sign in and sign out to your Ionic app.

This is how it will look like:

Document image


Prerequisites

To complete this quickstart, you need:

At any time, you can access the complete Ionic Project built with this tutorial at our GItHub repository.

1 - Install parse SDK

Considering you have an existing Ionic project, the first thing you need to do is to install parse SDK. You can do it by running:

$ npm install parse

2 - Set up app’s credentials

  1. Open your app.component.ts, import parse and initialize its connection to Back4App Parse server.
app.component.html


If you don’t know how to find your keys, check out the first Ionic tutorial Start From Template.

3 - Create the LogIn Page

Now, let’s create our LogIn page. Luckly, Ionic does everything to us. All we need to do is to run the following command:

$ ionic generate page Login

In this page view, we need to add inputs for username and password and two buttons, one for signing up and another one for signing in.

login.html


Let’s implement now the methods signIn() and signUp() referred in our Login view.

login.ts


Learn more about signUp() at Parse Documentation.

login.ts


Learn more about Parse.User.logIn() at Parse Documentation.

This is how it should look like.

Document image


4 - Implement Log out

Let’s move to our HomePage (or the page the user will be directed after logging in) and implement the sign out.

First, go ahead, open home.html and add a button for doing so.

login.html


Now, let’s implement the logOut() method and also add a Toast component if the request fails.

home.ts


Learn more about Parse.User.logOut() at Parse Documentation.

It should look like this.

Document image


5 - Set root page

An important feature of parse is that it remembers if a user is logged or not in a device. It means that even if the user closes the app, you can still restore his session when the app is open.

With that, we can determine if the app initial page will be our LoginPage or HomePage.

To do so, we just need to call currentAsync(). It will return the user logged in or null.

app.component.ts


Learn more about Parse.User.currentAsync() at Parse Documentation.

Finally, it’s all set up!

At this point, just run ionic serve and you will have a sign in, sign up and sign out features working that also remembers the logged user until he/she logs out.