Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Dec 23, 2024
1 parent 31cea1f commit 2f14ca8
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/core/FirebaseApp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Created October 30, 2024
* Created December 23, 2024
*
* The MIT License (MIT)
* Copyright (c) 2024 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -102,42 +102,23 @@ namespace firebase
return false;
}

template <typename T = int>
bool parseItem(StringUtil &sut, const String &src, T &dest, const String &name, const String &delim, int &p1, int &p2)
{
sut.parse(src, name, delim, p1, p2);
if (p1 > -1 && p2 > -1)
{
if (src[p1] == '"')
p1++;
if (src[p2] == '"')
p2--;
dest = atoi(src.substring(p1, p2).c_str());
p1 = p2;
return true;
}
p1 = 0;
p2 = 0;
return false;
}

bool parseToken(const String &payload)
{
StringUtil sut;
int p1 = 0, p2 = 0;
auth_data.app_token.clear();
String token, refresh;
String token, refresh, str;

if (payload.indexOf("\"error\"") > -1)
{
String str;
if (parseItem(sut, payload, str, "\"error\"", auth_data.user_auth.auth_type == auth_user_id_token || auth_data.user_auth.auth_type == auth_sa_custom_token ? "}" : ",", p1, p2))
{
int code = 0;
str = "";
p1 = 0;
p2 = 0;
parseItem(sut, payload, code, "\"code\"", ",", p1, p2);
if (parseItem(sut, payload, str, "\"code\"", ",", p1, p2))
code = atoi(str.c_str());
str = "";
parseItem(sut, payload, str, "\"message\"", ",", p1, p2);
parseItem(sut, payload, str, "\"error_description\"", "}", p1, p2);
if (str[str.length() - 1] == '"')
Expand All @@ -156,12 +137,14 @@ namespace firebase
sut.trim(token);
parseItem(sut, payload, refresh, "\"refreshToken\"", ",", p1, p2);
sut.trim(refresh);
parseItem(sut, payload, auth_data.app_token.expire, "\"expiresIn\"", "}", p1, p2);
if (parseItem(sut, payload, str, "\"expiresIn\"", "}", p1, p2))
auth_data.app_token.expire = atoi(str.c_str());
}
}
else if (payload.indexOf("\"id_token\"") > -1)
{
parseItem(sut, payload, auth_data.app_token.expire, "\"expires_in\"", ",", p1, p2);
if (parseItem(sut, payload, str, "\"expires_in\"", ",", p1, p2))
auth_data.app_token.expire = atoi(str.c_str());
parseItem(sut, payload, refresh, "\"refresh_token\"", ",", p1, p2);
parseItem(sut, payload, token, "\"id_token\"", ",", p1, p2);
parseItem(sut, payload, auth_data.app_token.val[app_tk_ns::uid], "\"user_id\"", ",", p1, p2);
Expand All @@ -173,7 +156,8 @@ namespace firebase
{
if (parseItem(sut, payload, token, "\"access_token\"", ",", p1, p2))
{
parseItem(sut, payload, auth_data.app_token.expire, "\"expires_in\"", ",", p1, p2);
if (parseItem(sut, payload, str, "\"expires_in\"", ",", p1, p2))
auth_data.app_token.expire = atoi(str.c_str());
parseItem(sut, payload, auth_data.app_token.val[app_tk_ns::type], "\"token_type\"", "}", p1, p2);
}
}
Expand Down

0 comments on commit 2f14ca8

Please sign in to comment.