oke pembaruan auth ke google
This commit is contained in:
parent
75d9e3220e
commit
499bf7d568
|
|
@ -8,9 +8,7 @@ part 'sign_in_state.dart';
|
||||||
class SignInBloc extends Bloc<SignInEvent, SignInState> {
|
class SignInBloc extends Bloc<SignInEvent, SignInState> {
|
||||||
final UserRepository _userRepository;
|
final UserRepository _userRepository;
|
||||||
|
|
||||||
SignInBloc(
|
SignInBloc(this._userRepository) : super(SignInInitial()) {
|
||||||
this._userRepository
|
|
||||||
) : super(SignInInitial()) {
|
|
||||||
on<SignInRequired>((event, emit) async {
|
on<SignInRequired>((event, emit) async {
|
||||||
emit(SignInProcess());
|
emit(SignInProcess());
|
||||||
try {
|
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());
|
on<SignOutRequired>((event, emit) async => await _userRepository.logOut());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,6 @@ class SignInRequired extends SignInEvent {
|
||||||
List<Object> get props => [email, password];
|
List<Object> get props => [email, password];
|
||||||
}
|
}
|
||||||
|
|
||||||
class SignOutRequired extends SignInEvent {
|
class GoogleSignInRequired extends SignInEvent {}
|
||||||
|
|
||||||
}
|
class SignOutRequired extends SignInEvent {}
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,23 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
|
||||||
on<SignUpRequired>((event, emit) async {
|
on<SignUpRequired>((event, emit) async {
|
||||||
emit(SignUpProcess());
|
emit(SignUpProcess());
|
||||||
try {
|
try {
|
||||||
MyUser myUser = await _userRepository.signUp(event.user, event.password);
|
MyUser myUser =
|
||||||
|
await _userRepository.signUp(event.user, event.password);
|
||||||
await _userRepository.setUserData(myUser);
|
await _userRepository.setUserData(myUser);
|
||||||
emit(SignUpSuccess());
|
emit(SignUpSuccess());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(SignUpFailure(e.toString()));
|
emit(SignUpFailure(e.toString()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
on<GoogleSignInRequired>((event, emit) async {
|
||||||
|
emit(SignUpProcess());
|
||||||
|
try {
|
||||||
|
await _userRepository.signInWithGoogle();
|
||||||
|
emit(SignUpSuccess());
|
||||||
|
} catch (e) {
|
||||||
|
emit(SignUpFailure(e.toString()));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,3 +16,5 @@ class SignUpRequired extends SignUpEvent {
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [user, password];
|
List<Object> get props => [user, password];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class GoogleSignInRequired extends SignUpEvent {}
|
||||||
|
|
|
||||||
|
|
@ -59,9 +59,7 @@ class _SignInScreenState extends State<SignInScreen> {
|
||||||
return 'Please fill in this field';
|
return 'Please fill in this field';
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
})),
|
||||||
)
|
|
||||||
),
|
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
SizedBox(
|
SizedBox(
|
||||||
width: MediaQuery.of(context).size.width * 0.9,
|
width: MediaQuery.of(context).size.width * 0.9,
|
||||||
|
|
@ -102,36 +100,62 @@ class _SignInScreenState extends State<SignInScreen> {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
context.read<SignInBloc>().add(SignInRequired(
|
context.read<SignInBloc>().add(SignInRequired(
|
||||||
emailController.text,
|
emailController.text,
|
||||||
passwordController.text)
|
passwordController.text));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
style: TextButton.styleFrom(
|
style: TextButton.styleFrom(
|
||||||
elevation: 3.0,
|
elevation: 3.0,
|
||||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
backgroundColor:
|
||||||
|
Theme.of(context).colorScheme.primary,
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
borderRadius: BorderRadius.circular(60)
|
borderRadius: BorderRadius.circular(60))),
|
||||||
)
|
|
||||||
),
|
|
||||||
child: const Padding(
|
child: const Padding(
|
||||||
padding: EdgeInsets.symmetric(horizontal: 25, vertical: 5),
|
padding: EdgeInsets.symmetric(
|
||||||
|
horizontal: 25, vertical: 5),
|
||||||
child: Text(
|
child: Text(
|
||||||
'Sign In',
|
'Sign In',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
fontSize: 16,
|
fontSize: 16,
|
||||||
fontWeight: FontWeight.w600
|
fontWeight: FontWeight.w600),
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
),
|
),
|
||||||
|
)),
|
||||||
)
|
)
|
||||||
: const CircularProgressIndicator(),
|
: 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))),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +55,8 @@ class _SignUpScreenState extends State<SignUpScreen> {
|
||||||
const SizedBox(height: 30),
|
const SizedBox(height: 30),
|
||||||
Container(
|
Container(
|
||||||
width: MediaQuery.of(context).size.width * 0.92,
|
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(
|
decoration: BoxDecoration(
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
borderRadius: BorderRadius.circular(12),
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
|
@ -74,14 +75,16 @@ class _SignUpScreenState extends State<SignUpScreen> {
|
||||||
const Center(
|
const Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
'Buat Akun',
|
'Buat Akun',
|
||||||
style: TextStyle(fontSize: 22, fontWeight: FontWeight.w700),
|
style: TextStyle(
|
||||||
|
fontSize: 22, fontWeight: FontWeight.w700),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 6),
|
const SizedBox(height: 6),
|
||||||
const Center(
|
const Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
'Daftar untuk mulai monitoring',
|
'Daftar untuk mulai monitoring',
|
||||||
style: TextStyle(fontSize: 13, color: Colors.black54),
|
style:
|
||||||
|
TextStyle(fontSize: 13, color: Colors.black54),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 14),
|
const SizedBox(height: 14),
|
||||||
|
|
@ -202,19 +205,59 @@ class _SignUpScreenState extends State<SignUpScreen> {
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
backgroundColor: Colors.black87,
|
backgroundColor: Colors.black87,
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
padding: const EdgeInsets.symmetric(
|
||||||
|
vertical: 16),
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
child: const Text(
|
child: const Text(
|
||||||
'Daftar',
|
'Daftar',
|
||||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
|
style: TextStyle(
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: FontWeight.w600),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
: const CircularProgressIndicator(),
|
: 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),
|
const SizedBox(height: 12),
|
||||||
Center(
|
Center(
|
||||||
child: Row(
|
child: Row(
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,15 @@ class FirebaseUserRepo implements UserRepository {
|
||||||
try {
|
try {
|
||||||
final userData = await userCollection.doc(firebaseUser.uid).get();
|
final userData = await userCollection.doc(firebaseUser.uid).get();
|
||||||
if (userData.exists && userData.data() != null) {
|
if (userData.exists && userData.data() != null) {
|
||||||
yield MyUser.fromEntity(MyUserEntity.fromDocument(userData.data()!));
|
yield MyUser.fromEntity(
|
||||||
|
MyUserEntity.fromDocument(userData.data()!));
|
||||||
} else {
|
} else {
|
||||||
// User baru yang belum ada data di Firestore, tunggu sebentar
|
// User baru yang belum ada data di Firestore, tunggu sebentar
|
||||||
await Future.delayed(const Duration(milliseconds: 500));
|
await Future.delayed(const Duration(milliseconds: 500));
|
||||||
final retryData = await userCollection.doc(firebaseUser.uid).get();
|
final retryData = await userCollection.doc(firebaseUser.uid).get();
|
||||||
if (retryData.exists && retryData.data() != null) {
|
if (retryData.exists && retryData.data() != null) {
|
||||||
yield MyUser.fromEntity(MyUserEntity.fromDocument(retryData.data()!));
|
yield MyUser.fromEntity(
|
||||||
|
MyUserEntity.fromDocument(retryData.data()!));
|
||||||
} else {
|
} else {
|
||||||
yield MyUser.empty;
|
yield MyUser.empty;
|
||||||
}
|
}
|
||||||
|
|
@ -44,9 +46,7 @@ class FirebaseUserRepo implements UserRepository {
|
||||||
Future<void> signIn(String email, String password) async {
|
Future<void> signIn(String email, String password) async {
|
||||||
try {
|
try {
|
||||||
await _firebaseAuth.signInWithEmailAndPassword(
|
await _firebaseAuth.signInWithEmailAndPassword(
|
||||||
email: email,
|
email: email, password: password);
|
||||||
password: password
|
|
||||||
);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log(e.toString());
|
log(e.toString());
|
||||||
rethrow;
|
rethrow;
|
||||||
|
|
@ -57,9 +57,7 @@ class FirebaseUserRepo implements UserRepository {
|
||||||
Future<MyUser> signUp(MyUser myUser, String password) async {
|
Future<MyUser> signUp(MyUser myUser, String password) async {
|
||||||
try {
|
try {
|
||||||
UserCredential user = await _firebaseAuth.createUserWithEmailAndPassword(
|
UserCredential user = await _firebaseAuth.createUserWithEmailAndPassword(
|
||||||
email: myUser.email,
|
email: myUser.email, password: password);
|
||||||
password: password
|
|
||||||
);
|
|
||||||
myUser.userId = user.user!.uid;
|
myUser.userId = user.user!.uid;
|
||||||
return myUser;
|
return myUser;
|
||||||
} catch (e) {
|
} 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -9,5 +9,7 @@ abstract class UserRepository {
|
||||||
|
|
||||||
Future<void> signIn(String email, String password);
|
Future<void> signIn(String email, String password);
|
||||||
|
|
||||||
|
Future<void> signInWithGoogle();
|
||||||
|
|
||||||
Future<void> logOut();
|
Future<void> logOut();
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue