oke pembaruan auth ke google

This commit is contained in:
kleponijo 2026-04-05 20:33:12 +07:00
parent 75d9e3220e
commit 499bf7d568
8 changed files with 434 additions and 339 deletions

View File

@ -8,9 +8,7 @@ part 'sign_in_state.dart';
class SignInBloc extends Bloc<SignInEvent, SignInState> {
final UserRepository _userRepository;
SignInBloc(
this._userRepository
) : super(SignInInitial()) {
SignInBloc(this._userRepository) : super(SignInInitial()) {
on<SignInRequired>((event, emit) async {
emit(SignInProcess());
try {
@ -20,6 +18,15 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
}
});
on<GoogleSignInRequired>((event, emit) async {
emit(SignInProcess());
try {
await _userRepository.signInWithGoogle();
} catch (e) {
emit(SignInFailure());
}
});
on<SignOutRequired>((event, emit) async => await _userRepository.logOut());
}
}

View File

@ -17,6 +17,6 @@ class SignInRequired extends SignInEvent {
List<Object> get props => [email, password];
}
class SignOutRequired extends SignInEvent {
class GoogleSignInRequired extends SignInEvent {}
}
class SignOutRequired extends SignInEvent {}

View File

@ -12,12 +12,23 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
on<SignUpRequired>((event, emit) async {
emit(SignUpProcess());
try {
MyUser myUser = await _userRepository.signUp(event.user, event.password);
MyUser myUser =
await _userRepository.signUp(event.user, event.password);
await _userRepository.setUserData(myUser);
emit(SignUpSuccess());
} catch (e) {
emit(SignUpFailure(e.toString()));
}
});
on<GoogleSignInRequired>((event, emit) async {
emit(SignUpProcess());
try {
await _userRepository.signInWithGoogle();
emit(SignUpSuccess());
} catch (e) {
emit(SignUpFailure(e.toString()));
}
});
}
}

View File

@ -16,3 +16,5 @@ class SignUpRequired extends SignUpEvent {
@override
List<Object> get props => [user, password];
}
class GoogleSignInRequired extends SignUpEvent {}

View File

@ -25,15 +25,15 @@ class _SignInScreenState extends State<SignInScreen> {
Widget build(BuildContext context) {
return BlocListener<SignInBloc, SignInState>(
listener: (context, state) {
if(state is SignInSuccess) {
if (state is SignInSuccess) {
setState(() {
signInRequired = false;
});
} else if(state is SignInProcess) {
} else if (state is SignInProcess) {
setState(() {
signInRequired = true;
});
} else if(state is SignInFailure) {
} else if (state is SignInFailure) {
setState(() {
signInRequired = false;
_errorMsg = 'Invalid email or password';
@ -59,9 +59,7 @@ class _SignInScreenState extends State<SignInScreen> {
return 'Please fill in this field';
}
return null;
}
)
),
})),
const SizedBox(height: 10),
SizedBox(
width: MediaQuery.of(context).size.width * 0.9,
@ -82,7 +80,7 @@ class _SignInScreenState extends State<SignInScreen> {
onPressed: () {
setState(() {
obscurePassword = !obscurePassword;
if(obscurePassword) {
if (obscurePassword) {
iconPassword = CupertinoIcons.eye_fill;
} else {
iconPassword = CupertinoIcons.eye_slash_fill;
@ -102,36 +100,62 @@ class _SignInScreenState extends State<SignInScreen> {
if (_formKey.currentState!.validate()) {
context.read<SignInBloc>().add(SignInRequired(
emailController.text,
passwordController.text)
);
passwordController.text));
}
},
style: TextButton.styleFrom(
elevation: 3.0,
backgroundColor: Theme.of(context).colorScheme.primary,
backgroundColor:
Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(60)
)
),
borderRadius: BorderRadius.circular(60))),
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 25, vertical: 5),
padding: EdgeInsets.symmetric(
horizontal: 25, vertical: 5),
child: Text(
'Sign In',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w600
),
),
)
fontWeight: FontWeight.w600),
),
)),
)
: const CircularProgressIndicator(),
],
)
const SizedBox(height: 20),
SizedBox(
width: MediaQuery.of(context).size.width * 0.5,
child: TextButton.icon(
onPressed: () {
context.read<SignInBloc>().add(GoogleSignInRequired());
},
icon: const Text(
'G',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
label: const Text(
'Sign In with Google',
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600),
),
style: TextButton.styleFrom(
elevation: 3.0,
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(60),
side: const BorderSide(color: Colors.grey))),
),
),
],
)),
);
}
}

