Skip to content

Commit 94611b3

Browse files
authored
Merge pull request #2 from Rnbsov/supabase-auth
πŸ‹ Supabase auth
2 parents 23ae98d + 4670574 commit 94611b3

5 files changed

Lines changed: 514 additions & 69 deletions

File tree

β€Ždevtools_options.yamlβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: This file stores settings for Dart & Flutter DevTools.
2+
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
3+
extensions:

β€Žlib/main.dartβ€Ž

Lines changed: 100 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import 'package:flutter/material.dart';
2+
import 'package:supabase_auth_ui/supabase_auth_ui.dart';
3+
24
import 'util.dart';
35
import 'theme.dart';
46

5-
void main() {
7+
void main() async {
8+
await Supabase.initialize(
9+
url: 'https://jbpeszoljhnymgqhufmd.supabase.co',
10+
anonKey:
11+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpicGVzem9samhueW1ncWh1Zm1kIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDMyNTAzMDIsImV4cCI6MjA1ODgyNjMwMn0.NX3h_DpXqnpabbla3wR-RHQTuZskM3aJeX2qU1HNUe0',
12+
);
613
runApp(const MyApp());
714
}
815

@@ -51,70 +58,115 @@ class MyHomePage extends StatefulWidget {
5158
State<MyHomePage> createState() => _MyHomePageState();
5259
}
5360

54-
class _MyHomePageState extends State<MyHomePage> {
55-
int _counter = 0;
56-
57-
void _incrementCounter() {
58-
setState(() {
59-
// This call to setState tells the Flutter framework that something has
60-
// changed in this State, which causes it to rerun the build method below
61-
// so that the display can reflect the updated values. If we changed
62-
// _counter without calling setState(), then the build method would not be
63-
// called again, and so nothing would appear to happen.
64-
_counter++;
65-
});
61+
class _MyHomePageState extends State<MyHomePage>
62+
with SingleTickerProviderStateMixin {
63+
@override
64+
Widget build(BuildContext context) {
65+
return Scaffold(
66+
appBar: AppBar(
67+
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
68+
title: Text(widget.title),
69+
),
70+
body: Center(
71+
child: Column(
72+
mainAxisAlignment: MainAxisAlignment.center,
73+
children: [
74+
SupaEmailAuth(
75+
onSignInComplete: (response) {
76+
final session = response.session;
77+
if (session != null && session.user.email != null) {
78+
Navigator.of(context).pushReplacement(
79+
MaterialPageRoute(
80+
builder:
81+
(context) =>
82+
WelcomeScreen(email: session.user.email!),
83+
),
84+
);
85+
}
86+
},
87+
onSignUpComplete: (response) {
88+
final session = response.session;
89+
if (session != null && session.user.email != null) {
90+
Navigator.of(context).pushReplacement(
91+
MaterialPageRoute(
92+
builder:
93+
(context) =>
94+
WelcomeScreen(email: session.user.email!),
95+
),
96+
);
97+
}
98+
},
99+
),
100+
SupaSocialsAuth(
101+
socialProviders: [OAuthProvider.google],
102+
colored: true,
103+
onSuccess: (session) {
104+
if (session != null && session.user.email != null) {
105+
Navigator.of(context).pushReplacement(
106+
MaterialPageRoute(
107+
builder:
108+
(context) =>
109+
WelcomeScreen(email: session.user.email!),
110+
),
111+
);
112+
}
113+
},
114+
),
115+
],
116+
),
117+
),
118+
);
119+
}
120+
}
121+
122+
// Add this class at the end of your file
123+
124+
class WelcomeScreen extends StatelessWidget {
125+
final String email;
126+
127+
const WelcomeScreen({super.key, required this.email});
128+
129+
// Extract username from email (everything before @)
130+
String get username {
131+
return email.split('@')[0];
66132
}
67133

68134
@override
69135
Widget build(BuildContext context) {
70-
// This method is rerun every time setState is called, for instance as done
71-
// by the _incrementCounter method above.
72-
//
73-
// The Flutter framework has been optimized to make rerunning build methods
74-
// fast, so that you can just rebuild anything that needs updating rather
75-
// than having to individually change instances of widgets.
76136
return Scaffold(
77137
appBar: AppBar(
78-
// TRY THIS: Try changing the color here to a specific color (to
79-
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
80-
// change color while the other colors stay the same.
81138
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
82-
// Here we take the value from the MyHomePage object that was created by
83-
// the App.build method, and use it to set our appbar title.
84-
title: Text(widget.title),
139+
title: const Text('Welcome'),
140+
actions: [
141+
IconButton(
142+
icon: const Icon(Icons.logout),
143+
onPressed: () async {
144+
await Supabase.instance.client.auth.signOut();
145+
if (context.mounted) {
146+
Navigator.of(context).pushAndRemoveUntil(
147+
MaterialPageRoute(
148+
builder:
149+
(context) =>
150+
const MyHomePage(title: 'Flutter Demo Home Page'),
151+
),
152+
(route) => false,
153+
);
154+
}
155+
},
156+
),
157+
],
85158
),
86159
body: Center(
87-
// Center is a layout widget. It takes a single child and positions it
88-
// in the middle of the parent.
89160
child: Column(
90-
// Column is also a layout widget. It takes a list of children and
91-
// arranges them vertically. By default, it sizes itself to fit its
92-
// children horizontally, and tries to be as tall as its parent.
93-
//
94-
// Column has various properties to control how it sizes itself and
95-
// how it positions its children. Here we use mainAxisAlignment to
96-
// center the children vertically; the main axis here is the vertical
97-
// axis because Columns are vertical (the cross axis would be
98-
// horizontal).
99-
//
100-
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
101-
// action in the IDE, or press "p" in the console), to see the
102-
// wireframe for each widget.
103161
mainAxisAlignment: MainAxisAlignment.center,
104-
children: <Widget>[
105-
const Text('You have pushed the button this many times:'),
162+
children: [
106163
Text(
107-
'$_counter',
164+
'πŸ‘‹ Hello ${username}',
108165
style: Theme.of(context).textTheme.headlineMedium,
109166
),
110167
],
111168
),
112169
),
113-
floatingActionButton: FloatingActionButton(
114-
onPressed: _incrementCounter,
115-
tooltip: 'Increment',
116-
child: const Icon(Icons.add),
117-
), // This trailing comma makes auto-formatting nicer for build methods.
118170
);
119171
}
120172
}

β€Žlib/theme.dartβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ class MaterialTheme {
344344
bodyColor: colorScheme.onSurface,
345345
displayColor: colorScheme.onSurface,
346346
),
347-
scaffoldBackgroundColor: colorScheme.background,
347+
scaffoldBackgroundColor: colorScheme.surface,
348348
canvasColor: colorScheme.surface,
349349
);
350350

0 commit comments

Comments
Β (0)