Skip to content

Commit

Permalink
fix: android web auth error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinhandoko committed Jun 23, 2024
1 parent 885c338 commit 129ade7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 11 deletions.
15 changes: 15 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ migration:
- platform: android
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: ios
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: linux
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: macos
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: web
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: windows
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1

# User provided section

Expand Down
20 changes: 20 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.linusu.flutter_web_auth_2.CallbackActivity"
android:exported="true">
<intent-filter android:label="flutter_web_auth_2">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="myapp" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>


<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package com.kelvin.spotify_clone

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity()
class MainActivity : FlutterActivity() {}
4 changes: 4 additions & 0 deletions lib/config/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class AppRouter {
}),
],
redirect: (BuildContext context, GoRouterState state) async {
if (state.matchedLocation == "/auth") {
return MyHomePage.routeName;
}

if (!await SpotifyService.isUserAuthenticated()) {
return LoginScreen.routeName;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/services/spotify.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'dart:io';

import 'package:flutter_web_auth/flutter_web_auth.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
import 'package:spotify/spotify.dart';
import 'package:spotify_clone/constants/credentials.dart';
import 'package:spotify_clone/model/playlist_extension.dart';
import 'package:url_launcher/url_launcher.dart';

final SpotifyApiCredentials _credentials =
SpotifyApiCredentials(Credentials.clientId, Credentials.clientSecret);
Expand Down Expand Up @@ -36,14 +34,19 @@ class SpotifyService {

static Future<void> authenticate() async {
try {
final res = await FlutterWebAuth.authenticate(
url: _authUri.toString(), callbackUrlScheme: "myapp");
final res = await FlutterWebAuth2.authenticate(
url: _authUri.toString(),
callbackUrlScheme: "myapp",
options: const FlutterWebAuth2Options(
silentAuth: true,
));

final credential = await _spotifyApi.getCredentials();
if (credential.fullyQualified) return;

_spotifyApi = SpotifyApi.fromAuthCodeGrant(_grant, res.toString());
} catch (e) {
print(e);
throw Exception("Failed to launch");
}
}
Expand Down
24 changes: 20 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,22 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_auth:
flutter_web_auth_2:
dependency: "direct main"
description:
name: flutter_web_auth
sha256: a69fa8f43b9e4d86ac72176bf747b735e7b977dd7cf215076d95b87cb05affdd
name: flutter_web_auth_2
sha256: "4d3d2fd3d26bf1a26b3beafd4b4b899c0ffe10dc99af25abc58ffe24e991133c"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "3.1.2"
flutter_web_auth_2_platform_interface:
dependency: transitive
description:
name: flutter_web_auth_2_platform_interface
sha256: e8669e262005a8354389ba2971f0fc1c36188481234ff50d013aaf993f30f739
url: "https://pub.dev"
source: hosted
version: "3.1.0"
flutter_web_plugins:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -853,6 +861,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.5.1"
window_to_front:
dependency: transitive
description:
name: window_to_front
sha256: "7aef379752b7190c10479e12b5fd7c0b9d92adc96817d9e96c59937929512aee"
url: "https://pub.dev"
source: hosted
version: "0.0.3"
xdg_directories:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dependencies:
loading_animation_widget: ^1.2.1
font_awesome_flutter: ^10.7.0
url_launcher: ^6.3.0
flutter_web_auth: ^0.5.0
flutter_web_auth_2: ^3.1.2



Expand Down

0 comments on commit 129ade7

Please sign in to comment.