View File

@ -55,7 +55,8 @@ class _SignUpScreenState extends State<SignUpScreen> {
const SizedBox(height: 30),
Container(
width: MediaQuery.of(context).size.width * 0.92,
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 22),
padding: const EdgeInsets.symmetric(
horizontal: 20, vertical: 22),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
@ -74,14 +75,16 @@ class _SignUpScreenState extends State<SignUpScreen> {
const Center(
child: Text(
'Buat Akun',
style: TextStyle(fontSize: 22, fontWeight: FontWeight.w700),
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.w700),
),
),
const SizedBox(height: 6),
const Center(
child: Text(
'Daftar untuk mulai monitoring',
style: TextStyle(fontSize: 13, color: Colors.black54),
style:
TextStyle(fontSize: 13, color: Colors.black54),
),
),
const SizedBox(height: 14),
@ -202,19 +205,59 @@ class _SignUpScreenState extends State<SignUpScreen> {
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black87,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
padding: const EdgeInsets.symmetric(
vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text(
'Daftar',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600),
),
),
)
: const CircularProgressIndicator(),
const SizedBox(height: 12),
Center(
child: Text('Atau'),
),
const SizedBox(height: 12),
SizedBox(
width: MediaQuery.of(context).size.width * 0.92,
child: TextButton.icon(
onPressed: () {
context
.read<SignUpBloc>()
.add(GoogleSignInRequired());
},
icon: const Text(
'G',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
label: const Text(
'Daftar dengan Google',
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600),
),
style: TextButton.styleFrom(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: const BorderSide(color: Colors.grey)),
padding: const EdgeInsets.symmetric(vertical: 16),
),
),
),
const SizedBox(height: 12),
Center(
child: Row(

View File

@ -21,13 +21,15 @@ class FirebaseUserRepo implements UserRepository {
try {
final userData = await userCollection.doc(firebaseUser.uid).get();
if (userData.exists && userData.data() != null) {
yield MyUser.fromEntity(MyUserEntity.fromDocument(userData.data()!));
yield MyUser.fromEntity(
MyUserEntity.fromDocument(userData.data()!));
} else {
// User baru yang belum ada data di Firestore, tunggu sebentar
await Future.delayed(const Duration(milliseconds: 500));
final retryData = await userCollection.doc(firebaseUser.uid).get();
if (retryData.exists && retryData.data() != null) {
yield MyUser.fromEntity(MyUserEntity.fromDocument(retryData.data()!));
yield MyUser.fromEntity(
MyUserEntity.fromDocument(retryData.data()!));
} else {
yield MyUser.empty;
}
@ -44,9 +46,7 @@ class FirebaseUserRepo implements UserRepository {
Future<void> signIn(String email, String password) async {
try {
await _firebaseAuth.signInWithEmailAndPassword(
email: email,
password: password
);
email: email, password: password);
} catch (e) {
log(e.toString());
rethrow;
@ -57,9 +57,7 @@ class FirebaseUserRepo implements UserRepository {
Future<MyUser> signUp(MyUser myUser, String password) async {
try {
UserCredential user = await _firebaseAuth.createUserWithEmailAndPassword(
email: myUser.email,
password: password
);
email: myUser.email, password: password);
myUser.userId = user.user!.uid;
return myUser;
} catch (e) {
@ -87,6 +85,14 @@ class FirebaseUserRepo implements UserRepository {
}
}
@override
Future<void> signInWithGoogle() async {
try {
final GoogleAuthProvider googleProvider = GoogleAuthProvider();
await _firebaseAuth.signInWithPopup(googleProvider);
} catch (e) {
log('Google sign-in error: $e');
rethrow;
}
}
}

View File

@ -9,5 +9,7 @@ abstract class UserRepository {
Future<void> signIn(String email, String password);
Future<void> signInWithGoogle();
Future<void> logOut();
}