Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Firebase Phone Authentication #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions android/app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"project_info": {
"project_number": "1096226607310",
"project_id": "phoneauth-e6e57",
"storage_bucket": "phoneauth-e6e57.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1096226607310:android:f782257ba8f1049677009f",
"android_client_info": {
"package_name": "com.example.newauth"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyDSayGhkQLs51khA68J_2nApv1pPLJXfLk"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:1096226607310:android:fb2cde750c45486377009f",
"android_client_info": {
"package_name": "com.rrdhoi.event_app"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyDSayGhkQLs51khA68J_2nApv1pPLJXfLk"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
}
],
"configuration_version": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Generated file.
//
// If you wish to remove Flutter's multidex support, delete this entire file.
//
// Modifications to this file should be done in a copy under a different name
// as this file may be regenerated.

package io.flutter.app;

import android.app.Application;
import android.content.Context;
import androidx.annotation.CallSuper;
import androidx.multidex.MultiDex;

/**
* Extension of {@link android.app.Application}, adding multidex support.
*/
public class FlutterMultiDexApplication extends Application {
@Override
@CallSuper
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.6.10'
ext.kotlin_version = '1.7.10'
repositories {
google()
mavenCentral()
Expand Down
Binary file added assets/images/login.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions lib/auth/otpscreen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'dart:developer';

import 'package:event_app/ui/pages/home_page.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:event_app/auth/phoneauth.dart';

class OTPScreen extends StatefulWidget {
final String verificationid;
OTPScreen({super.key, required this.verificationid});

@override
State<OTPScreen> createState() => _OTPScreenState();
}

class _OTPScreenState extends State<OTPScreen> {
TextEditingController otpController = TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("OTPScreen"),
centerTitle: true,
),
body: Column(children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: TextField(
controller: otpController,
keyboardType: TextInputType.phone,
decoration: InputDecoration(
hintText: "Enter OTP",
suffixIcon: Icon(Icons.phone),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30))),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
try {
PhoneAuthCredential credential =
await PhoneAuthProvider.credential(
verificationId: widget.verificationid,
smsCode: otpController.text.toString());
FirebaseAuth.instance
.signInWithCredential(credential)
.then((value) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => HomePage()));
}).catchError((error) {
// Authentication failed, handle the error
print("Authentication failed: $error");
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => PhoneAuth(),
),
);
// You can show an error message to the user or handle the error in any other way
});
} catch (e) {
print(e.toString());
log(e.toString());
}
},
child: Text("OTP"))
]),
);
}
}
181 changes: 181 additions & 0 deletions lib/auth/phoneauth.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import 'package:country_picker/country_picker.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:event_app/auth/otpscreen.dart';
import 'package:event_app/ui/widgets/custom_button.dart';

class PhoneAuth extends StatefulWidget {
const PhoneAuth({super.key});

@override
State<PhoneAuth> createState() => _PhoneAuthState();
}

class _PhoneAuthState extends State<PhoneAuth> {
TextEditingController phoneController = TextEditingController();
Country selectedCountry = Country(
phoneCode: "91",
countryCode: "IN",
e164Sc: 0,
geographic: true,
level: 1,
name: "India",
example: "India",
displayName: "India",
displayNameNoCountryCode: "IN",
e164Key: "",
);

@override
Widget build(BuildContext context) {
phoneController.selection = TextSelection.fromPosition(
TextPosition(
offset: phoneController.text.length,
),
);
return Scaffold(
body: SingleChildScrollView(
child: SafeArea(
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 25, horizontal: 35),
child: Column(
children: [
Container(
width: 200,
height: 200,
padding: const EdgeInsets.all(20.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.purple.shade50,
),
child: Image.asset(
"assets/images/login.jpg",
),
),
const SizedBox(height: 20),
const Text(
"Register",
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
const Text(
"Add your phone number. We'll send you a verification code",
style: TextStyle(
fontSize: 14,
color: Colors.black38,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
TextFormField(
keyboardType: TextInputType.number,
cursorColor: Colors.purple,
controller: phoneController,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
onChanged: (value) {
setState(() {
phoneController.text = value;
});
},
decoration: InputDecoration(
hintText: "Enter phone number",
hintStyle: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 15,
color: Colors.grey.shade600,
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.black12),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.black12),
),
prefixIcon: Container(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
showCountryPicker(
context: context,
countryListTheme: const CountryListThemeData(
bottomSheetHeight: 550,
),
onSelect: (value) {
setState(() {
selectedCountry = value;
});
});
},
child: Text(
"${selectedCountry.flagEmoji} + ${selectedCountry.phoneCode}",
style: const TextStyle(
fontSize: 18,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
),
suffixIcon: phoneController.text.length > 9
? Container(
height: 30,
width: 30,
margin: const EdgeInsets.all(10.0),
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.green,
),
child: const Icon(
Icons.done,
color: Colors.white,
size: 20,
),
)
: null,
),
),
CustomButton(
text: "Verify Phone Number",
onPressed: () async {
String phoneNumber =
"+${selectedCountry.phoneCode}${phoneController.text.toString()}";
await FirebaseAuth.instance.verifyPhoneNumber(
verificationCompleted: (PhoneAuthCredential credential) {
print("Verified Successfully");
},
verificationFailed: (FirebaseAuthException ex) {
print("Error verifying OTP: ${ex.message}");


},
codeSent: (String verificationid, int? resendToken) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OTPScreen(
verificationid: verificationid,
)));
},
codeAutoRetrievalTimeout: (String verificationid) {},
phoneNumber: phoneNumber,
);
},
)
],
),
),
),
),
),
);
}
}
Loading
Loading