Autologin implemented
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:maggs_victory_voyage/services/db_helper.dart';
|
||||
|
||||
import 'pages/login_page.dart';
|
||||
import 'pages/splash_page.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await DbHelper.init();
|
||||
runApp(const Application());
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:maggs_victory_voyage/services/db_helper.dart';
|
||||
|
||||
import 'login_page.dart';
|
||||
|
||||
@@ -10,8 +11,38 @@ class SplashPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _SplashPageState extends State<SplashPage> {
|
||||
late final Future<bool> loginFuture;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
loginFuture = DbHelper.autoLogin();
|
||||
DbHelper.authEventStream.listen((event) => setState(() {}));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: FutureBuilder(
|
||||
future: loginFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
if (DbHelper.currentUser == null) {
|
||||
return const LoginPage();
|
||||
}
|
||||
return const Column(
|
||||
children: [
|
||||
Text('Logged in!'),
|
||||
TextButton(
|
||||
onPressed: DbHelper.logout,
|
||||
child: Text('Log out'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
@@ -7,6 +8,11 @@ class DbHelper {
|
||||
|
||||
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',
|
||||
@@ -17,22 +23,43 @@ class DbHelper {
|
||||
|
||||
static void login(String username) async {
|
||||
final prefs = await _prefs;
|
||||
final String? savedUsername = prefs.getString('username');
|
||||
|
||||
if (savedUsername == null) {
|
||||
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);
|
||||
// TODO create new account
|
||||
} else if (savedUsername == username) {
|
||||
// TODO log into database
|
||||
} else {
|
||||
// TODO wrong 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');
|
||||
|
||||
//TODO log out on database
|
||||
_supabase.auth.signOut();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user