A Json Web Token or JWT is a token or string which is typically associated with one or more attribute of user data which can uniquely identify a user
It is typically sent from a server to a client(such as mobile app) in the response of a user authentication request
The client then uses this token for subsequent requests to the server, restricting user credentials to authentication apis
A jwt can be associated with an expiry period and additional session data, which is implemented on the server side
Provided application requires a server which performs the authentication and returns a Jwt
Check Nodejs Jwt Authentication Server to implement such a server in Nodejs
Check React Native Installation for installation related info
Use react-native init
command to create a new React Native project (here named NcJwtAuthApp)
This creates a directory named NcJwtAuthApp and initializes it as a react native application directory
This can be skipped in case an existing react native project is being integrated into
Since this example uses NavigationDrawer component, related packages have to be added
Go to project directory (here NcJwtAuthApp)
Add react-navigation
react-navigation-drawer
&& react-navigation-stack
and associated dependencies to current project using yarn
or using npm
After yarn add
command, cd into ios folder inside project directory
Use pod command to install any required CocoaPods dependencies:
Login Page
LoginPage component renders two textinputs to get username and password from user and a button which initiates a request to the server
username and password is send as body of request, where password is encoded prior to being sent
With basic base64 encoding the password can be easily decoded if request is sniffed by an attacker
Something like AES encryption is recommended when sending user credentials, to enhance security
Response from server, which contains the jwt, is sent to ViewUser
page, to be used for subsequent requests to server
Check for presence of token in server response is omitted here
ViewUser page
To ger user info, authorization string is created with username and password
jwt is retrieved from state
attribute of navigation object, which is passed as props to ViewUser component
userData is the server response sent from Login page
Prefix Bearer
is according to server implementation, which can be omitted and/or additional encryption can be implemented on the token
Resultant authentication string is then passed as a header for requests to the server
Authorization
key used in header can also be changed to some other name, while the server is configured to parse that key
User info is retrieved as specific fields from the response received from server and used to set state which gets rendered
Following code of App.js
defines
ViewUser
class displays some info about a user
cd into project directory (here NcJwtAuthApp)
Run metro server to serve js
Go to project directory in another terminal tab
Enter following run command for Android:
cd into project directory (here NcJwtAuthApp)
Enter following command :
This might take some time
If the app shows error about metro server not configured (check Running an App in iOS), then run metro server in another tab:
Reload the app
Re-run react-native run-ios
command if reloading doesn't work