Not just where (eg: SQLite...) but also how (libs, best specific practices)?
import 'package:flutter_secure_storage/flutter_secure_storage. dart'; // Create storage final storage = new FlutterSecureStorage(); // Write value await storage. write(key: 'jwt', value: token);
Storing JWT Token We need to store this token somewhere. We can store it as a client-side cookie or in a localStorage or sessionStorage. There are pros and cons in each option but for this app, we'll store it in sessionStorage. //persisted across tabs and new windows.
To keep them secure, you should always store JWTs inside an httpOnly cookie. This is a special kind of cookie that's only sent in HTTP requests to the server. It's never accessible (both for reading or writing) from JavaScript running in the browser.
You probably don't want to store sensitive data in shared preferences. Instead you might want to look into a plugin like this: https://pub.dartlang.org/packages/flutter_secure_storage
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; // Create storage final storage = new FlutterSecureStorage(); // Write value await storage.write(key: 'jwt', value: token);
As I mentioned on a deleted post, I've been using hive to storage my tokens and other local data. With hive it's possible to create an encrypted box
import 'dart:typed_data'; import 'package:hive/hive.dart'; void main() async { var keyBox = await Hive.openBox('encryptionKeyBox'); if (!keyBox.containsKey('key')) { var key = Hive.generateSecureKey(); keyBox.put('key', key); } var key = keyBox.get('key') as Uint8List; print('Encryption key: $key'); var encryptedBox = await Hive.openBox('vaultBox', encryptionKey: key); encryptedBox.put('secret', 'Hive is cool'); print(encryptedBox.get('secret')); }
As mentioned in comments:
The example above stores the encryption key in an unencrypted box. You should NEVER do that.
Important:
So, if you don't need any of
hive
specific features, flutter_secure_storage should be a better option for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With