forked from eatvio/eatvio_app
feat(chat): fix token storage
This commit is contained in:
@@ -12,7 +12,6 @@ class InitializationCubit extends Cubit<InitializationState> {
|
||||
runInitialization() async {
|
||||
try {
|
||||
await TokenStorage.haveTokenInStorage();
|
||||
|
||||
final wsUrl = await chatRepository.getWebsocket();
|
||||
|
||||
PusherOptions options = PusherOptions(
|
||||
@@ -22,12 +21,12 @@ class InitializationCubit extends Cubit<InitializationState> {
|
||||
auth: PusherAuth(
|
||||
'wss://' + wsUrl.host + '/app/' + wsUrl.key,
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${TokenStorage.getTokenFromStorage()}',
|
||||
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
PusherClient pusherClient = PusherClient(
|
||||
PusherClient pusherClient = await PusherClient(
|
||||
wsUrl.key,
|
||||
options,
|
||||
autoConnect: true,
|
||||
@@ -37,15 +36,6 @@ class InitializationCubit extends Cubit<InitializationState> {
|
||||
await pusherClient.connect();
|
||||
|
||||
emit(const InitializationState.success());
|
||||
|
||||
Channel channel = pusherClient.subscribe('private-direct-chat.' + '7909ea56-7822-4d5f-8892-134bc937127f');
|
||||
|
||||
channel.bind("pusher_internal:subscription_succeeded", (PusherEvent? event) {
|
||||
channel.trigger("pusher_internal:subscription_succeeded", {});
|
||||
});
|
||||
|
||||
channel.registerListener('pusher:subscribe', (data) => print(data.toString()));
|
||||
|
||||
} catch (e) {
|
||||
print(e);
|
||||
emit(InitializationState.error(errorMessage: e.toString()));
|
||||
|
||||
@@ -3,7 +3,7 @@ import 'package:eatvio_app/locator_service.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
|
||||
abstract class TokenStorage {
|
||||
static addTokenToDioHeader(String token) {
|
||||
static _addTokenToDioHeader(String token) {
|
||||
sl<Dio>().options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
|
||||
@@ -13,20 +13,17 @@ abstract class TokenStorage {
|
||||
if (token == null) {
|
||||
return false;
|
||||
} else {
|
||||
addTokenToDioHeader(token);
|
||||
_addTokenToDioHeader(token);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static getTokenFromStorage() async => await Hive.openBox('token')
|
||||
..get('token');
|
||||
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);
|
||||
(await Hive.openBox('token')).put('token', token);
|
||||
_addTokenToDioHeader(token);
|
||||
}
|
||||
|
||||
static removeTokenInStorage(String token) async => await Hive.openBox('token')
|
||||
..delete('token');
|
||||
static removeTokenInStorage() async => (await Hive.openBox('token')).delete('token');
|
||||
}
|
||||
|
||||
@@ -60,9 +60,7 @@ dependencies:
|
||||
# http request
|
||||
dio: ^5.4.3+1
|
||||
# websocket
|
||||
pusher_client:
|
||||
git:
|
||||
url: https://github.com/bihim/pusher_client
|
||||
pusher_client: ^2.0.0
|
||||
# photo gallery viewer + dismissible page
|
||||
photo_view: ^0.15.0
|
||||
dismissible_page: ^1.0.2
|
||||
|
||||
Reference in New Issue
Block a user