Skip to content

Commit

Permalink
fix: 401 received when using member token
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Aug 28, 2024
1 parent a8e8ba2 commit 1a1df5b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 34 deletions.
7 changes: 6 additions & 1 deletion lib/controllers/anime_weekly_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AnimeWeeklyController {
await init();
}

Future<void> nextPage() async {
Future<void> nextPage({bool isRetry = false}) async {
if (isLoading) {
return;
}
Expand All @@ -52,6 +52,11 @@ class AnimeWeeklyController {
} catch (exception, stackTrace) {
debugPrint(exception.toString());
debugPrint(stackTrace.toString());

if (!isRetry) {
await MemberController.instance.login();
await nextPage(isRetry: true);
}
} finally {
isLoading = false;
}
Expand Down
88 changes: 75 additions & 13 deletions lib/controllers/member_controller.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:application/dtos/anime_dto.dart';
import 'package:application/dtos/episode_mapping_dto.dart';
Expand Down Expand Up @@ -56,7 +57,7 @@ class MemberController {
Future<String> register() async {
final response = await HttpRequest().post('/v1/members/register');

if (response.statusCode != 201) {
if (response.statusCode != HttpStatus.created) {
throw const HttpException('Failed to register');
}

Expand All @@ -70,11 +71,11 @@ class MemberController {
final response =
await HttpRequest().post('/v1/members/login', body: identifier);

if (response.statusCode == 404) {
if (response.statusCode == HttpStatus.notFound) {
throw const HttpException('Failed to login, identifier not found');
}

if (response.statusCode != 200) {
if (response.statusCode != HttpStatus.ok) {
throw ClientException('Server error');
}

Expand Down Expand Up @@ -140,6 +141,27 @@ class MemberController {
);
}

Future<void> updateImage(Uint8List image) async {
final response = await HttpRequest().postMultipart(
'/v1/members/image',
member!.token,
image,
);

if (response.statusCode == HttpStatus.unauthorized) {
await login();
return updateImage(image);
}

if (response.statusCode != HttpStatus.ok) {
throw HttpException(
'Failed to change image ${response.body}',
);
}

await increaseImageVersion();
}

Future<void> increaseImageVersion() async {
imageVersion++;
await _sharedPreferences.setInt('imageVersion', imageVersion);
Expand All @@ -156,11 +178,16 @@ class MemberController {
body: email,
);

if (response.statusCode == 409) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return associateEmail(email);
}

if (response.statusCode == HttpStatus.conflict) {
throw const ConflictEmailException();
}

if (response.statusCode != 201) {
if (response.statusCode != HttpStatus.created) {
throw const HttpException('Failed to associate email');
}

Expand All @@ -174,11 +201,16 @@ class MemberController {
body: email,
);

if (response.statusCode == 409) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return forgotIdentifier(email);
}

if (response.statusCode == HttpStatus.conflict) {
throw const ConflictEmailException();
}

if (response.statusCode != 201) {
if (response.statusCode != HttpStatus.created) {
throw const HttpException('Failed to associate email');
}

Expand All @@ -192,7 +224,12 @@ class MemberController {
body: code,
);

if (response.statusCode != 200) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return validateAction(uuid, code);
}

if (response.statusCode != HttpStatus.ok) {
throw const HttpException('Failed to validate action');
}
}
Expand All @@ -204,7 +241,12 @@ class MemberController {
jsonEncode({'uuid': anime.uuid}),
);

if (response.statusCode != 200) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return followAnime(anime);
}

if (response.statusCode != HttpStatus.ok) {
throw const HttpException('Failed to follow anime');
}

Expand All @@ -219,7 +261,12 @@ class MemberController {
jsonEncode({'uuid': anime.uuid}),
);

if (response.statusCode != 200) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return unfollowAnime(anime);
}

if (response.statusCode != HttpStatus.ok) {
throw const HttpException('Failed to unfollow anime');
}

Expand All @@ -238,7 +285,12 @@ class MemberController {
jsonEncode({'uuid': anime.uuid}),
);

if (response.statusCode != 200) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return followAllEpisodes(anime);
}

if (response.statusCode != HttpStatus.ok) {
throw const HttpException('Failed to follow all episodes');
}

Expand All @@ -263,7 +315,12 @@ class MemberController {
jsonEncode({'uuid': episode.uuid}),
);

if (response.statusCode != 200) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return followEpisode(episode);
}

if (response.statusCode != HttpStatus.ok) {
throw const HttpException('Failed to follow episode');
}

Expand All @@ -280,7 +337,12 @@ class MemberController {
jsonEncode({'uuid': episode.uuid}),
);

if (response.statusCode != 200) {
if (response.statusCode == HttpStatus.unauthorized) {
await login();
return unfollowEpisode(episode);
}

if (response.statusCode != HttpStatus.ok) {
throw const HttpException('Failed to unfollow episode');
}

Expand Down
7 changes: 6 additions & 1 deletion lib/controllers/missed_anime_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MissedAnimeController {
await init();
}

Future<void> loadPage({bool emit = true}) async {
Future<void> loadPage({bool emit = true, bool isRetry = false}) async {
if (isLoading) {
return;
}
Expand All @@ -87,6 +87,11 @@ class MissedAnimeController {
canLoadMore = missedAnimes.length < pageableDto.total;
} catch (e) {
debugPrint(e.toString());

if (!isRetry) {
await MemberController.instance.login();
await loadPage(emit: emit, isRetry: true);
}
} finally {
isLoading = false;
page++;
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/http_request.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';

import 'package:application/dtos/pageable_dto.dart';
import 'package:application/utils/constant.dart';
Expand All @@ -25,7 +26,7 @@ class HttpRequest {
)
.timeout(_timeout);

if (response.statusCode != 200) {
if (response.statusCode != HttpStatus.ok) {
throw Exception('Failed to load data');
}

Expand Down
19 changes: 1 addition & 18 deletions lib/views/crop_view.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:application/controllers/member_controller.dart';
import 'package:application/utils/http_request.dart';
import 'package:crop_your_image/crop_your_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down Expand Up @@ -35,22 +33,7 @@ class CropView extends StatelessWidget {
baseColor: Colors.black,
onCropped: (value) {
Navigator.of(context).pop();

HttpRequest()
.postMultipart(
'/v1/members/image',
MemberController.instance.member!.token,
value,
)
.then((response) {
if (response.statusCode != 200) {
throw HttpException(
'Failed to change image ${response.body}',
);
}

MemberController.instance.increaseImageVersion();
});
MemberController.instance.updateImage(value);
},
),
),
Expand Down

0 comments on commit 1a1df5b

Please sign in to comment.