67 lines
2.0 KiB
Dart
67 lines
2.0 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
class DbHelper {
|
|
static Future<SharedPreferences> get _prefs async =>
|
|
SharedPreferences.getInstance();
|
|
|
|
static final _supabase = Supabase.instance.client;
|
|
|
|
static User? get currentUser => _supabase.auth.currentUser;
|
|
|
|
static Stream<AuthState> get authEventStream =>
|
|
_supabase.auth.onAuthStateChange;
|
|
|
|
static Future<void> init() async {
|
|
await Supabase.initialize(
|
|
url: 'https://vhmrtvhcmvylhrhblyjb.supabase.co',
|
|
anonKey:
|
|
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InZobXJ0dmhjbXZ5bGhyaGJseWpiIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTc4MTU3MTksImV4cCI6MjAxMzM5MTcxOX0.Djltae5oIvANqHdY1cNqA1ja5aAEivr-a_XDYkSd924',
|
|
);
|
|
}
|
|
|
|
static void login(String username) async {
|
|
if (username.length < 2) return;
|
|
final prefs = await _prefs;
|
|
|
|
try {
|
|
await _supabase.auth.signInWithPassword(
|
|
email: '$username@skup.in',
|
|
password: 'password',
|
|
);
|
|
await prefs.setString('username', username);
|
|
} on AuthException catch (e) {
|
|
await _supabase.auth.signUp(
|
|
email: '$username@skup.in',
|
|
password: 'password',
|
|
data: {'username': username});
|
|
await prefs.setString('username', username);
|
|
}
|
|
}
|
|
|
|
static Future<bool> autoLogin() async {
|
|
final prefs = await _prefs;
|
|
final String? username = prefs.getString('username');
|
|
if (username == null) return false;
|
|
try {
|
|
await _supabase.auth.signInWithPassword(
|
|
email: '$username@skup.in',
|
|
password: 'password',
|
|
);
|
|
return true;
|
|
} on AuthException catch (e) {
|
|
if (kDebugMode) {
|
|
print('Could\'t automaticall log in.\n${e.message}');
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static void logout() async {
|
|
final prefs = await _prefs;
|
|
bool success = await prefs.remove('username');
|
|
_supabase.auth.signOut();
|
|
}
|
|
}
|