diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h @@ -163,7 +163,13 @@ jsi::String userID, jsi::String deviceID, jsi::String accessToken) override; + virtual void setCommServicesAuthMetadata( + std::string userID, + std::string deviceID, + std::string accessToken); virtual jsi::Value getCommServicesAuthMetadata(jsi::Runtime &rt) override; + virtual jsi::Value clearCommServicesAuthMetadata(jsi::Runtime &rt) override; + virtual void clearCommServicesAuthMetadata(); virtual jsi::Value setCommServicesAccessToken( jsi::Runtime &rt, jsi::String accessToken) override; diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -1482,6 +1482,7 @@ taskType job = [this, promise]() { std::string error; try { + this->clearCommServicesAuthMetadata(); DatabaseManager::clearSensitiveData(); } catch (const std::exception &e) { error = e.what(); @@ -1587,92 +1588,101 @@ rt, [this, userIDStr, deviceIDStr, accessTokenStr]( jsi::Runtime &innerRt, std::shared_ptr promise) { - taskType job = - [this, promise, userIDStr, deviceIDStr, accessTokenStr]() { - std::string error; - try { - CommSecureStore::set(CommSecureStore::userID, userIDStr); - CommSecureStore::set(CommSecureStore::deviceID, deviceIDStr); - CommSecureStore::set( - CommSecureStore::commServicesAccessToken, accessTokenStr); - CommServicesAuthMetadataEmitter::sendAuthMetadataToJS( - accessTokenStr, userIDStr); - } catch (const std::exception &e) { - error = e.what(); - } - this->jsInvoker_->invokeAsync([error, promise]() { - if (error.size()) { - promise->reject(error); - } else { - promise->resolve(jsi::Value::undefined()); - } - }); - }; - GlobalDBSingleton::instance.scheduleOrRunCancellable( - job, promise, this->jsInvoker_); + std::string error; + try { + this->setCommServicesAuthMetadata( + userIDStr, deviceIDStr, accessTokenStr); + } catch (const std::exception &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync([error, promise]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); + } + }); }); } +void CommCoreModule::setCommServicesAuthMetadata( + std::string userID, + std::string deviceID, + std::string accessToken) { + CommSecureStore::set(CommSecureStore::userID, userID); + CommSecureStore::set(CommSecureStore::deviceID, deviceID); + CommSecureStore::set(CommSecureStore::commServicesAccessToken, accessToken); + CommServicesAuthMetadataEmitter::sendAuthMetadataToJS(accessToken, userID); +} + jsi::Value CommCoreModule::getCommServicesAuthMetadata(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { - taskType job = [this, &innerRt, promise]() { - std::string error; - std::string userID; - std::string deviceID; - std::string accessToken; - try { - folly::Optional userIDOpt = - CommSecureStore::get(CommSecureStore::userID); - if (userIDOpt.hasValue()) { - userID = userIDOpt.value(); - } - folly::Optional deviceIDOpt = - CommSecureStore::get(CommSecureStore::deviceID); - if (deviceIDOpt.hasValue()) { - deviceID = deviceIDOpt.value(); - } - folly::Optional accessTokenOpt = - CommSecureStore::get(CommSecureStore::commServicesAccessToken); - if (accessTokenOpt.hasValue()) { - accessToken = accessTokenOpt.value(); - } - } catch (const std::exception &e) { - error = e.what(); + std::string error; + std::string userID; + std::string deviceID; + std::string accessToken; + try { + folly::Optional userIDOpt = + CommSecureStore::get(CommSecureStore::userID); + if (userIDOpt.hasValue()) { + userID = userIDOpt.value(); } - this->jsInvoker_->invokeAsync( - [&innerRt, error, userID, deviceID, accessToken, promise]() { - if (error.size()) { - promise->reject(error); - } else { - auto authMetadata = jsi::Object(innerRt); - if (!userID.empty()) { - authMetadata.setProperty( - innerRt, - "userID", - jsi::String::createFromUtf8(innerRt, userID)); - } - if (!deviceID.empty()) { - authMetadata.setProperty( - innerRt, - "deviceID", - jsi::String::createFromUtf8(innerRt, deviceID)); - } - if (!accessToken.empty()) { - authMetadata.setProperty( - innerRt, - "accessToken", - jsi::String::createFromUtf8(innerRt, accessToken)); - } - promise->resolve(std::move(authMetadata)); + folly::Optional deviceIDOpt = + CommSecureStore::get(CommSecureStore::deviceID); + if (deviceIDOpt.hasValue()) { + deviceID = deviceIDOpt.value(); + } + folly::Optional accessTokenOpt = + CommSecureStore::get(CommSecureStore::commServicesAccessToken); + if (accessTokenOpt.hasValue()) { + accessToken = accessTokenOpt.value(); + } + } catch (const std::exception &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync( + [&innerRt, error, userID, deviceID, accessToken, promise]() { + if (error.size()) { + promise->reject(error); + } else { + auto authMetadata = jsi::Object(innerRt); + if (!userID.empty()) { + authMetadata.setProperty( + innerRt, + "userID", + jsi::String::createFromUtf8(innerRt, userID)); } - }); - }; - GlobalDBSingleton::instance.scheduleOrRunCancellable( - job, promise, this->jsInvoker_); + if (!deviceID.empty()) { + authMetadata.setProperty( + innerRt, + "deviceID", + jsi::String::createFromUtf8(innerRt, deviceID)); + } + if (!accessToken.empty()) { + authMetadata.setProperty( + innerRt, + "accessToken", + jsi::String::createFromUtf8(innerRt, accessToken)); + } + promise->resolve(std::move(authMetadata)); + } + }); }); } +jsi::Value CommCoreModule::clearCommServicesAuthMetadata(jsi::Runtime &rt) { + return this->setCommServicesAuthMetadata( + rt, + jsi::String::createFromUtf8(rt, ""), + jsi::String::createFromUtf8(rt, ""), + jsi::String::createFromUtf8(rt, "")); +} + +void CommCoreModule::clearCommServicesAuthMetadata() { + return this->setCommServicesAuthMetadata("", "", ""); +} + jsi::Value CommCoreModule::setCommServicesAccessToken( jsi::Runtime &rt, jsi::String accessToken) { @@ -1681,47 +1691,39 @@ rt, [this, accessTokenStr]( jsi::Runtime &innerRt, std::shared_ptr promise) { - taskType job = [this, promise, accessTokenStr]() { - std::string error; - try { - CommSecureStore::set( - CommSecureStore::commServicesAccessToken, accessTokenStr); - } catch (const std::exception &e) { - error = e.what(); + std::string error; + try { + CommSecureStore::set( + CommSecureStore::commServicesAccessToken, accessTokenStr); + } catch (const std::exception &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync([error, promise]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); } - this->jsInvoker_->invokeAsync([error, promise]() { - if (error.size()) { - promise->reject(error); - } else { - promise->resolve(jsi::Value::undefined()); - } - }); - }; - GlobalDBSingleton::instance.scheduleOrRunCancellable( - job, promise, this->jsInvoker_); + }); }); } jsi::Value CommCoreModule::clearCommServicesAccessToken(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { - taskType job = [this, promise]() { - std::string error; - try { - CommSecureStore::set(CommSecureStore::commServicesAccessToken, ""); - } catch (const std::exception &e) { - error = e.what(); + std::string error; + try { + CommSecureStore::set(CommSecureStore::commServicesAccessToken, ""); + } catch (const std::exception &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync([error, promise]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); } - this->jsInvoker_->invokeAsync([error, promise]() { - if (error.size()) { - promise->reject(error); - } else { - promise->resolve(jsi::Value::undefined()); - } - }); - }; - GlobalDBSingleton::instance.scheduleOrRunCancellable( - job, promise, this->jsInvoker_); + }); }); } diff --git a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp --- a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp +++ b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp @@ -161,6 +161,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getCommServicesAuthMetadata(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->getCommServicesAuthMetadata(rt); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearCommServicesAuthMetadata(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->clearCommServicesAuthMetadata(rt); +} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_setCommServicesAccessToken(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->setCommServicesAccessToken(rt, args[0].asString(rt)); } @@ -238,6 +241,7 @@ methodMap_["generateRandomString"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateRandomString}; methodMap_["setCommServicesAuthMetadata"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_setCommServicesAuthMetadata}; methodMap_["getCommServicesAuthMetadata"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getCommServicesAuthMetadata}; + methodMap_["clearCommServicesAuthMetadata"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearCommServicesAuthMetadata}; methodMap_["setCommServicesAccessToken"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_setCommServicesAccessToken}; methodMap_["clearCommServicesAccessToken"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearCommServicesAccessToken}; methodMap_["startBackupHandler"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_startBackupHandler}; diff --git a/native/cpp/CommonCpp/_generated/commJSI.h b/native/cpp/CommonCpp/_generated/commJSI.h --- a/native/cpp/CommonCpp/_generated/commJSI.h +++ b/native/cpp/CommonCpp/_generated/commJSI.h @@ -68,6 +68,7 @@ virtual jsi::Value generateRandomString(jsi::Runtime &rt, double size) = 0; virtual jsi::Value setCommServicesAuthMetadata(jsi::Runtime &rt, jsi::String userID, jsi::String deviceID, jsi::String accessToken) = 0; virtual jsi::Value getCommServicesAuthMetadata(jsi::Runtime &rt) = 0; + virtual jsi::Value clearCommServicesAuthMetadata(jsi::Runtime &rt) = 0; virtual jsi::Value setCommServicesAccessToken(jsi::Runtime &rt, jsi::String accessToken) = 0; virtual jsi::Value clearCommServicesAccessToken(jsi::Runtime &rt) = 0; virtual void startBackupHandler(jsi::Runtime &rt) = 0; @@ -481,6 +482,14 @@ return bridging::callFromJs( rt, &T::getCommServicesAuthMetadata, jsInvoker_, instance_); } + jsi::Value clearCommServicesAuthMetadata(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::clearCommServicesAuthMetadata) == 1, + "Expected clearCommServicesAuthMetadata(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::clearCommServicesAuthMetadata, jsInvoker_, instance_); + } jsi::Value setCommServicesAccessToken(jsi::Runtime &rt, jsi::String accessToken) override { static_assert( bridging::getParameterCount(&T::setCommServicesAccessToken) == 2, diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -136,6 +136,7 @@ accessToken: string, ) => Promise; +getCommServicesAuthMetadata: () => Promise; + +clearCommServicesAuthMetadata: () => Promise; +setCommServicesAccessToken: (accessToken: string) => Promise; +clearCommServicesAccessToken: () => Promise; +startBackupHandler: () => void;