Lesson 15

Offline Access and Reauthentication

Allowing offline access and automatic reauthentication

PRO

Lesson Outline

Offline Access and Reauthentication

At the beginning of this module we set two requirements that we have no yet met:

  • If a user has previously logged in, and they have an unexpired token, they should be automatically logged in when returning to the application
  • Users should have offline access to the data in the application that syncs when online

Although we have not yet implemented this functionality, we already have most of the work done. We already store a users information in local storage when they log in, which includes a token for re-authentication, and we already have all the data we need stored locally in PouchDB, so we don't need an Internet connection to access existing data. As we have already discussed, PouchDB supports modifying data locally and syncing when online, so there's no issue there either.

We just need to make a couple of changes to the application so that the user doesn't get stuck at the login screen when they are offline and if they have already logged in and have a valid token. In order to access the application while offline the user will have to have already logged in previously while online - it's not possible to authenticate whilst offline.

Modify the Auth Service

In order to facilitate this functionality, we are going to add an additional function to our Auth service called reauthenticate. This will attempt to make use of the user data stored in local storage to perform the authentication - if a valid token is available then the authentication will pass without having to check against the server.

PRO

Thanks for checking out the preview of this lesson!

You do not have the appropriate membership to view the full lesson. If you would like full access to this module you can view membership options (or log in if you are already have an appropriate membership).