You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.9KB

  1. /* Copyright 2013-2019 Matt Tytel
  2. *
  3. * vital is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * vital is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with vital. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #if NDEBUG && !NO_AUTH
  18. #include "JuceHeader.h"
  19. #include "load_save.h"
  20. #if defined(__APPLE__)
  21. #include <firebase/app.h>
  22. #include <firebase/auth.h>
  23. #else
  24. #include "firebase/app.h"
  25. #include "firebase/auth.h"
  26. #endif
  27. class Authentication {
  28. public:
  29. static void onTokenRefreshResult(const firebase::Future<std::string>& completed_future, void* ref_data) {
  30. const MessageManagerLock lock(Thread::getCurrentThread());
  31. if (!lock.lockWasGained())
  32. return;
  33. if (completed_future.status() != firebase::kFutureStatusComplete) {
  34. LoadSave::writeErrorLog("Firebase getting token error: not complete");
  35. return;
  36. }
  37. if (completed_future.error()) {
  38. std::string error = "Firebase getting token error: error code ";
  39. LoadSave::writeErrorLog(error + std::to_string(completed_future.error()));
  40. return;
  41. }
  42. Authentication* reference = (Authentication*)ref_data;
  43. reference->setToken(*completed_future.result());
  44. }
  45. static void create() {
  46. if (firebase::App::GetInstance() != nullptr)
  47. return;
  48. firebase::AppOptions auth_app_options = firebase::AppOptions();
  49. auth_app_options.set_app_id("");
  50. auth_app_options.set_api_key("");
  51. auth_app_options.set_project_id("");
  52. firebase::App::Create(auth_app_options);
  53. }
  54. Authentication() : auth_(nullptr) { }
  55. void init() {
  56. if (auth_ == nullptr)
  57. auth_ = firebase::auth::Auth::GetAuth(firebase::App::GetInstance());
  58. }
  59. bool hasAuth() const { return auth_ != nullptr; }
  60. firebase::auth::Auth* auth() const { return auth_; }
  61. void setToken(const std::string& token) { token_ = token; }
  62. std::string token() const { return token_; }
  63. bool loggedIn() { return auth_ && auth_->current_user() != nullptr; }
  64. void refreshToken() {
  65. if (auth_ == nullptr || auth_->current_user() == nullptr)
  66. return;
  67. firebase::Future<std::string> future = auth_->current_user()->GetToken(false);
  68. future.OnCompletion(onTokenRefreshResult, this);
  69. }
  70. private:
  71. firebase::auth::Auth* auth_;
  72. std::string token_;
  73. };
  74. #else
  75. class Authentication {
  76. public:
  77. static void create() { }
  78. std::string token() { return ""; }
  79. bool loggedIn() { return false; }
  80. void refreshToken() { }
  81. };
  82. #endif