Flutter JWT Authentication Plugin
This plugin provides an easy-to-use JWT (JSON Web Token) authentication solution for Flutter applications. It simplifies token storage, retrieval, and renewal, allowing for secure and seamless user session management.
Features
- Login & Logout: Easily authenticate users and clear sessions.
- Token Storage: Securely store and access tokens using
flutter_secure_storage
. - Token Renewal: Automatically manage token expiration and refresh.
- State management: Useful state management with Bloc.
Installation
Add the plugin to your pubspec.yaml
:
dependencies:
flutter_jwt_auth: ^1.0.0
flutter_secure_storage: ^5.0.2
dio: ^5.7.0
flutter_bloc: ^8.1.6
flutter pub get
Usage
This plugin uses some library, the most important is Bloc for state management and Dio for http client request management. Dio is useful for the interceptors feature. This plugin automatically refresh token when expired.
Initialize the Plugin
Initialize the plugin with any required configuration before using it. This should typically be done at the start of your app, such as in the main method.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:jwt_auth/bloc/jwt_authentication_bloc.dart';
import 'package:jwt_auth/bloc/jwt_authentication_event.dart';
import 'dart:async';
import 'package:jwt_auth/jwt_authentication_repository.dart';
import 'package:jwt_auth/models/jwt_auth_config.dart';
import 'package:jwt_auth/models/jwt_auth_log_level.dart';
import 'package:jwt_auth_example/app.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final Dio = Dio();
late final JwtAuthenticationRepository _authenticationRepository;
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
super.initState();
_authenticationRepository = JwtAuthenticationRepository(
config: JwtAuthConfig(
tokenUrl: 'http://10.0.2.2:5244/api/v1/auth/token', //your login endpoint
refreshUrl: 'http://10.0.2.2:5244/api/v1/auth/refreshToken', //your refreshToken endpoint
logLevel: JwtAuthLogLevel.none,
),
);
dio.interceptors.add(JwtAuthenticationInterceptor(repository)); //for authomatically refresh token
}
@override
void dispose() {
_authenticationRepository.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return RepositoryProvider.value(
value: _authenticationRepository,
child: BlocProvider(
lazy: false,
create: (_) => JwtAuthenticationBloc(
authenticationRepository: _authenticationRepository,
)..add(JwtAuthenticationInitializationRequested()),
child: const AppView(),
),
);
}
}
Libraries
- bloc/jwt_authentication_bloc
- bloc/jwt_authentication_event
- bloc/jwt_authentication_state
- jwt_auth_api_client
- jwt_auth_flutter
- jwt_auth_flutter_method_channel
- jwt_auth_flutter_platform_interface
- jwt_auth_flutter_web
- jwt_auth_method_channel
- jwt_auth_platform_interface
- jwt_auth_storage
- jwt_auth_web
- jwt_authentication_interceptor
- jwt_authentication_repository
- models/jwt_auth_config
- models/jwt_auth_log_level
- models/jwt_authentication_status
- models/jwt_response_error
- models/jwt_token
- requests/refresh_token_request
- requests/token_request