User Login & Authentication - GrasspIt/GrasspReactNativeDriverApp GitHub Wiki

User authentication for this app begins in the navigation folder with AuthNavigator. This determines whether to show the DashboardScreen or the LoginScreen when the app is opened.

Auto Login

As soon as the user opens the app, preloadAccessTokenFromLocalStorage is called to see if there is a valid access token stored on the device in SecureStore.

If there is a valid access token, the user is automatically logged in and taken to the main navigation stack. If not, they remain on the LoginScreen.

If there is an expired token, they are logged out via the api middleware and returned to the LoginScreen.

The user sees a loading spinner or Activity Indicator until this is resolved.

Login Screen

If the user submits the login form with an email and password, a request to login will be sent to the api via attemptLogin.

If the login fails, the user will receive an alert with the error message 'Invalid email or password.'

If it succeeds, the user's info will be fetched from the api via getLoggedInUser.

Auth Navigator

If the logged in user has no DSPR drivers associated with their account, they will be logged out, returned to the LoginScreen, and receive an alert that they must be a DSPR driver to use the app.

If the user has one DSPR driver associated with their account, that driver's id will be set as the dsprDriverId in state. They will be then be brought to the DashboardScreen in the DrawerNavigator.

If the user has multiple drivers associated with their account, they will be taken to the DSPRScreen where they will see a list of the DSPs associated with each of the drivers on their account. Once they select a DSP from the list, that driver's id will be set in state and they will be taken to the DashboardScreen for that driver.

Logging Out

A user may logout by opening the navigation drawer and pressing the logout button. This returns them to the LoginScreen.