Vital
Loading...
Searching...
No Matches
authentication.h
Go to the documentation of this file.
1#pragma once
2
3#if NDEBUG && !NO_AUTH
4
5#include "JuceHeader.h"
6#include "load_save.h"
7
8#if defined(__APPLE__)
9#include <firebase/app.h>
10#include <firebase/auth.h>
11#else
12#include "firebase/app.h"
13#include "firebase/auth.h"
14#endif
15
32class Authentication {
33public:
43 static void onTokenRefreshResult(const firebase::Future<std::string>& completed_future, void* ref_data) {
44 const MessageManagerLock lock(Thread::getCurrentThread());
45 if (!lock.lockWasGained())
46 return;
47
48 if (completed_future.status() != firebase::kFutureStatusComplete) {
49 LoadSave::writeErrorLog("Firebase getting token error: not complete");
50 return;
51 }
52
53 if (completed_future.error()) {
54 std::string error = "Firebase getting token error: error code ";
55 LoadSave::writeErrorLog(error + std::to_string(completed_future.error()));
56 return;
57 }
58
59 Authentication* reference = (Authentication*)ref_data;
60 reference->setToken(*completed_future.result());
61 }
62
69 static void create() {
70 if (firebase::App::GetInstance() != nullptr)
71 return;
72
73 firebase::AppOptions auth_app_options = firebase::AppOptions();
74 auth_app_options.set_app_id("");
75 auth_app_options.set_api_key("");
76 auth_app_options.set_project_id("");
77
78 firebase::App::Create(auth_app_options);
79 }
80
87 Authentication() : auth_(nullptr) { }
88
95 void init() {
96 if (auth_ == nullptr)
97 auth_ = firebase::auth::Auth::GetAuth(firebase::App::GetInstance());
98 }
99
104 bool hasAuth() const { return auth_ != nullptr; }
105
110 firebase::auth::Auth* auth() const { return auth_; }
111
120 void setToken(const std::string& token) { token_ = token; }
121
126 std::string token() const { return token_; }
127
132 bool loggedIn() { return auth_ && auth_->current_user() != nullptr; }
133
141 void refreshToken() {
142 if (auth_ == nullptr || auth_->current_user() == nullptr)
143 return;
144
145 firebase::Future<std::string> future = auth_->current_user()->GetToken(false);
146 future.OnCompletion(onTokenRefreshResult, this);
147 }
148
149private:
150 firebase::auth::Auth* auth_;
151 std::string token_;
152};
153
154#else
155
164public:
168 static void create() { }
169
174 std::string token() { return ""; }
175
180 bool loggedIn() { return false; }
181
185 void refreshToken() { }
186};
187
188#endif
A no-op stub implementation used when authentication is disabled.
Definition authentication.h:163
static void create()
No-op create method.
Definition authentication.h:168
bool loggedIn()
Always returns false, indicating no user is logged in.
Definition authentication.h:180
void refreshToken()
No-op token refresh method.
Definition authentication.h:185
std::string token()
Returns an empty token string.
Definition authentication.h:174
static void writeErrorLog(String error_log)
Appends an error message to an error log file.
Definition load_save.cpp:1140