按照this教程,在谷歌登录后,我被重定向到Google.com,而不是带我回到我的应用程序.我怎么才能解决这个问题呢?
登录页面的示例代码:
class _LoginScreenState extends State<LoginScreen> {
final supabase = Supabase.instance.client;
@override
void initState() {
_setupAuthListener();
super.initState();
}
void _setupAuthListener() {
supabase.auth.onAuthStateChange.listen((data) {
final event = data.event;
if (event == AuthChangeEvent.signedIn) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const ProfileScreen(),
),
);
}
});
}
/// Function to generate a random 16 character string.
String _generateRandomString() {
final random = Random.secure();
return base64Url.encode(List<int>.generate(16, (_) => random.nextInt(256)));
}
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () async {
const appAuth = FlutterAppAuth();
final rawNonce = _generateRandomString();
final hashedNonce =
sha256.convert(utf8.encode(rawNonce)).toString();
final clientId =
'MYCLIENTID';
/// Set as reversed DNS form of Google Client ID + `:/` for Google login
final redirectUrl = '${clientId.split('.').reversed.join('.')}:/';
const discoveryUrl =
'https://accounts.google.com/.well-known/openid-configuration';
// authorize the user by opening the concent page
final result = await appAuth.authorize(
AuthorizationRequest(
clientId,
redirectUrl,
discoveryUrl: discoveryUrl,
nonce: hashedNonce,
scopes: [
'openid',
'email',
'profile',
],
),
);
if (result == null) {
throw 'No result';
}
// Request the access and id token to google
final tokenResult = await appAuth.token(
TokenRequest(
clientId,
redirectUrl,
authorizationCode: result.authorizationCode,
discoveryUrl: discoveryUrl,
codeVerifier: result.codeVerifier,
nonce: result.nonce,
scopes: [
'openid',
'email',
],
),
);
final idToken = tokenResult?.idToken;
if (idToken == null) {
throw 'No idToken';
}
await supabase.auth.signInWithIdToken(
provider: Provider.google,
idToken: idToken,
nonce: rawNonce,
);
},
child: const Text('Google login'),
),
);
}
}
Build.gradle:
defaultConfig {
applicationId "com.user.appname"
minSdkVersion 33
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
manifestPlaceholders += [
'appAuthRedirectScheme': applicationId
]
}
代码与上面链接的Supabase文档中的代码相同.希望找到解决这个问题的办法.如果您有任何问题,请随时留言