Commit 30a7d9c2 authored by Budi Prasetyo's avatar Budi Prasetyo

register with firebase

parent 523fc0a2
...@@ -6,15 +6,18 @@ import 'package:medapp_eksad/model/user_model.dart'; ...@@ -6,15 +6,18 @@ import 'package:medapp_eksad/model/user_model.dart';
var cmd = 'https://dmsdev-api.eksad.com/gateway/medapp/v1/cmd'; var cmd = 'https://dmsdev-api.eksad.com/gateway/medapp/v1/cmd';
var qry = 'https://dmsdev-api.eksad.com/gateway/medapp/v1/qry'; var qry = 'https://dmsdev-api.eksad.com/gateway/medapp/v1/qry';
Future<bool> signUp(nama, email, nohp, username, password)async{ Future<bool> signUp(namars,nors,alamatrs,namapic,nopic,email, password)async{
final response = await http.post( final response = await http.post(
Uri.parse('$cmd/user/signup'), Uri.parse('$cmd/user/signup'),
body: jsonEncode({ body: jsonEncode({
"namaUser": nama, "namaRs": namars,
"emailUser": email, "noRs": nors,
"noHp": nohp, "alamatRs": alamatrs,
"username": username, "namaPic": namapic,
"password": password}), "noPic": nopic,
"emailPic": email,
"password": password
}),
headers: { headers: {
'Content-type' : 'application/json; charset=UTF-8', 'Content-type' : 'application/json; charset=UTF-8',
} }
......
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:medapp_eksad/firebase/showOtpDialog.dart';
import 'package:medapp_eksad/firebase/showSnackbar.dart';
class FirebaseAuthMethods {
final FirebaseAuth _auth;
FirebaseAuthMethods(this._auth);
// FOR EVERY FUNCTION HERE
// POP THE ROUTE USING: Navigator.of(context).pushNamedAndRemoveUntil('/', (Route<dynamic> route) => false);
// GET USER DATA
// using null check operator since this method should be called only
// when the user is logged in
User get user => _auth.currentUser!;
// STATE PERSISTENCE STREAM
Stream<User?> get authState => FirebaseAuth.instance.authStateChanges();
// OTHER WAYS (depends on use case):
// Stream get authState => FirebaseAuth.instance.userChanges();
// Stream get authState => FirebaseAuth.instance.idTokenChanges();
// KNOW MORE ABOUT THEM HERE: https://firebase.flutter.dev/docs/auth/start#auth-state
// EMAIL SIGN UP
Future<void> signUpWithEmail({
required String nameRS,
required String phoneRS,
required String addressRS,
required String namePIC,
required String phonePIC,
required String addressPIC,
required String email,
required String password,
required BuildContext context,
}) async {
try {
await _auth.createUserWithEmailAndPassword(
// nameRS: nameRS,
// phoneRS: phoneRS,
// addressRS: addressRS,
// namePIC: namePIC,
// phonePIC: phonePIC,
// addressPIC: addressPIC,
email: email,
password: password,
);
await sendEmailVerification(context);
} on FirebaseAuthException catch (e) {
// if you want to display your own custom error message
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
showSnackBar(
context, e.message!); // Displaying the usual firebase error message
}
}
// EMAIL LOGIN
Future<void> loginWithEmail({
// required String nameRS,
// required String phoneRS,
// required String addressRS,
// required String namePIC,
// required String phonePIC,
// required String addressPIC,
required String email,
required String password,
required BuildContext context,
}) async {
try {
await _auth.signInWithEmailAndPassword(
// nameRS: nameRS,
// phoneRS: phoneRS,
// addressRS: addressRS,
// namePIC: namePIC,
// phonePIC: phonePIC,
// addressPIC: addressPIC,
email: email,
password: password,
);
if (!user.emailVerified) {
await sendEmailVerification(context);
// restrict access to certain things using provider
// transition to another page instead of home screen
}
} on FirebaseAuthException catch (e) {
showSnackBar(context, e.message!); // Displaying the error message
}
}
// EMAIL VERIFICATION
Future<void> sendEmailVerification(BuildContext context) async {
try {
_auth.currentUser!.sendEmailVerification();
showSnackBar(context, 'Email verification sent!');
} on FirebaseAuthException catch (e) {
showSnackBar(context, e.message!); // Display error message
}
}
// GOOGLE SIGN IN
// Future<void> signInWithGoogle(BuildContext context) async {
// try {
// if (kIsWeb) {
// GoogleAuthProvider googleProvider = GoogleAuthProvider();
//
// googleProvider
// .addScope('https://www.googleapis.com/auth/contacts.readonly');
//
// await _auth.signInWithPopup(googleProvider);
// } else {
// final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
//
// final GoogleSignInAuthentication? googleAuth =
// await googleUser?.authentication;
//
// if (googleAuth?.accessToken != null && googleAuth?.idToken != null) {
// // Create a new credential
// final credential = GoogleAuthProvider.credential(
// accessToken: googleAuth?.accessToken,
// idToken: googleAuth?.idToken,
// );
// UserCredential userCredential =
// await _auth.signInWithCredential(credential);
//
// // if you want to do specific task like storing information in firestore
// // only for new users using google sign in (since there are no two options
// // for google sign in and google sign up, only one as of now),
// // do the following:
//
// // if (userCredential.user != null) {
// // if (userCredential.additionalUserInfo!.isNewUser) {}
// // }
// }
// }
// } on FirebaseAuthException catch (e) {
// showSnackBar(context, e.message!); // Displaying the error message
// }
// }
// ANONYMOUS SIGN IN
Future<void> signInAnonymously(BuildContext context) async {
try {
await _auth.signInAnonymously();
} on FirebaseAuthException catch (e) {
showSnackBar(context, e.message!); // Displaying the error message
}
}
// FACEBOOK SIGN IN
// Future<void> signInWithFacebook(BuildContext context) async {
// try {
// final LoginResult loginResult = await FacebookAuth.instance.login();
//
// final OAuthCredential facebookAuthCredential =
// FacebookAuthProvider.credential(loginResult.accessToken!.token);
//
// await _auth.signInWithCredential(facebookAuthCredential);
// } on FirebaseAuthException catch (e) {
// showSnackBar(context, e.message!); // Displaying the error message
// }
// }
// PHONE SIGN IN
Future<void> phoneSignIn(
BuildContext context,
String phoneNumber,
) async {
TextEditingController codeController = TextEditingController();
if (kIsWeb) {
// !!! Works only on web !!!
ConfirmationResult result =
await _auth.signInWithPhoneNumber(phoneNumber);
// Diplay Dialog Box To accept OTP
showOTPDialog(
codeController: codeController,
context: context,
onPressed: () async {
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: result.verificationId,
smsCode: codeController.text.trim(),
);
await _auth.signInWithCredential(credential);
Navigator.of(context).pop(); // Remove the dialog box
},
);
} else {
// FOR ANDROID, IOS
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
// Automatic handling of the SMS code
verificationCompleted: (PhoneAuthCredential credential) async {
// !!! works only on android !!!
await _auth.signInWithCredential(credential);
},
// Displays a message when verification fails
verificationFailed: (e) {
showSnackBar(context, e.message!);
},
// Displays a dialog box when OTP is sent
codeSent: ((String verificationId, int? resendToken) async {
showOTPDialog(
codeController: codeController,
context: context,
onPressed: () async {
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: codeController.text.trim(),
);
// !!! Works only on Android, iOS !!!
await _auth.signInWithCredential(credential);
Navigator.of(context).pop(); // Remove the dialog box
},
);
}),
codeAutoRetrievalTimeout: (String verificationId) {
// Auto-resolution timed out...
},
);
}
}
// SIGN OUT
Future<void> signOut(BuildContext context) async {
try {
await _auth.signOut();
} on FirebaseAuthException catch (e) {
showSnackBar(context, e.message!); // Displaying the error message
}
}
// DELETE ACCOUNT
Future<void> deleteAccount(BuildContext context) async {
try {
await _auth.currentUser!.delete();
} on FirebaseAuthException catch (e) {
showSnackBar(context, e.message!); // Displaying the error message
// if an error of requires-recent-login is thrown, make sure to log
// in user again and then delete account.
}
}
}
import 'package:flutter/material.dart';
void showOTPDialog({
required BuildContext context,
required TextEditingController codeController,
required VoidCallback onPressed,
}) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: const Text("Enter OTP"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: codeController,
),
],
),
actions: <Widget>[
TextButton(
child: const Text("Done"),
onPressed: onPressed,
)
],
),
);
}
import 'package:flutter/material.dart';
void showSnackBar(BuildContext context, String text) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(text),
),
);
}
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:medapp_eksad/auth/forget_password.dart'; import 'package:medapp_eksad/auth/forget_password.dart';
import 'package:medapp_eksad/dashboard/admin/main_dashboard_admin.dart'; import 'package:medapp_eksad/dashboard/admin/main_dashboard_admin.dart';
import 'package:medapp_eksad/firebase/firebase_auth_methods.dart';
import 'package:medapp_eksad/firebase_options.dart';
import 'package:medapp_eksad/homepage.dart'; import 'package:medapp_eksad/homepage.dart';
import 'package:medapp_eksad/login.dart'; import 'package:medapp_eksad/login.dart';
import 'package:medapp_eksad/register.dart'; import 'package:medapp_eksad/register.dart';
...@@ -8,8 +12,13 @@ import 'package:medapp_eksad/register_pic.dart'; ...@@ -8,8 +12,13 @@ import 'package:medapp_eksad/register_pic.dart';
import 'package:medapp_eksad/screen/contact_us/contact_us.dart'; import 'package:medapp_eksad/screen/contact_us/contact_us.dart';
import 'package:medapp_eksad/screen/solution.dart'; import 'package:medapp_eksad/screen/solution.dart';
import 'package:medapp_eksad/screen_user/demo_user.dart'; import 'package:medapp_eksad/screen_user/demo_user.dart';
import 'package:provider/provider.dart';
void main() { void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp()); runApp(const MyApp());
} }
...@@ -18,7 +27,17 @@ class MyApp extends StatelessWidget { ...@@ -18,7 +27,17 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MultiProvider(
providers: [
Provider<FirebaseAuthMethods>(
create: (_) => FirebaseAuthMethods(FirebaseAuth.instance),
),
StreamProvider(
create: (context) => context.read<FirebaseAuthMethods>().authState,
initialData: null,
),
],
child: MaterialApp(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
title: "MedApp by Eksad", title: "MedApp by Eksad",
initialRoute: '/', initialRoute: '/',
...@@ -28,12 +47,14 @@ class MyApp extends StatelessWidget { ...@@ -28,12 +47,14 @@ class MyApp extends StatelessWidget {
'/solutions': (context) => const Solutions(), '/solutions': (context) => const Solutions(),
'/contact': (context) => const ContactUs(), '/contact': (context) => const ContactUs(),
'/login': (context) => const SignIn(), '/login': (context) => const SignIn(),
'/register': (context) => const RegisterPic(), //'/register': (context) => const RegisterPic(),
'/register': (context) => const Register(),
'/dashboard': (context) => const DashboardAdmin(), '/dashboard': (context) => const DashboardAdmin(),
'/reset_password': (context) => const ForgotPassword(), '/reset_password': (context) => const ForgotPassword(),
'/demo': (context) => const DemoUser(), '/demo': (context) => const DemoUser(),
//'/register_pic': (context) => const RegisterPic(), //'/register_pic': (context) => const RegisterPic(),
}, },
),
); );
} }
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment