Skip to content

Commit

Permalink
fix(auth, windows): allow idToken and accessToken to be nullable …
Browse files Browse the repository at this point in the history
…to stop windows crashing for `signInWithCredential()` (#12688)
  • Loading branch information
russellwheatley committed Apr 25, 2024
1 parent c42c4cb commit ca9f92d
Show file tree
Hide file tree
Showing 3 changed files with 402 additions and 287 deletions.
11 changes: 11 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ scripts:
run: git clean -x -d -f -q
description: Clean things very deeply, can be used to establish "pristine checkout" status.

test:e2e:windows:
run: |
melos exec -c 1 --fail-fast -- \
"flutter test integration_test/e2e_test.dart -d windows"
description: |
Run all e2e tests.
packageFilters:
dirExists:
- integration_test
scope: '*tests*'

qualitycheck:
run: |
melos run clean:deep && \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <future>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -605,41 +606,56 @@ firebase::auth::Credential getCredentialFromArguments(
return firebase::auth::Credential();
}

std::string idToken = std::get<std::string>(arguments[kArgumentIdToken]);
std::string accessToken =
std::get<std::string>(arguments[kArgumentAccessToken]);
// Lambda function to extract an optional string from the arguments map. This
// allows us to pass nullptr if no value exists
auto getStringOpt =
[&](const std::string& key) -> std::optional<std::string> {
auto it = arguments.find(key);
if (it != arguments.end() &&
std::holds_alternative<std::string>(it->second)) {
return std::get<std::string>(it->second);
}
return std::nullopt;
};

std::optional<std::string> idToken = getStringOpt(kArgumentIdToken);
std::optional<std::string> accessToken = getStringOpt(kArgumentAccessToken);

// Facebook Auth
if (signInMethod == kSignInMethodFacebook) {
return firebase::auth::FacebookAuthProvider::GetCredential(
accessToken.c_str());
accessToken.value().c_str());
}

// Google Auth
if (signInMethod == kSignInMethodGoogle) {
// Both accessToken and idToken arguments can be null. You can use one or
// the other
return firebase::auth::GoogleAuthProvider::GetCredential(
idToken.c_str(), accessToken.c_str());
idToken ? idToken.value().c_str() : nullptr,
accessToken ? accessToken.value().c_str() : nullptr);
}

// Twitter Auth
if (signInMethod == kSignInMethodTwitter) {
std::string secret = std::get<std::string>(arguments[kArgumentSecret]);
return firebase::auth::TwitterAuthProvider::GetCredential(idToken.c_str(),
secret.c_str());
return firebase::auth::TwitterAuthProvider::GetCredential(
idToken.value().c_str(), secret.c_str());
}

// GitHub Auth
if (signInMethod == kSignInMethodGithub) {
return firebase::auth::GitHubAuthProvider::GetCredential(
accessToken.c_str());
accessToken.value().c_str());
}

// OAuth
if (signInMethod == kSignInMethodOAuth) {
std::string providerId =
std::get<std::string>(arguments[kArgumentProviderId]);
return firebase::auth::OAuthProvider::GetCredential(
providerId.c_str(), idToken.c_str(), accessToken.c_str());
providerId.c_str(), idToken.value().c_str(),
accessToken.value().c_str());
}

// If no known auth method matched
Expand Down

0 comments on commit ca9f92d

Please sign in to comment.