I'm trying to login to the Supabase using password:
final supabaseClient = SupabaseClient('supabaseUrl','supabaseKey');
final response = await supabaseClient.auth.signInWithPassword(email: 'example@gmail.com',password: 'password');
await Supabase.instance.client.auth.setSession(response.session!.refreshToken!);
when I press the 'login' button the 'signInWithPassword' recall multiple times until it end with this exception:
AuthException(message: Rate limit exceeded, statusCode: 429)
and the refresh token become invalid ... when I tried to store the session by "persistSessionString" like this using "SharedPreferences":
preferences.setString('persistSessionString',supabase.auth.currentSession!.persistSessionString);
and trying to recover the session after expired (after hot reload) :
final persistSessionString =preferences.getString('persistSessionString',) ??'?';
final res = await supabase.auth.recoverSession(persistSessionString);
it give me this error:
Invalid Refresh Token: Refresh Token Not Found...400
in the 'AndroidManifest' :
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with YOUR_SCHEME://YOUR_HOST -->
<data
android:scheme="io.supabase.flutterdomain"
android:host="login-callback" />
</intent-filter>
in the main function:
await Supabase.initialize(
url: 'I add it from Supabase dashboard',
anonKey:'I add it from Supabase dashboard',
authCallbackUrlHostname: 'login-callback',
);
I need to make users login just for the first time after downloading the app then save their sessions so they don't have to login every time they reopen the app ..
Im stuck here for a while and I coulden't find out where is the problem!
You can use the supabase_flutter package instead of supabase
package. supabase_flutter will persist the auth state just by initializing it like this with no additional configuration.
import 'package:supabase_flutter/supabase_flutter.dart';
void main() async {
await Supabase.initialize(
url: SUPABASE_URL,
anonKey: SUPABASE_ANON_KEY,
);
runApp(MyApp());
}
// It's handy to then extract the Supabase client in a variable for later uses
final supabase = Supabase.instance.client;
The problem with your code is that you are creating a new SupabaseClient
instance with the following code.
// Won't work. Should not use.
final supabaseClient = SupabaseClient('supabaseUrl','supabaseKey');
Creating a new Supabase instance will not persist the auth state. In order to persist the auth state, use the SupabaseClient
instance on the Supabase
object like this:
Will automatically handle all the auth stuff.
final supabase = Supabase.instance.client;