0% found this document useful (0 votes)
24 views3 pages

Flutter Token Store Process

The document outlines the secure storage of authentication and notification tokens in Flutter using FlutterSecureStorage and Firebase Messaging. It provides code snippets for saving, retrieving, and deleting tokens, as well as sending notification tokens to a backend API. Best practices include securely storing tokens, implementing expiration and refresh mechanisms, and ensuring backend synchronization when tokens change.

Uploaded by

kalek72088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Flutter Token Store Process

The document outlines the secure storage of authentication and notification tokens in Flutter using FlutterSecureStorage and Firebase Messaging. It provides code snippets for saving, retrieving, and deleting tokens, as well as sending notification tokens to a backend API. Best practices include securely storing tokens, implementing expiration and refresh mechanisms, and ensuring backend synchronization when tokens change.

Uploaded by

kalek72088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Storage of Tokens in Flu er

Authen ca on Token Storage


 Store the token securely for API requests:
dart
final storage = Flu erSecureStorage();

// Save token
await storage.write(key: 'authToken', value: jwtToken);

// Retrieve token
final authToken = await storage.read(key: 'authToken');

// Delete token
await storage.delete(key: 'authToken');
No fica on Token Storage
 Use Firebase Messaging to get the token and store it:
1. Install Firebase Messaging:
bash
flu er pub add firebase_messaging
2. Retrieve and Store No fica on Token:
dart
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flu er_secure_storage/flu er_secure_storage.dart';

final FirebaseMessaging messaging = FirebaseMessaging.instance;


final storage = Flu erSecureStorage();
Future<void> storeNo fica onToken() async {
// Get FCM token
final fcmToken = await messaging.getToken();
print("No fica on Token: $fcmToken");

// Save FCM token


await storage.write(key: 'no fica onToken', value: fcmToken);
}

Future<String?> getNo fica onToken() async {


return await storage.read(key: 'no fica onToken');
}
3. Send to Backend: Send this no fica on token to your backend API
so it can be associated with the user:
dart
Future<void> sendNo fica onTokenToBackend(String token) async {
final response = await h p.post(
Uri.parse('h p://your-backend-api.com/save-token'),
headers: {'Content-Type': 'applica on/json'},
body: json.encode({'no fica onToken': token}),
);

if (response.statusCode == 200) {
print('Token sent successfully');
} else {
print('Error sending token');
}
}

Complete Workflow
1. Authen ca on Token:
o Backend par generate hota hai.
o App mein securely store kiya jata hai.
o Har API request ke sath send hota hai (Authoriza on header
mein).
2. No fica on Token:
o Firebase Messaging se milta hai.
o App aur backend dono par store hota hai.
o No fica ons bhejne ke liye backend is token ko target karta hai.

Best Prac ces


1. Authen ca on Token:
o Securely store using flu er_secure_storage.
o Token expira on aur refresh ka mechanism implement karein.
2. No fica on Token:
o Backend par update karein jab token change ho (e.g., app reinstall
ya device reset ke baad).
o Firebase se automa cally handle ho hai token genera on, lekin
aapko backend par sync karna hoga.

You might also like