mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
* Refactor login_page with a _showSnackBar function * Add an onError block to onAuthStateChange Errors get swallowed without this error handler, I noticed it when I had requested too many magic link emails, there was no feedback in the ui, nor in the logs in the console. * Fix SnackBars not being displayed and add a _showSnackBar function * Fix code samples for login_page.dart and account_page.dart to reflect fixes in #27104 * Fix isError not being passed for an Unexpected Error in login_page * Update flutter user management example to a more modern code * Update the guide to match the new content * run formatter * got rid of excess code and minor format fix --------- Co-authored-by: dshukertjr <dshukertjr@gmail.com>
174 lines
4.9 KiB
Dart
174 lines
4.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import 'package:supabase_quickstart/components/avatar.dart';
|
|
import 'package:supabase_quickstart/main.dart';
|
|
import 'package:supabase_quickstart/pages/login_page.dart';
|
|
|
|
class AccountPage extends StatefulWidget {
|
|
const AccountPage({super.key});
|
|
|
|
@override
|
|
State<AccountPage> createState() => _AccountPageState();
|
|
}
|
|
|
|
class _AccountPageState extends State<AccountPage> {
|
|
final _usernameController = TextEditingController();
|
|
final _websiteController = TextEditingController();
|
|
|
|
String? _avatarUrl;
|
|
var _loading = true;
|
|
|
|
/// Called once a user id is received within `onAuthenticated()`
|
|
Future<void> _getProfile() async {
|
|
setState(() {
|
|
_loading = true;
|
|
});
|
|
|
|
try {
|
|
final userId = supabase.auth.currentSession!.user.id;
|
|
final data =
|
|
await supabase.from('profiles').select().eq('id', userId).single();
|
|
_usernameController.text = (data['username'] ?? '') as String;
|
|
_websiteController.text = (data['website'] ?? '') as String;
|
|
_avatarUrl = (data['avatar_url'] ?? '') as String;
|
|
} on PostgrestException catch (error) {
|
|
if (mounted) context.showSnackBar(error.message, isError: true);
|
|
} catch (error) {
|
|
if (mounted) {
|
|
context.showSnackBar('Unexpected error occurred', isError: true);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Called when user taps `Update` button
|
|
Future<void> _updateProfile() async {
|
|
setState(() {
|
|
_loading = true;
|
|
});
|
|
final userName = _usernameController.text.trim();
|
|
final website = _websiteController.text.trim();
|
|
final user = supabase.auth.currentUser;
|
|
final updates = {
|
|
'id': user!.id,
|
|
'username': userName,
|
|
'website': website,
|
|
'updated_at': DateTime.now().toIso8601String(),
|
|
};
|
|
try {
|
|
await supabase.from('profiles').upsert(updates);
|
|
if (mounted) context.showSnackBar('Successfully updated profile!');
|
|
} on PostgrestException catch (error) {
|
|
if (mounted) context.showSnackBar(error.message, isError: true);
|
|
} catch (error) {
|
|
if (mounted) {
|
|
context.showSnackBar('Unexpected error occurred', isError: true);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _signOut() async {
|
|
try {
|
|
await supabase.auth.signOut();
|
|
} on AuthException catch (error) {
|
|
if (mounted) context.showSnackBar(error.message, isError: true);
|
|
} catch (error) {
|
|
if (mounted) {
|
|
context.showSnackBar('Unexpected error occurred', isError: true);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(builder: (_) => const LoginPage()),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Called when image has been uploaded to Supabase storage from within Avatar widget
|
|
Future<void> _onUpload(String imageUrl) async {
|
|
try {
|
|
final userId = supabase.auth.currentUser!.id;
|
|
await supabase.from('profiles').upsert({
|
|
'id': userId,
|
|
'avatar_url': imageUrl,
|
|
});
|
|
if (mounted) {
|
|
const SnackBar(
|
|
content: Text('Updated your profile image!'),
|
|
);
|
|
}
|
|
} on PostgrestException catch (error) {
|
|
if (mounted) context.showSnackBar(error.message, isError: true);
|
|
} catch (error) {
|
|
if (mounted) {
|
|
context.showSnackBar('Unexpected error occurred', isError: true);
|
|
}
|
|
}
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_avatarUrl = imageUrl;
|
|
});
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_getProfile();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_websiteController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Profile')),
|
|
body: ListView(
|
|
padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 12),
|
|
children: [
|
|
Avatar(
|
|
imageUrl: _avatarUrl,
|
|
onUpload: _onUpload,
|
|
),
|
|
const SizedBox(height: 18),
|
|
TextFormField(
|
|
controller: _usernameController,
|
|
decoration: const InputDecoration(labelText: 'User Name'),
|
|
),
|
|
const SizedBox(height: 18),
|
|
TextFormField(
|
|
controller: _websiteController,
|
|
decoration: const InputDecoration(labelText: 'Website'),
|
|
),
|
|
const SizedBox(height: 18),
|
|
ElevatedButton(
|
|
onPressed: _loading ? null : _updateProfile,
|
|
child: Text(_loading ? 'Saving...' : 'Update'),
|
|
),
|
|
const SizedBox(height: 18),
|
|
TextButton(onPressed: _signOut, child: const Text('Sign Out')),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|