US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No 405, Kabir Shilp, Opp. Kansar Hotel, Opp. Landmark, Kudasan, Gandhinagar, Gujarat 382421

[email protected]

What’s the Best Way to Save JWT Tokens In Flutter Apps?

What's the Best Way to Save JWT Tokens In Flutter Apps

What’s the Best Way to Save JWT Tokens In Flutter Apps?

Hello, hope you guys are enjoying our articles based on flutter. So in today’s article, we will go through What’s the Best Way to Save JWT Tokens In Flutter Apps.

What’s the Best Way to Save JWT Tokens In Flutter Apps?

Many times users don’t want to store their sensitive data in shared preferences. But take a look at the plugin we have listed: https://pub.dev/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);

Moreover, you can also achieve this with the hive. Users can create an encrypted box according to their needs.

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'));
}

The above example will store the encryption key in an unencrypted box. But let us tell you that you should never follow this approach. There are some points which you need to remember. We have listed these points below.

  • Whenever the values are encrypted, the keys will remain in plaintext.
  • Also, when your application is not open you will need to make sure to securely store the encryption key. For this purpose, you can use the flutter_secure_storage or a similar package in the app.
  • There is no way to check if the encryption key is correct. If it is not correct then developers may encounter a few problems.

You can use https://pub.dev/packages/shared_preferences. This is the best option for you because it provides a persistent store for simple data.

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      body: Center(
      child: RaisedButton(
        onPressed: _getAndSaveToken,
        child: Text('Get token'),
        ),
      ),
    ),
  ));
}

_getAndSaveToken() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  String token = await _getTokenFromHttp();
  await prefs.setInt('jwt', token);
}

Future<String> _getTokenFromHttp() async {
  // http code here
}

Conclusion:

So, in this article, we have seen What’s the Best Way to Save JWT Tokens In Flutter Apps. Do let us know your feedback/comments on the same. Flutter Agency is the best Flutter App development company dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc. So, contact us for your next project.

Post a Comment