This project demonstrates a simple authentication system using JWT (JSON Web Tokens) in a React Native app. It includes login, registration, and user listing functionalities using a backend API.
- User registration
- User login with JWT
- Authentication state management
- Secure token storage using
expo-secure-store
- Fetching and displaying a list of users from a protected endpoint
- React Native - Frontend framework for building mobile apps.
- Axios - HTTP client for making API requests.
- JWT - Token-based authentication.
- Expo Secure Store - Securely store and retrieve the JWT token.
- React Navigation - Navigation between screens.
- Express - For backend.
Before running the project, make sure you have the following tools installed:
- Node.js & npm
- Expo CLI:
npm install -g expo-cli
- Backend API server that provides
/register
,/login
, and/users
endpoints (using JWT for authentication).
-
Clone the repository:
git clone https://github.com/jerenat/react-native-auth-jwt.git
-
Navigate to the project directory:
cd react-native-auth-jwt
-
Install the dependencies:
npm install
-
If you are using Expo:
expo install
-
Start the Expo server:
expo start --android
-
Open the app on a mobile device using the Expo Go app, or run it on an emulator.
You need to configure your backend API URL. Modify the following in your AuthContext
:
export const API_URL = "http://localhost:3000";
Make sure that your backend API provides endpoints for user registration, login, and fetching users.
.
├── assets # Static assets (images, etc.)
├── components # Reusable React components
├── context # Authentication context
│ └── AuthContext.js
├── screens # App screens (Login, Home, etc.)
│ ├── Home.jsx
│ └── Login.jsx
├── App.js # Main app entry point
├── README.md # Project documentation
└── package.json # Project dependencies and scripts
- Fill in a username and password on the registration screen.
- Press "Create Account" to register a new user.
- Enter the username and password on the login screen.
- Press "Sign in" to authenticate the user.
- Upon successful login, a token will be saved in secure storage.
- After login, navigate to the Users screen.
- The app will fetch and display a list of users from the API.
react-native
- Framework for mobile development.axios
- HTTP client for API requests.expo-secure-store
- Secure token storage.react-navigation
- App navigation.express
- Backend API
- Improve error handling and input validation.
- Add form validation on login and registration screens.
- Implement refresh token mechanism.
- Add loading indicators during API requests.
Feel free to submit issues or pull requests if you want to contribute to this project.
This project is licensed under the MIT License.