Skip to content

Instantly share code, notes, and snippets.

@bhupiister
Last active May 4, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhupiister/72f894df9ff6ad03ac7dfa0ebfcb8d20 to your computer and use it in GitHub Desktop.
Save bhupiister/72f894df9ff6ad03ac7dfa0ebfcb8d20 to your computer and use it in GitHub Desktop.
Unity Google Sign in plugin with Firebase, App crashes with error configuration is null!? and Request not configured! Failing authenticate
namespace Google {
using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Google.Impl;
using UnityEngine;
public class GoogleSignIn {
#if !UNITY_ANDROID && !UNITY_IOS
static GoogleSignIn() {
Debug.LogError("This platform is not supported");
}
#endif
private static GoogleSignIn theInstance = null;
private static GoogleSignInConfiguration theConfiguration = null;
private ISignInImpl impl;
///<summary> The configuration settings for Google Sign-in.</summary>
///<remarks> The configuration should be set before calling the sign-in
/// methods. Once the configuration is set it cannot be changed.
///</remarks>
public static GoogleSignInConfiguration Configuration
{
set
{
// Can set the configuration until the singleton is created.
if (theInstance == null || theConfiguration == value || theConfiguration == null)
{
if (theInstance != null && theConfiguration == null)
{
//From sign out, Instance stays non-null but theConfiguration is null.
//Needs configuration refresh.
theInstance.impl.Configure(value);
}
theConfiguration = value;
}
else
{
throw new SignInException(GoogleSignInStatusCode.DeveloperError,
"DefaultInstance already created. " +
" Cannot change configuration after creation.");
}
}
get
{
return theConfiguration;
}
}
/// <summary>
/// Singleton instance of this class.
/// </summary>
/// <value>The instance.</value>
public static GoogleSignIn DefaultInstance
{
get
{
Debug.Log("GSIn 1.0");
if (theInstance == null)
{
#if UNITY_ANDROID || UNITY_IOS
theInstance = new GoogleSignIn(new GoogleSignInImpl(Configuration));
#else
theInstance = new GoogleSignIn(null);
throw new SignInException(
GoogleSignInStatusCode.DeveloperError,
"This platform is not supported by GoogleSignIn");
#endif
}
Debug.Log("GSIn 1.1");
return theInstance;
}
}
internal GoogleSignIn(GoogleSignInImpl impl) {
this.impl = impl;
}
public void EnableDebugLogging(bool flag) {
impl.EnableDebugLogging(flag);
}
/// <summary>Starts the authentication process.</summary>
/// <remarks>
/// The authenication process is started and may display account picker
/// popups and consent prompts based on the state of authentication and
/// the requested elements.
/// </remarks>
public Task<GoogleSignInUser> SignIn()
{
Debug.Log("GSInUser 1.0");
var tcs = new TaskCompletionSource<GoogleSignInUser>();
SignInHelperObject.Instance.StartCoroutine(
impl.SignIn().WaitForResult(tcs));
Debug.Log("GSInUser 1.1");
return tcs.Task;
}
/// <summary>Starts the silent authentication process.</summary>
/// <remarks>
/// The authenication process is started and will attempt to sign in without
/// displaying any UI. If this cannot be done, the developer should call
/// SignIn().
/// </remarks>
public Task<GoogleSignInUser> SignInSilently() {
var tcs = new TaskCompletionSource<GoogleSignInUser>();
SignInHelperObject.Instance.StartCoroutine(
impl.SignInSilently().WaitForResult(tcs));
return tcs.Task;
}
/// <summary>
/// Signs out the User.
/// </summary>
/// <remarks>Future sign-in attempts will require the user to select the
/// account to use when signing in.
/// </remarks>
public void SignOut() {
theConfiguration = null;
impl.SignOut();
}
/// <summary>
/// Disconnect this instance.
/// </summary>
/// <remarks>When the user is disconnected, it revokes all access that may
/// have been granted to this application. This includes any server side
/// access tokens derived from server auth codes. As a result, future
/// sign-in attempts will require the user to re-consent to the requested
/// scopes.
/// </remarks>
public void Disconnect() {
impl.Disconnect();
}
/// <summary>
/// Sign in exception. This is a checked exception for handling specific
/// errors during the sign-in process.
/// </summary>
[Serializable]
public class SignInException : Exception {
internal SignInException(GoogleSignInStatusCode status) {
Status = status;
}
public SignInException(GoogleSignInStatusCode status, string message) :
base(message) {
Status = status;
}
public SignInException(GoogleSignInStatusCode status, string message,
Exception innerException) : base(message, innerException) {
Status = status;
}
protected SignInException(GoogleSignInStatusCode status,
SerializationInfo info,
StreamingContext context) :
base(info, context) {
Status = status;
}
public GoogleSignInStatusCode Status {
get;
internal set;
}
}
}
internal interface ISignInImpl {
Future<GoogleSignInUser> SignIn();
Future<GoogleSignInUser> SignInSilently();
void EnableDebugLogging(bool flag);
void SignOut();
void Disconnect();
void Configure(GoogleSignInConfiguration configuration);
}
} // namespace Google
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment