Working auth and added shared notes
This commit is contained in:
+109
-62
@@ -2,58 +2,104 @@ import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:helpers/helpers/print.dart';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import 'package:rluv/global/store.dart';
|
||||
|
||||
class Api {
|
||||
static final Api _instance = Api._internal();
|
||||
import '../models/token.dart';
|
||||
|
||||
factory Api() {
|
||||
if (_instance.initDone) return _instance;
|
||||
|
||||
_instance.initDone = true;
|
||||
_instance.dio = Dio();
|
||||
_instance.dio.options.baseUrl = "http://localhost:8081/";
|
||||
// _instance.dio.options.baseUrl = "https://rluv.fosscat.com/";
|
||||
_instance.dio.interceptors.add(
|
||||
LoggingInterceptor(),
|
||||
// InterceptorsWrapper(
|
||||
// onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
|
||||
// // Do something before request is sent.
|
||||
// // If you want to resolve the request with custom data,
|
||||
// // you can resolve a `Response` using `handler.resolve(response)`.
|
||||
// // If you want to reject the request with a error message,
|
||||
// // you can reject with a `DioException` using `handler.reject(dioError)`.
|
||||
// return handler.next(options);
|
||||
// },
|
||||
// onResponse: (Response response, ResponseInterceptorHandler handler) {
|
||||
// if (response.statusCode != null &&
|
||||
// response.statusCode! < 500 &&
|
||||
// response.statusCode! >= 400) {
|
||||
// return handler.reject(DioException.badResponse(
|
||||
// requestOptions: RequestOptions(),
|
||||
// response: response,
|
||||
// statusCode: response.statusCode!));
|
||||
// }
|
||||
// // Do something with response data.
|
||||
// // If you want to reject the request with a error message,
|
||||
// // you can reject a `DioException` object using `handler.reject(dioError)`.
|
||||
// return handler.next(response);
|
||||
// },
|
||||
// onError: (DioException e, ErrorInterceptorHandler handler) {
|
||||
// printPink(e);
|
||||
// // Do something with response error.
|
||||
// // If you want to resolve the request with some custom data,
|
||||
// // you can resolve a `Response` object using `handler.resolve(response)`.
|
||||
// return handler.next(e);
|
||||
// },
|
||||
// ),
|
||||
);
|
||||
return _instance;
|
||||
final tokenProvider = StateProvider<Token?>((ref) {
|
||||
final jwt = ref.watch(jwtProvider);
|
||||
printLime('Current token: $jwt');
|
||||
if (jwt == null) return null;
|
||||
try {
|
||||
return Token.fromJson(JwtDecoder.decode(jwt));
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
Api._internal();
|
||||
});
|
||||
|
||||
bool initDone = false;
|
||||
late final Dio dio;
|
||||
final jwtProvider = StateNotifierProvider<_JwtNotifier, String?>((ref) {
|
||||
final prefs = ref.watch(prefsProvider);
|
||||
final jwt = prefs?.getString('jwt');
|
||||
return _JwtNotifier(ref, jwt);
|
||||
});
|
||||
|
||||
class _JwtNotifier extends StateNotifier<String?> {
|
||||
_JwtNotifier(this.ref, String? jwt) : super(null) {
|
||||
if (jwt != null) {
|
||||
setToken(jwt);
|
||||
}
|
||||
}
|
||||
|
||||
final StateNotifierProviderRef ref;
|
||||
|
||||
void setToken(String jwt) {
|
||||
state = jwt;
|
||||
printCyan('Loaded jwt into client: $jwt');
|
||||
ref.read(prefsProvider)?.setString('jwt', jwt);
|
||||
}
|
||||
|
||||
void revokeToken() {
|
||||
printCyan('jwt token revoked');
|
||||
state = null;
|
||||
ref.read(prefsProvider)?.remove('jwt');
|
||||
}
|
||||
}
|
||||
|
||||
final apiProvider = StateNotifierProvider<_ApiNotifier, Dio>((ref) {
|
||||
return _ApiNotifier(ref, Dio());
|
||||
});
|
||||
|
||||
class _ApiNotifier extends StateNotifier<Dio> {
|
||||
_ApiNotifier(this.ref, this.dio) : super(dio) {
|
||||
// dio.options.baseUrl = "https://fe7d-136-36-2-234.ngrok-free.app";
|
||||
// dio.options.baseUrl = "http://localhost:8081/";
|
||||
dio.options.baseUrl = "https://rluv.fosscat.com/";
|
||||
dio.interceptors.addAll([
|
||||
InterceptorsWrapper(onRequest:
|
||||
(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
final jwt = ref.read(jwtProvider);
|
||||
if (jwt != null) {
|
||||
options.headers['token'] = jwt;
|
||||
}
|
||||
return handler.next(options);
|
||||
}, onResponse: (Response response, ResponseInterceptorHandler handler) {
|
||||
if (response.statusCode != null) {
|
||||
if (response.statusCode == 200) {
|
||||
try {
|
||||
if ((response.data as Map<String, dynamic>)
|
||||
.containsKey('success')) {
|
||||
if (!response.data['success']) return handler.next(response);
|
||||
}
|
||||
if ((response.data as Map<String, dynamic>)
|
||||
.containsKey('token')) {
|
||||
final jwt = response.data['token'];
|
||||
if (jwt != null) {
|
||||
ref.read(jwtProvider.notifier).setToken(jwt);
|
||||
// ref.read(tokenProvider.notifier).state =
|
||||
// Token.fromJson(jwtData);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
printRed('Error in interceptor for token: $err');
|
||||
return handler.next(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
return handler.next(response);
|
||||
}, onError: (DioException err, ErrorInterceptorHandler handler) {
|
||||
if (err.response?.statusCode == 403) {
|
||||
ref.read(jwtProvider.notifier).revokeToken();
|
||||
}
|
||||
}),
|
||||
_LoggingInterceptor(),
|
||||
]);
|
||||
}
|
||||
|
||||
final Dio dio;
|
||||
final StateNotifierProviderRef ref;
|
||||
|
||||
Future<Map<String, dynamic>?> get(String path) async {
|
||||
try {
|
||||
@@ -64,7 +110,6 @@ class Api {
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
printRed('Error in get: $err');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -79,7 +124,6 @@ class Api {
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
printRed('Error in put: $err');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -94,7 +138,6 @@ class Api {
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
printRed('Error in put: $err');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -109,14 +152,13 @@ class Api {
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
printRed('Error in delete: $err');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LoggingInterceptor extends Interceptor {
|
||||
LoggingInterceptor();
|
||||
class _LoggingInterceptor extends Interceptor {
|
||||
_LoggingInterceptor();
|
||||
|
||||
@override
|
||||
Future onRequest(
|
||||
@@ -134,7 +176,7 @@ class LoggingInterceptor extends Interceptor {
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
logPrint('///*** ERROR RESPONSE ***\\\\\\');
|
||||
printRed('///*** ERROR RESPONSE ***\\\\\\');
|
||||
logPrint('URI: ${err.requestOptions.uri}');
|
||||
if (err.response != null) {
|
||||
logPrint('STATUS CODE: ${err.response?.statusCode?.toString()}');
|
||||
@@ -167,15 +209,20 @@ class LoggingInterceptor extends Interceptor {
|
||||
}
|
||||
}
|
||||
|
||||
void printJson(Map<String, dynamic>? s) {
|
||||
if (kDebugMode) {
|
||||
if (s == null) {
|
||||
printAmber({});
|
||||
return;
|
||||
void printJson(dynamic s) {
|
||||
try {
|
||||
final data = (s as Map<String, dynamic>?);
|
||||
if (kDebugMode) {
|
||||
if (data == null) {
|
||||
printAmber({});
|
||||
return;
|
||||
}
|
||||
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
|
||||
String prettyprint = encoder.convert(s);
|
||||
printAmber(prettyprint);
|
||||
}
|
||||
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
|
||||
String prettyprint = encoder.convert(s);
|
||||
printAmber(prettyprint);
|
||||
} catch (_) {
|
||||
printAmber(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user