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

add user singup and singin verifications with firebase #3

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MyApp extends StatelessWidget {
theme: ThemeData.dark()
.copyWith(scaffoldBackgroundColor: mobileSearchColor),

home: const SingUpPage(),
home: const LoginPage(),
);
}
}
19 changes: 19 additions & 0 deletions lib/pages/homepage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';

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

@override
State<Homepage> createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text("this is homepage"),
),
);
}
}
23 changes: 20 additions & 3 deletions lib/pages/login_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:instagram_clone/service/authentication.dart';
import 'package:instagram_clone/util/colors.dart';
import 'package:instagram_clone/util/text_styles.dart';
import 'package:instagram_clone/util/variabals.dart';
Expand All @@ -15,6 +16,9 @@ class LoginPage extends StatefulWidget {
class _LoginPageState extends State<LoginPage> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final Authentication _authentication = Authentication();
bool isLoadding = false;

@override
void dispose() {
// TODO: implement dispose
Expand All @@ -23,6 +27,19 @@ class _LoginPageState extends State<LoginPage> {
super.dispose();
}

void SingInUserAndToggleHome() async {
setState(() {
isLoadding = !isLoadding;
});
await _authentication.singInUser(
email: _emailController.text,
password: _passwordController.text,
context: context);
setState(() {
isLoadding = !isLoadding;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -70,8 +87,8 @@ class _LoginPageState extends State<LoginPage> {
height: 40,
),
//login button
GestureDetector(
onTap: () {},
InkWell(
onTap: SingInUserAndToggleHome,
child: Container(
width: double.infinity,
height: MediaQuery.of(context).size.width * 0.13,
Expand All @@ -84,7 +101,7 @@ class _LoginPageState extends State<LoginPage> {
child: Center(
child: Text(
"Log In",
style: title.copyWith(color: mobileBackgroundColor),
style: title.copyWith(color: primaryColor),
),
),
),
Expand Down
83 changes: 59 additions & 24 deletions lib/pages/sing_up_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:image_picker/image_picker.dart';
import 'package:instagram_clone/service/authentication.dart';
import 'package:instagram_clone/service/media.dart';
import 'package:instagram_clone/service/storage.dart';
import 'package:instagram_clone/util/colors.dart';
import 'package:instagram_clone/util/text_styles.dart';
import 'package:instagram_clone/util/variabals.dart';
Expand All @@ -18,7 +23,10 @@ class _SingUpPageState extends State<SingUpPage> {
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _bioController = TextEditingController();

final Authentication _authServises = Authentication();
final MediaAdder _adder = MediaAdder();
Uint8List? _image;
bool isLoading = false;
@override
void dispose() {
// TODO: implement dispose
Expand All @@ -29,7 +37,32 @@ class _SingUpPageState extends State<SingUpPage> {
super.dispose();
}

Authentication _authServises = Authentication();
void imageSelector() async {
Uint8List img = await _adder.imagePicker(ImageSource.gallery);
setState(() {
_image = img;
});
}

//sing up user:store pro pic and other data
singUpUser() async {
setState(() {
isLoading = !isLoading;
});
String url = await StorageServices()
.uploadImagesToStorage("profile pics", _image!, false);
await _authServises.singUpUser(
email: _emailController.text,
password: _passwordController.text,
username: _usernameController.text,
bio: _bioController.text,
proPic: url,
context: context);
setState(() {
isLoading = !isLoading;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -54,16 +87,20 @@ class _SingUpPageState extends State<SingUpPage> {
//add profile pic
Stack(
children: [
const CircleAvatar(
radius: 65,
backgroundImage: AssetImage(
"assets/WhatsApp Image 2024-06-03 at 16.37.25_f9a84384.jpg"),
),
_image != null
? CircleAvatar(
radius: 65,
backgroundImage: MemoryImage(_image!),
)
: const CircleAvatar(
radius: 65,
backgroundColor: textboxfillcolor,
),
Positioned(
bottom: 0,
right: 0,
child: IconButton(
onPressed: () {},
onPressed: imageSelector,
icon: const Icon(
Icons.add_a_photo,
size: 28,
Expand All @@ -73,7 +110,7 @@ class _SingUpPageState extends State<SingUpPage> {
)
],
),
SizedBox(
const SizedBox(
height: 15,
),
//log in form
Expand Down Expand Up @@ -124,15 +161,8 @@ class _SingUpPageState extends State<SingUpPage> {
height: 40,
),
//login button
GestureDetector(
onTap: () async {
_authServises.singUpUser(
email: _emailController.text,
password: _passwordController.text,
username: _usernameController.text,
bio: _bioController.text,
);
},
InkWell(
onTap: singUpUser,
child: Container(
width: double.infinity,
height: MediaQuery.of(context).size.width * 0.13,
Expand All @@ -142,12 +172,17 @@ class _SingUpPageState extends State<SingUpPage> {
borderRadius: BorderRadius.circular(5),
),
),
child: Center(
child: Text(
"Sing Up",
style: title.copyWith(color: primaryColor),
),
),
child: !isLoading
? Center(
child: Text(
"Sing Up",
style: title.copyWith(color: primaryColor),
),
)
: const Center(
child: CircularProgressIndicator(
color: primaryColor,
)),
),
),
],
Expand Down
68 changes: 59 additions & 9 deletions lib/service/authentication.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@


import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:instagram_clone/pages/homepage.dart';
import 'package:instagram_clone/util/colors.dart';
import 'package:instagram_clone/util/text_styles.dart';

//add firebase auth methods to sing up and sing in users
class Authentication {
//instances
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestorege = FirebaseFirestore.instance;
Future<String> singUpUser(

void massage(String res, BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: textboxfillcolor,
content: Text(
res,
style: body,
),
),
);
}

Future<void> singUpUser(
{required String email,
required String password,
required String username,
required String bio,
// required Uint8List imgfile
}) async {
String proPic = "",
required BuildContext context}) async {
try {
//create a new user
if (email.isNotEmpty &&
Expand All @@ -24,22 +40,56 @@ class Authentication {
.createUserWithEmailAndPassword(email: email, password: password);
//get created user id
final user = userCredential.user!.uid;

// String url = await _storageServices.uploadImagesToStorage(
// "profile pics", imgfile, false);
//store user data
await _firestorege.collection("users").doc(user).set({
"uid": user,
"username": username,
"email": email,
"password": password,
"bio": bio,
"avatar": proPic,
"followers": [],
"following": [],
});
print("succuss");
massage("Succsussful Registation", context);
} else {
massage("You have missing data", context);
}
} on FirebaseAuthException catch (err) {
if (err.code == "invalid-email") {
massage("Invalid email format", context);
} else {
massage("Invalid password format", context);
}
} catch (err) {
massage("something went wrong", context);
}
}

//login user
Future<void> singInUser(
{required String email,
required String password,
required BuildContext context}) async {
// String response = "Invalid username or password";
try {
if (email.isNotEmpty && password.isNotEmpty) {
await _auth.signInWithEmailAndPassword(
email: email, password: password);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Homepage(),
));
// massage("Succsussfuly Log In", context);
} else {
massage("Missing credientials", context);
}
print("details missing");
} catch (err) {
print(err.toString());
massage(err.toString(), context);
}
return "";
}
}
13 changes: 13 additions & 0 deletions lib/service/media.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:image_picker/image_picker.dart';

class MediaAdder {
Future imagePicker(ImageSource source) async {
final ImagePicker picker = ImagePicker();
XFile? _image = await picker.pickImage(source: source);
if (_image != null) {
return await _image.readAsBytes();
} else {
return null;
}
}
}
26 changes: 26 additions & 0 deletions lib/service/storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:typed_data';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';

class StorageServices {
final FirebaseStorage _storage = FirebaseStorage.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
//upload file to storage
Future<String> uploadImagesToStorage(
String folderName, Uint8List file, bool isPost) async {
if (file.isNotEmpty) {
//get the path
Reference reference =
_storage.ref().child(folderName).child(_auth.currentUser!.uid);
//create uploadtask
UploadTask uploadTask = reference.putData(file!);
//get snapshot
TaskSnapshot snapshot = await uploadTask;
//get url of stored image and return
String imageUrl = await snapshot.ref.getDownloadURL();
return imageUrl;
}
return "";
}
}
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

#include "generated_plugin_registrant.h"

#include <file_selector_linux/file_selector_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
}
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
file_selector_linux
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import FlutterMacOS
import Foundation

import cloud_firestore
import file_selector_macos
import firebase_auth
import firebase_core
import firebase_storage
import path_provider_foundation

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAuthPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseStoragePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseStoragePlugin"))
Expand Down
Loading