fix: handle login
This commit is contained in:
@@ -1,117 +0,0 @@
|
||||
/// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
/// *****************************************************
|
||||
/// FlutterGen
|
||||
/// *****************************************************
|
||||
|
||||
// coverage:ignore-file
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class $AssetsImagesGen {
|
||||
const $AssetsImagesGen();
|
||||
|
||||
/// File path: assets/images/immich-logo.png
|
||||
AssetGenImage get immichLogo =>
|
||||
const AssetGenImage('assets/images/immich-logo.png');
|
||||
|
||||
/// File path: assets/images/immich-text-dark.png
|
||||
AssetGenImage get immichTextDark =>
|
||||
const AssetGenImage('assets/images/immich-text-dark.png');
|
||||
|
||||
/// File path: assets/images/immich-text-light.png
|
||||
AssetGenImage get immichTextLight =>
|
||||
const AssetGenImage('assets/images/immich-text-light.png');
|
||||
|
||||
/// List of all assets
|
||||
List<AssetGenImage> get values =>
|
||||
[immichLogo, immichTextDark, immichTextLight];
|
||||
}
|
||||
|
||||
class Assets {
|
||||
Assets._();
|
||||
|
||||
static const $AssetsImagesGen images = $AssetsImagesGen();
|
||||
}
|
||||
|
||||
class AssetGenImage {
|
||||
const AssetGenImage(
|
||||
this._assetName, {
|
||||
this.size,
|
||||
this.flavors = const {},
|
||||
});
|
||||
|
||||
final String _assetName;
|
||||
|
||||
final Size? size;
|
||||
final Set<String> flavors;
|
||||
|
||||
Image image({
|
||||
Key? key,
|
||||
AssetBundle? bundle,
|
||||
ImageFrameBuilder? frameBuilder,
|
||||
ImageErrorWidgetBuilder? errorBuilder,
|
||||
String? semanticLabel,
|
||||
bool excludeFromSemantics = false,
|
||||
double? scale,
|
||||
double? width,
|
||||
double? height,
|
||||
Color? color,
|
||||
Animation<double>? opacity,
|
||||
BlendMode? colorBlendMode,
|
||||
BoxFit? fit,
|
||||
AlignmentGeometry alignment = Alignment.center,
|
||||
ImageRepeat repeat = ImageRepeat.noRepeat,
|
||||
Rect? centerSlice,
|
||||
bool matchTextDirection = false,
|
||||
bool gaplessPlayback = false,
|
||||
bool isAntiAlias = false,
|
||||
String? package,
|
||||
FilterQuality filterQuality = FilterQuality.low,
|
||||
int? cacheWidth,
|
||||
int? cacheHeight,
|
||||
}) {
|
||||
return Image.asset(
|
||||
_assetName,
|
||||
key: key,
|
||||
bundle: bundle,
|
||||
frameBuilder: frameBuilder,
|
||||
errorBuilder: errorBuilder,
|
||||
semanticLabel: semanticLabel,
|
||||
excludeFromSemantics: excludeFromSemantics,
|
||||
scale: scale,
|
||||
width: width,
|
||||
height: height,
|
||||
color: color,
|
||||
opacity: opacity,
|
||||
colorBlendMode: colorBlendMode,
|
||||
fit: fit,
|
||||
alignment: alignment,
|
||||
repeat: repeat,
|
||||
centerSlice: centerSlice,
|
||||
matchTextDirection: matchTextDirection,
|
||||
gaplessPlayback: gaplessPlayback,
|
||||
isAntiAlias: isAntiAlias,
|
||||
package: package,
|
||||
filterQuality: filterQuality,
|
||||
cacheWidth: cacheWidth,
|
||||
cacheHeight: cacheHeight,
|
||||
);
|
||||
}
|
||||
|
||||
ImageProvider provider({
|
||||
AssetBundle? bundle,
|
||||
String? package,
|
||||
}) {
|
||||
return AssetImage(
|
||||
_assetName,
|
||||
bundle: bundle,
|
||||
package: package,
|
||||
);
|
||||
}
|
||||
|
||||
String get path => _assetName;
|
||||
|
||||
String get keyName => _assetName;
|
||||
}
|
||||
@@ -3,5 +3,11 @@ import 'package:flutter/material.dart';
|
||||
/// Log messages stored in the DB
|
||||
const int kLogMessageLimit = 500;
|
||||
|
||||
/// Headers
|
||||
// Auth header
|
||||
const String kImmichHeaderAuthKey = "x-immich-user-token";
|
||||
const String kImmichHeaderDeviceModel = "deviceModel";
|
||||
const String kImmichHeaderDeviceType = "deviceType";
|
||||
|
||||
/// Global ScaffoldMessengerKey to show snackbars
|
||||
final GlobalKey<ScaffoldMessengerState> kScafMessengerKey = GlobalKey();
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/store.interface.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/presentation/router/router.dart';
|
||||
import 'package:immich_mobile/service_locator.dart';
|
||||
import 'package:immich_mobile/utils/constants/globals.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log_context.mixin.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class ImmichApiClient extends ApiClient with LogContext {
|
||||
ImmichApiClient({required String endpoint}) : super(basePath: endpoint);
|
||||
|
||||
Future<void> init({String? accessToken}) async {
|
||||
final token =
|
||||
accessToken ?? (await di<IStoreRepository>().get(StoreKey.accessToken));
|
||||
|
||||
if (token != null) {
|
||||
addDefaultHeader(kImmichHeaderAuthKey, token);
|
||||
}
|
||||
|
||||
final deviceInfo = DeviceInfoPlugin();
|
||||
final String deviceModel;
|
||||
if (Platform.isIOS) {
|
||||
deviceModel = (await deviceInfo.iosInfo).utsname.machine;
|
||||
} else {
|
||||
deviceModel = (await deviceInfo.androidInfo).model;
|
||||
}
|
||||
|
||||
addDefaultHeader(kImmichHeaderDeviceModel, deviceModel);
|
||||
addDefaultHeader(kImmichHeaderDeviceType, Platform.operatingSystem);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> invokeAPI(
|
||||
String path,
|
||||
String method,
|
||||
List<QueryParam> queryParams,
|
||||
Object? body,
|
||||
Map<String, String> headerParams,
|
||||
Map<String, String> formParams,
|
||||
String? contentType,
|
||||
) async {
|
||||
final res = await super.invokeAPI(
|
||||
path,
|
||||
method,
|
||||
queryParams,
|
||||
body,
|
||||
headerParams,
|
||||
formParams,
|
||||
contentType,
|
||||
);
|
||||
|
||||
if (res.statusCode == HttpStatus.unauthorized) {
|
||||
log.severe("Token invalid. Redirecting to login route");
|
||||
await di<AppRouter>().replaceAll([const LoginRoute()]);
|
||||
throw ApiException(res.statusCode, "Unauthorized");
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// ignore: avoid-dynamic
|
||||
static dynamic _patchDto(dynamic value, String targetType) {
|
||||
switch (targetType) {
|
||||
case 'UserPreferencesResponseDto':
|
||||
if (value is Map) {
|
||||
if (value['rating'] == null) {
|
||||
value['rating'] = RatingResponse().toJson();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ignore: avoid-dynamic
|
||||
static dynamic fromJson(
|
||||
// ignore: avoid-dynamic
|
||||
dynamic value,
|
||||
String targetType, {
|
||||
bool growable = false,
|
||||
}) {
|
||||
_patchDto(value, targetType);
|
||||
return ApiClient.fromJson(value, targetType, growable: growable);
|
||||
}
|
||||
|
||||
UsersApi getUsersApi() => UsersApi(this);
|
||||
ServerApi getServerApi() => ServerApi(this);
|
||||
AuthenticationApi getAuthenticationApi() => AuthenticationApi(this);
|
||||
OAuthApi getOAuthApi() => OAuthApi(this);
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:immich_mobile/presentation/router/router.dart';
|
||||
import 'package:immich_mobile/service_locator.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log_context.mixin.dart';
|
||||
|
||||
class ImmichAuthInterceptor extends Interceptor with LogContext {
|
||||
String? _accessToken;
|
||||
|
||||
void setAccessToken(String token) => _accessToken = token;
|
||||
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
if (_accessToken != null) {
|
||||
options.headers["x-immich-user-token"] = _accessToken;
|
||||
}
|
||||
|
||||
handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||
if (response.statusCode == 401) {
|
||||
log.severe("Token expired. Logging user out");
|
||||
di<AppRouter>().replaceAll([const LoginRoute()]);
|
||||
return;
|
||||
}
|
||||
handler.next(response);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import 'package:logging/logging.dart';
|
||||
class LogManager {
|
||||
LogManager._();
|
||||
static final LogManager _instance = LogManager._();
|
||||
|
||||
// ignore: match-getter-setter-field-names
|
||||
static LogManager get I => _instance;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user