英文:
Supabase Flutter Web OAuth Redirect Issue
问题
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:spacevault_cash/constants.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:supabase/supabase.dart' as supabase_;
import '../services/supabase_service.dart';
class TestPage extends ConsumerWidget {
const TestPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final user = ref.watch(userProvider);
print(supabase.auth.currentSession);
// Use the user variable here
return ElevatedButton(
onPressed: () {
ref.read(supabaseProvider).auth.signInWithOAuth(
supabase_.Provider.github,
redirectTo: kIsWeb ? null : authRedirectUrl);
},
child: Text(user != null ? 'Logged in' : 'Login'),
);
}
}
I'm having trouble authenticating users when using the web client. When clicking on the button, the user gets redirected to GitHub. After entering valid credentials, the users is sent back to the application; however, supabase.auth.currentSession
prints null
. However, I can see the access_token in the URL bar, so it should actually have everything it needs. Any Ideas?
Just for context: This works fine for iOS and Android.
英文:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:spacevault_cash/constants.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:supabase/supabase.dart' as supabase_;
import '../services/supabase_service.dart';
class TestPage extends ConsumerWidget {
const TestPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final user = ref.watch(userProvider);
print(supabase.auth.currentSession);
// Use the user variable here
return ElevatedButton(
onPressed: () {
ref.read(supabaseProvider).auth.signInWithOAuth(
supabase_.Provider.github,
redirectTo: kIsWeb ? null : authRedirectUrl);
},
child: Text(user != null ? 'Logged in' : 'Login'),
);
}
}
I'm having trouble authenticating users when using the web client. When clicking on the button, the user gets redirected to GitHub. After entering valid credentials, the users is sent back to the application, however supabase.auth.currentSession
prints null
.
However, I can see the access_token in the URL bar, so it should actually have everything it needs. Any Ideas?
Just for context: This works fine for iOS and Android
答案1
得分: 1
对于 Web 身份验证,重要的是不设置 authCallbackUrlHostname
。 如果你使用 authCallbackUrlHostname
初始化 Supabase 客户端,仅仅将 null
用作 redirectTo
是不够的。
我不得不从以下代码中删除了 authCallbackUrlHostname
。
await Supabase.initialize(
url: supabaseURL,
anonKey: supabaseAnonKey,
authCallbackUrlHostname: 'login-callback-url',
debug: true,
);
实际上,我编写了两种不同的设置,通过查看运行平台来导入。
import 'configure_nonweb.dart' if (dart.library.html) 'configure_web.dart';
英文:
For web authentication, it is important to not set authCallbackUrlHostname
.
Using null
in as the redirectTo
is not enough if you initialize the supabase client with the authCallbackUrlHostname
.
I had to remove the authCallbackUrlHostname
from the following code.
await Supabase.initialize(
url: supabaseURL,
anonKey: supabaseAnonKey,
authCallbackUrlHostname: 'login-callback-url',
debug: true,
);
I actually wrote two different setups which I import by looking at what platform it is run on.
import 'configure_nonweb.dart' if (dart.library.html) 'configure_web.dart';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论