Skip to content

User authentication with login, signup, OTP verification, automatic token refresh, and secure storage using Dio HTTP client and Flutter Secure Storage.

Notifications You must be signed in to change notification settings

MAHMOUDELSAYED7/Auth-with-Refresh-Token

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flutter Authentication with Refresh Token

Overview

This Flutter project demonstrates handling authentication processes such as login, signup, OTP verification, and automatic token refresh using the Dio HTTP client, with secure storage management using Flutter Secure Storage.

Features

  • User Authentication (Login, Signup)
  • OTP Verification
  • Automatic Token Refresh using Interceptors
  • Secure Token Storage

Usage

Authentication

  • Login: Use ApiService().login(email, password) to authenticate users.
  • Signup: Use ApiService().signUp(name, email, password) to register new users.
  • Verify OTP: Use ApiService().verifyOtp(email, otp) for OTP verification.

Token Management

  • Securely Store Tokens: Use SecureStorageManager.writeData('accessToken', token) to store tokens securely.
  • Read Tokens: Use SecureStorageManager.readData('accessToken') to retrieve tokens securely.

Secure API Requests

Ensure all API requests are authenticated using the access token retrieved from secure storage:

Future<Response> getProductByToken() async {
  final token = await SecureStorageManager.readData('accessToken');
  final response = await _dio.get('/product/1',
      options: Options(headers: {'Authorization': 'Bearer $token'}));
  return response;
}

Handling Token Expiry

The RefreshTokenInterceptor automatically handles token expiration by refreshing the token and retrying the original request seamlessly when encountering 401 or 403 errors.

intain secure sessions across multiple services or APIs.

Contribution

Contributions to the project are welcome! Please fork the repository and submit a pull request with your features or fixes.

Issues

If you encounter any issues while using this project, please open an issue in the repository with a detailed description of the problem, steps to reproduce it, and the expected behavior. We appreciate your contributions to improving this project.

Contact

For any questions or feedback, please reach out via email: [email protected]