Skip to content

Commit 497883e

Browse files
authored
Add get_auth_token (#1)
* Android implementation of GetAuthToken * Add firebase/installations.h * Android Firebase Installations library * iOS Firebase Installations libs * Mention get_auth_token in documentation * tabs -> spaces * Add Gradle dependency * Add get_auth_token to API file * Rename auth token variables/functions for clarity
1 parent 21a372e commit 497883e

File tree

9 files changed

+64
-2
lines changed

9 files changed

+64
-2
lines changed

docs/index.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,14 @@ function init(self)
9292
print(err)
9393
return
9494
end
95+
9596
-- firebase is ready to use!
96-
end
97+
98+
-- installation auth token can be used for configuring test devices for A/B tests
99+
firebase.get_installation_auth_token(function(self, token)
100+
print("installation auth token is " .. token)
101+
end)
102+
end
97103
end
98104
```
99105

firebase/api/firebase.script_api

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@
1414
- name: error
1515
type: string
1616
desc: Error message if initialisation failed
17+
18+
- name: get_installation_auth_token
19+
type: function
20+
desc: Get the Firebase Installation auth token
21+
22+
parameters:
23+
- name: callback
24+
type: function
25+
desc: Function to invoke with the auth token (self, token)
1.22 MB
Binary file not shown.
331 KB
Binary file not shown.
1010 KB
Binary file not shown.
316 KB
Binary file not shown.
356 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dependencies {
22
implementation 'com.google.firebase:firebase-analytics:18.0.3'
3+
implementation 'com.google.firebase:firebase-installations:16.3.5'
34
implementation 'com.google.android.gms:play-services-base:17.6.0'
45
}

firebase/src/firebase.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#include "luautils.h"
1010
#include "platform_utils.h"
1111
#include "firebase/app.h"
12-
12+
#include "firebase/installations.h"
1313

1414
static firebase::App* firebase_app_;
15+
static dmScript::LuaCallbackInfo* g_InstallationAuthTokenCallback;
1516

1617
using namespace firebase;
1718

@@ -45,6 +46,50 @@ static int Firebase_Init(lua_State* L) {
4546
return 2;
4647
}
4748

49+
static int Firebase_GetInstallationAuthToken(lua_State* L) {
50+
DM_LUA_STACK_CHECK(L, 0);
51+
52+
g_InstallationAuthTokenCallback = dmScript::CreateCallback(L, 1);
53+
54+
auto* installations_object = installations::Installations::GetInstance(firebase::App::GetInstance());
55+
56+
installations_object->GetToken(false)
57+
.OnCompletion([](const Future< std::string >& completed_future) {
58+
if (!dmScript::IsCallbackValid(g_InstallationAuthTokenCallback))
59+
{
60+
dmLogWarning("Firebase installation auth token callback is not valid");
61+
return;
62+
}
63+
64+
if (dmScript::SetupCallback(g_InstallationAuthTokenCallback))
65+
{
66+
lua_State* L = dmScript::GetCallbackLuaContext(g_InstallationAuthTokenCallback);
67+
68+
if (completed_future.error() == 0) {
69+
lua_pushstring(L, completed_future.result()->c_str());
70+
int ret = lua_pcall(L, 2, 0, 0);
71+
if (ret != 0) {
72+
lua_pop(L, 1);
73+
}
74+
}
75+
else {
76+
dmLogError("%d: %s", completed_future.error(), completed_future.error_message());
77+
lua_pushnil(L);
78+
lua_pushstring(L, completed_future.error_message());
79+
int ret = lua_pcall(L, 3, 0, 0);
80+
if (ret != 0) {
81+
lua_pop(L, 2);
82+
}
83+
}
84+
dmScript::TeardownCallback(g_InstallationAuthTokenCallback);
85+
}
86+
87+
dmScript::DestroyCallback(g_InstallationAuthTokenCallback);
88+
g_InstallationAuthTokenCallback = 0;
89+
});
90+
return 0;
91+
}
92+
4893

4994
firebase::App* Firebase_GetFirebaseApp()
5095
{
@@ -67,6 +112,7 @@ static void LuaInit(lua_State* L) {
67112

68113
// push functions on the firebase global table
69114
lua_pushtablestringfunction(L, "init", Firebase_Init);
115+
lua_pushtablestringfunction(L, "get_installation_auth_token", Firebase_GetInstallationAuthToken);
70116

71117
lua_pop(L, 1); // pop "firebase" global table
72118
}

0 commit comments

Comments
 (0)