step-2
This commit is contained in:
parent
6559c6721d
commit
60c52f0ab7
|
@ -2,6 +2,9 @@ import 'package:firebase_auth/firebase_auth.dart';
|
|||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mynotes/firebase_options.dart';
|
||||
import 'package:mynotes/views/login_view.dart';
|
||||
import 'package:mynotes/views/register_view.dart';
|
||||
import 'package:mynotes/views/verify_email_view.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
@ -12,6 +15,10 @@ void main() {
|
|||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const HomePage(),
|
||||
routes: {
|
||||
'/login/': (context) => const LoginView(),
|
||||
'/register/': (context) => const RegisterView(),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -21,29 +28,28 @@ class HomePage extends StatelessWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Home'),
|
||||
return FutureBuilder(
|
||||
future: Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
),
|
||||
builder: (context, snapshot) {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.done:
|
||||
final user = FirebaseAuth.instance.currentUser;
|
||||
if (user?.emailVerified ?? false) {
|
||||
print('You are a verified user');
|
||||
builder: (context, snapshot) {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.done:
|
||||
final user = FirebaseAuth.instance.currentUser;
|
||||
if (user != null) {
|
||||
if (user.emailVerified) {
|
||||
print('Email is verified');
|
||||
} else {
|
||||
print('You need to verify your email first');
|
||||
return const VerifyEmailView();
|
||||
}
|
||||
return const Text('Done');
|
||||
default:
|
||||
return const Text('Loading...');
|
||||
}
|
||||
},
|
||||
),
|
||||
} else {
|
||||
return const LoginView();
|
||||
}
|
||||
return const Text('Done');
|
||||
default:
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mynotes/firebase_options.dart';
|
||||
|
||||
class LoginView extends StatefulWidget {
|
||||
const LoginView({Key? key}) : super(key: key);
|
||||
|
@ -34,60 +32,57 @@ class _LoginViewState extends State<LoginView> {
|
|||
appBar: AppBar(
|
||||
title: const Text('Login'),
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
),
|
||||
builder: (context, snapshot) {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.done:
|
||||
return Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: _email,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your email here',
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
controller: _password,
|
||||
obscureText: true,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your password here',
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final email = _email.text;
|
||||
final password = _password.text;
|
||||
try {
|
||||
final userCredential = await FirebaseAuth.instance
|
||||
.signInWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
print(userCredential);
|
||||
} on FirebaseAuthException catch (e) {
|
||||
if (e.code == 'user-not-found') {
|
||||
print('User not found');
|
||||
} else if (e.code == 'wrong-password') {
|
||||
print('Wrong password');
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Login'),
|
||||
),
|
||||
],
|
||||
body: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: _email,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your email here',
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
controller: _password,
|
||||
obscureText: true,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your password here',
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final email = _email.text;
|
||||
final password = _password.text;
|
||||
try {
|
||||
final userCredential =
|
||||
await FirebaseAuth.instance.signInWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
print(userCredential);
|
||||
} on FirebaseAuthException catch (e) {
|
||||
if (e.code == 'user-not-found') {
|
||||
print('User not found');
|
||||
} else if (e.code == 'wrong-password') {
|
||||
print('Wrong password');
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Login'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
'/register/',
|
||||
(route) => false,
|
||||
);
|
||||
default:
|
||||
return const Text('Loading...');
|
||||
}
|
||||
},
|
||||
},
|
||||
child: const Text('Not registered yet? Register here!'),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -34,62 +34,59 @@ class _RegisterViewState extends State<RegisterView> {
|
|||
appBar: AppBar(
|
||||
title: const Text('Register'),
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
),
|
||||
builder: (context, snapshot) {
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.done:
|
||||
return Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: _email,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your email here',
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
controller: _password,
|
||||
obscureText: true,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your password here',
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final email = _email.text;
|
||||
final password = _password.text;
|
||||
try {
|
||||
final userCredential = await FirebaseAuth.instance
|
||||
.createUserWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
print(userCredential);
|
||||
} on FirebaseAuthException catch (e) {
|
||||
if (e.code == 'weak-password') {
|
||||
print('Weak password');
|
||||
} else if (e.code == 'email-already-in-use') {
|
||||
print('Email is already in use');
|
||||
} else if (e.code == 'invalid-email') {
|
||||
print('invalid email entered');
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Register'),
|
||||
),
|
||||
],
|
||||
body: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: _email,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your email here',
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
controller: _password,
|
||||
obscureText: true,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Enter your password here',
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final email = _email.text;
|
||||
final password = _password.text;
|
||||
try {
|
||||
final userCredential =
|
||||
await FirebaseAuth.instance.createUserWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
print(userCredential);
|
||||
} on FirebaseAuthException catch (e) {
|
||||
if (e.code == 'weak-password') {
|
||||
print('Weak password');
|
||||
} else if (e.code == 'email-already-in-use') {
|
||||
print('Email is already in use');
|
||||
} else if (e.code == 'invalid-email') {
|
||||
print('invalid email entered');
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Register'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
'/login/',
|
||||
(route) => false,
|
||||
);
|
||||
default:
|
||||
return const Text('Loading...');
|
||||
}
|
||||
},
|
||||
},
|
||||
child: const Text('Already registered? Login here!'),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class VerifyEmailView extends StatefulWidget {
|
||||
const VerifyEmailView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_VerifyEmailViewState createState() => _VerifyEmailViewState();
|
||||
}
|
||||
|
||||
class _VerifyEmailViewState extends State<VerifyEmailView> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Verify email'),
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
const Text('Please verify your email address:'),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final user = FirebaseAuth.instance.currentUser;
|
||||
await user?.sendEmailVerification();
|
||||
},
|
||||
child: const Text('Send email verification'),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue