google_oauth2_flutter
A Flutter package that enables OAuth 2.0 authorization with Google to obtain access tokens and interact with Google APIs on behalf of the user.
β Supports:
- Android
- Web
- Windows
π This package simplifies the Google OAuth 2.0 flow across platforms.
π Features
- Easy Google Sign-In with OAuth 2.0
- Custom success, failure, and loading UI support
- Built-in handling of
access_token,id_token, and authorization code - Works on both mobile (Android) and desktop (Windows), as well as the web
π Usage
1. Sign in with Google (Android & Windows)
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return GoogleOAuthView(
setup: GoogleAuthSetup(
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUri,
scopes: scopes,
loginHint: loginHint,
onSuccess: (tokens) {
// Handle tokens here
},
onError: (error) {
// Handle error here
},
appBarTitle: 'Sign in with Google',
centerTitle: true,
loadingWidget: CircularProgressIndicator(),
failureWidget: Text('Login failed'),
successWidget: Text('Login successful'),
),
);
},
),
);
2. Google Sign-In Flow (Web Only)
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
void initState() {
super.initState();
WebGoogleAuth.listenToRequestUrl(
setup: GoogleAuthSetup(
clientId: dotenv.env["GoogleClientId"]!,
clientSecret: dotenv.env["GoogleClientSecret"]!,
redirectUri: redirectUri,
onSuccess: (tokens) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(tokens.accessToken),
),
);
},
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: IconButton(
onPressed: () {
WebGoogleAuth.startSignin(
setup: GoogleAuthSetup(
clientId: dotenv.env["GoogleClientId"]!,
clientSecret: dotenv.env["GoogleClientSecret"]!,
redirectUri: redirectUri,
),
);
},
icon: Icon(Icons.login),
),
),
);
}
}
π Learn More
Read this article to understand the Google OAuth 2.0 flow in detail: π How to Access Google APIs via OAuth 2.0
Libraries
- enums/google_auth_state
- errors/failure
- errors/google_auth_failure
- google_oauth2
- models/auth_req_sec_params
- models/google_auth_callback
- models/google_auth_setup
- models/google_auth_url_paarmeters
- models/google_oauth_model
- models/google_token_exchanger_input_model
- models/google_tokens
- repo/google_oauth_repo
- utils/auth_req_sec_params_generator
- utils/code_challange_generator
- utils/code_verifer_generator
- utils/generate_state
- utils/google_auth_listener
- utils/google_auth_url
- utils/google_token_exchanger
- views/google_oauth_view
- web_service/web_google_auth_real
- web_service/web_google_auth_stub