flutter_session_jwt 1.1.0 copy "flutter_session_jwt: ^1.1.0" to clipboard
flutter_session_jwt: ^1.1.0 copied to clipboard

A flutter package which makes easy to use JWT token to store and get details encoded in JWT Token.

flutter_session_jwt #

This package allows you to store the JWT token in secure storage and can decode the json web token. Since the payload is base64 encoded you can easily know the payload data stored with no password required, there are other methods available to get expiry date, issued date, and can check whether token expired or not.

This package can help you to store the JWT token in secure storage and provide you different methods to access information from the token.

Note: Make sure to save the token before accessing other methods.

Getting started #

In your pubspec.yaml file within your Flutter Project:

dependencies:
  flutter_session_jwt: <latest_version>
copied to clipboard

Example Screenshot #

Example screenshot

Usage #

Import the package

import 'package:flutter_session_jwt/flutter_session_jwt.dart';
copied to clipboard

Here is an exmaple to store the JWT token post login

Future<http.Response> login(String userName , String password) async{
  var response = await http.post(
    Uri.parse('https://jsonplaceholder.typicode.com/albums'),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      'userName': userName,
      'password': password
    }),
  );

  if(response.statusCode == 200){
    var token = response.body.token;
    await FlutterSessionJwt.saveToken(token);
  }
}
copied to clipboard

Once token is saved, you can access the other methods as below.

  • To get payload from JWT token
//This will return payload object/map
await FlutterSessionJwt.getPayload();
copied to clipboard
  • To retrieve saved token
//This method will return saved token for further API calls
await FlutterSessionJwt.retrieveToken();
copied to clipboard
  • To get expiration date and time
//Make sure pass `exp` key in the payload
//This method will return expiration ```DateTime```
await FlutterSessionJwt.getExpirationDateTime();
copied to clipboard
  • To get issued date and time
//Make sure pass `iat` key in the payload
//This method will return issuedAt ```DateTime```
 await FlutterSessionJwt.getIssuedDateTime();
copied to clipboard
  • To get whether token has expired or not
//This will return bool with true/false
//If token expired, it will return true else false
await FlutterSessionJwt.isTokenExpired();
copied to clipboard
  • To get the time difference between issued time and current time
//This will return the token's age since issue
await FlutterSessionJwt.getDurationFromIssuedTime();
copied to clipboard
  • To delete the token from storage
//This will delete the token
await FlutterSessionJwt.deleteToken();
copied to clipboard

License #

MIT

9
likes
160
points
1.31k
downloads

Publisher

unverified uploader

Weekly Downloads

2024.07.01 - 2025.05.26

A flutter package which makes easy to use JWT token to store and get details encoded in JWT Token.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_secure_storage

More

Packages that depend on flutter_session_jwt