Supabase Flutter Web OAuth 重定向问题

huangapple go评论58阅读模式
英文:

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';

huangapple
  • 本文由 发表于 2023年5月21日 05:31:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定