forked from eatvio/eatvio_app
30 lines
853 B
Dart
30 lines
853 B
Dart
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/locator_service.dart';
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
abstract class TokenStorage {
|
|
static _addTokenToDioHeader(String token) {
|
|
sl<Dio>().options.headers['Authorization'] = 'Bearer $token';
|
|
}
|
|
|
|
static haveTokenInStorage() async {
|
|
final box = await Hive.openBox('token');
|
|
final token = box.get('token');
|
|
if (token == null) {
|
|
return false;
|
|
} else {
|
|
_addTokenToDioHeader(token);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static Future<String> getTokenFromStorage() async => (await Hive.openBox('token')).get('token');
|
|
|
|
static saveTokenInStorage(String token) async {
|
|
(await Hive.openBox('token')).put('token', token);
|
|
_addTokenToDioHeader(token);
|
|
}
|
|
|
|
static removeTokenInStorage() async => (await Hive.openBox('token')).delete('token');
|
|
}
|