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/recipe/recipe_edit_screen/models/recipe_edit_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';
@@ -29,16 +29,14 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
Emitter<RecipeEditState> 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 String? recipeID = state.recipeModelObj?.recipeID;
final response = await http.get(
Uri.parse(baseUrl + '/api/recipes/show/$recipeID'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
if (response.statusCode == 200) {
@@ -79,7 +77,7 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
emit(
RecipeEditLoadedState(
token: token,
token: await TokenStorage.getTokenFromStorage(),
recipe: data,
title: TextEditingController(text: data['title']),
description: TextEditingController(text: data['description']),
@@ -191,17 +189,14 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
}
_fetchTags(int id) async {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(baseUrl + '/api/recipes/getTags/$id'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
final data = jsonDecode(response.body);
@@ -210,9 +205,7 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
FutureOr<void> _reloadScreen(RecipeEditEvent? event, Emitter<RecipeEditState> emit) async {
print('Reloading..');
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
if (state is RecipeEditLoadedState) {

View File

@@ -2,12 +2,12 @@ import 'dart:async';
import 'package:dio/dio.dart';
import 'package:eatvio_app/app_router.dart';
import 'package:eatvio_app/core/token_storage.dart';
import 'package:eatvio_app/locator_service.dart';
import 'package:eatvio_app/presentation/recipe/recipe_select_screen/models/recipe_select_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:image_picker/image_picker.dart';
import '/core/app_export.dart';
@@ -26,9 +26,7 @@ class RecipeSelectBloc extends Bloc<RecipeSelectEvent, RecipeSelectState> {
Emitter<RecipeSelectState> 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 response = await sl<Dio>().get(baseUrl + '/api/recipes/index');

View File

@@ -2,12 +2,12 @@ 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/feed/blog_post_screen/models/comment_model.dart';
import 'package:eatvio_app/presentation/recipe/recipe_show_screen/models/recipe_show_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 'package:intl/intl.dart';
@@ -29,16 +29,14 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
Emitter<RecipeState> 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 String? recipeID = state.recipeModelObj?.recipeID;
final response = await http.get(
Uri.parse(baseUrl + '/api/recipes/show/$recipeID'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
if (response.statusCode == 200) {
@@ -60,7 +58,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
} else {
emit(
RecipeLoadedState(
token: token,
token: await TokenStorage.getTokenFromStorage(),
recipe: data,
steps: data['steps'],
author: await _fetchAuthor(data['id']),
@@ -85,16 +83,14 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
FutureOr<void> _deleteEntry(DeleteEntry event, Emitter<RecipeState> emit) async {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final int id = event.id;
final response = await http.post(
Uri.parse(baseUrl + '/api/recipes/delete/$id'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
if (response.statusCode == 200) {
@@ -122,17 +118,14 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
_fetchAuthor(int id) async {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(baseUrl + '/api/recipes/getAuthor/$id'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
final data = jsonDecode(response.body);
@@ -140,15 +133,13 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
Future<List> _fetchComments(int id) async {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(baseUrl + '/api/recipes/getComments/$id'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
final data = jsonDecode(response.body);
@@ -156,17 +147,14 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
_fetchTags(int id) async {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(baseUrl + '/api/recipes/getTags/$id'),
headers: {
'Authorization': 'Bearer $token',
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
final data = jsonDecode(response.body);