Im very new to flutter. Now i
m tring to make my original app and I want it to have riverpod system.
The first thing I faced is Google signIn, I want to use riverpod to use google SignIn.
This is my auth class… and Im wondering how to change this into code with riverpod implemented. I googled and chatted with GPT, so I assume how to do this... but I would like to have pro coder
s advice.
This is what I`ve done so far.
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../pages/signin_page.dart';
part 'auth_service.g.dart';
@Riverpod()
class AuthService extends _$AuthService{
User? user;
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
Future<void> signInWithGoogle() async {
try {
final GoogleSignInAccount? googleSignInAccount =
await _googleSignIn.signIn();
if (googleSignInAccount == null) {
// 사용자가 Google 로그인 창을 닫거나 취소한 경우 처리
return;
}
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);
final User? user = userCredential.user;
if (user != null) {
print('로그인 성공: ${user.displayName} (${user.email})');
// 로그인 후 추가 작업 수행
}
// Use the user object for further operations or navigate to a new screen.
} catch (e) {
print('The error occured : {$e.toString()}');
}
}
//I think this is enough.
Future<void> signOutAndNavigate(BuildContext context) async {
try {
await _googleSignIn.signOut();
await _auth.signOut();
if (context.mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const SigninPage()),
);
}
} catch (e) {
print('The error occured : {$e.toString()}');
// 선택적으로 사용자 친화적인 오류 메시지를 표시합니다.
}
}
}
I watched many videos about riverpod, but I couldn`t find exact one.
How to use Riverpod code generator is just that ‘Add part 000.g.dart’, annotation(@riverpod), add extends _$000.
Rafa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.