|
| 1 | +// Copyright 2024 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart'; |
| 6 | + |
| 7 | +const _kProviderId = 'playgames.google.com'; |
| 8 | + |
| 9 | +/// This class should be used to either create a new Play Games credential with an |
| 10 | +/// access code, or use the provider to trigger user authentication flows. |
| 11 | +/// |
| 12 | +/// If authenticating with Play Games via a 3rd party, use the returned |
| 13 | +/// `serverAuthCode` to sign-in or link the user with the created credential, |
| 14 | +/// for example: |
| 15 | +/// |
| 16 | +/// ```dart |
| 17 | +/// String serverAuthCode = '...'; // From 3rd party provider |
| 18 | +/// var playGamesAuthCredential = PlayGamesAuthCredential.credential(serverAuthCode: serverAuthCode); |
| 19 | +/// |
| 20 | +/// FirebaseAuth.instance.signInWithCredential(playGamesAuthCredential) |
| 21 | +/// .then(...); |
| 22 | +/// ``` |
| 23 | +class PlayGamesAuthProvider extends AuthProvider { |
| 24 | + /// Creates a new instance. |
| 25 | + PlayGamesAuthProvider() : super(_kProviderId); |
| 26 | + |
| 27 | + /// Create a new [PlayGamesAuthCredential] from a provided [serverAuthCode] |
| 28 | + static OAuthCredential credential({ |
| 29 | + required String serverAuthCode, |
| 30 | + }) { |
| 31 | + return PlayGamesAuthCredential._credential( |
| 32 | + serverAuthCode: serverAuthCode, |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + /// This corresponds to the sign-in method identifier. |
| 37 | + static String get PLAY_GAMES_SIGN_IN_METHOD { |
| 38 | + return _kProviderId; |
| 39 | + } |
| 40 | + |
| 41 | + // ignore: public_member_api_docs |
| 42 | + static String get PROVIDER_ID { |
| 43 | + return _kProviderId; |
| 44 | + } |
| 45 | + |
| 46 | + Map<String, String> _parameters = {}; |
| 47 | + |
| 48 | + /// Returns the parameters for this provider instance. |
| 49 | + Map<String, String> get parameters { |
| 50 | + return _parameters; |
| 51 | + } |
| 52 | + |
| 53 | + /// Sets the OAuth custom parameters to pass in a Play Games OAuth request for |
| 54 | + /// popup and redirect sign-in operations. |
| 55 | + PlayGamesAuthProvider setCustomParameters( |
| 56 | + Map<String, String> customOAuthParameters, |
| 57 | + ) { |
| 58 | + _parameters = customOAuthParameters; |
| 59 | + return this; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// The auth credential returned from calling |
| 64 | +/// [PlayGamesAuthProvider.credential]. |
| 65 | +class PlayGamesAuthCredential extends OAuthCredential { |
| 66 | + PlayGamesAuthCredential._({ |
| 67 | + required String serverAuthCode, |
| 68 | + }) : super( |
| 69 | + providerId: _kProviderId, |
| 70 | + signInMethod: _kProviderId, |
| 71 | + serverAuthCode: serverAuthCode, |
| 72 | + ); |
| 73 | + |
| 74 | + factory PlayGamesAuthCredential._credential({ |
| 75 | + required String serverAuthCode, |
| 76 | + }) { |
| 77 | + return PlayGamesAuthCredential._(serverAuthCode: serverAuthCode); |
| 78 | + } |
| 79 | +} |
0 commit comments