refactor(token): add token storage, remove hive

This commit is contained in:
mso7nueh
2024-08-26 15:04:07 +03:00
parent 603728549d
commit cea81abd21
29 changed files with 98 additions and 271 deletions

View File

@@ -2,11 +2,11 @@ import 'dart:async';
import 'dart:convert';
import 'package:eatvio_app/app_router.dart';
import 'package:eatvio_app/core/token_storage.dart';
import 'package:eatvio_app/presentation/glossary/glossary_screen/models/glossary_screen_model.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart' as http;
import '/core/app_export.dart';
@@ -26,15 +26,13 @@ class GlossaryBloc extends Bloc<GlossaryEvent, GlossaryState> {
Emitter<GlossaryState> emit,
) async {
try {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final responseProductGroups = await http.get(
Uri.parse(baseUrl + '/api/glossaries/group/index'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
if (responseProductGroups.statusCode == 200) {
@@ -54,12 +52,12 @@ class GlossaryBloc extends Bloc<GlossaryEvent, GlossaryState> {
List<SelectionPopupModel> productGroupsModel = _fillProductGroups(dataProductGroups);
emit(
GlossaryLoadedState(
token: token,
token: await TokenStorage.getTokenFromStorage(),
productGroups: productGroupsModel,
productList: [],
),
);
await _getProductListById(token, emit, productGroupsModel[0].id);
await _getProductListById(await TokenStorage.getTokenFromStorage(), emit, productGroupsModel[0].id);
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку