googleProvider function

Google OAuth provider.

Based on Google's OAuth 2.0 and OpenID Connect documentation.

Resources

Example

final provider = googleProvider(
  GoogleProviderOptions(
    clientId: 'client-id',
    clientSecret: 'client-secret',
    redirectUri: 'https://example.com/auth/callback/google',
  ),
);

Implementation

OAuthProvider<GoogleProfile> googleProvider(GoogleProviderOptions options) {
  final authorizationParams = <String, String>{};
  if (options.accessType != null) {
    authorizationParams['access_type'] = options.accessType!;
  }
  if (options.prompt != null) {
    authorizationParams['prompt'] = options.prompt!;
  }
  if (options.hostedDomain != null) {
    authorizationParams['hd'] = options.hostedDomain!;
  }

  return OAuthProvider<GoogleProfile>(
    id: 'google',
    name: 'Google',
    type: AuthProviderType.oidc,
    clientId: options.clientId,
    clientSecret: options.clientSecret,
    authorizationEndpoint: Uri.parse(
      'https://accounts.google.com/o/oauth2/v2/auth',
    ),
    tokenEndpoint: Uri.parse('https://oauth2.googleapis.com/token'),
    userInfoEndpoint: Uri.parse(
      'https://openidconnect.googleapis.com/v1/userinfo',
    ),
    redirectUri: options.redirectUri,
    scopes: options.scopes,
    authorizationParams: authorizationParams,
    profileParser: GoogleProfile.fromJson,
    profileSerializer: (profile) => profile.toJson(),
    profile: (profile) {
      return AuthUser(
        id: profile.sub,
        name: profile.name,
        email: profile.email,
        image: profile.picture,
        attributes: profile.toJson(),
      );
    },
  );
}