mirror of
https://gitlab.com/comunic/comunicterm
synced 2024-11-16 10:31:06 +00:00
Can set login tokens from command line
This commit is contained in:
parent
c48659057e
commit
daa4a24941
@ -39,20 +39,39 @@ LoginResult AccountHelper::Login(const std::string &email, const std::string &pa
|
||||
AccountHelper::mToken1 = tokens.at("token1").as_string();
|
||||
AccountHelper::mToken2 = tokens.at("token2").as_string();
|
||||
|
||||
if(!refreshUserID())
|
||||
return ERROR;
|
||||
|
||||
// Get current user ID
|
||||
auto response = ApiRequest("user/getCurrentUserID", true).exec();
|
||||
if(response.code() != 200) return ERROR;
|
||||
AccountHelper::mUserID = response.object().at("userID").as_number().to_int32();
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
bool AccountHelper::signedIn()
|
||||
{
|
||||
return mToken1.length() > 0 && mToken2.length() > 0 && mUserID > 0;
|
||||
}
|
||||
|
||||
bool AccountHelper::refreshUserID()
|
||||
{
|
||||
// Get current user ID
|
||||
auto response = ApiRequest("user/getCurrentUserID", true).exec();
|
||||
if(response.code() != 200) return false;
|
||||
AccountHelper::mUserID = response.object().at("userID").as_number().to_int32();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int AccountHelper::userID()
|
||||
{
|
||||
return mUserID;
|
||||
}
|
||||
|
||||
void AccountHelper::setLoginTokens(std::vector<string> tokens)
|
||||
{
|
||||
AccountHelper::mToken1 = tokens[0];
|
||||
AccountHelper::mToken2 = tokens[1];
|
||||
}
|
||||
|
||||
vector<string> AccountHelper::loginTokens()
|
||||
{
|
||||
auto tokens = vector<string>();
|
||||
|
@ -25,8 +25,12 @@ public:
|
||||
const std::string &pass);
|
||||
|
||||
|
||||
static bool signedIn();
|
||||
|
||||
static bool refreshUserID();
|
||||
static int userID();
|
||||
|
||||
static void setLoginTokens(std::vector<std::string> tokens);
|
||||
static std::vector<std::string> loginTokens();
|
||||
|
||||
private:
|
||||
|
35
main.cpp
35
main.cpp
@ -21,19 +21,50 @@ int main(int argc, char **argv)
|
||||
cout << "Usage: " << endl;
|
||||
cout << "* help : Show this help" << endl;
|
||||
cout << "* getTokens : Get login token ONLY" << endl;
|
||||
cout << "* useTokens : Input login tokens (do not show login form)" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get login tokens
|
||||
if(arg == "getTokens") {
|
||||
else if(arg == "getTokens") {
|
||||
cout << "Getting only login tokens..." << endl;
|
||||
onlyGetTokens = true;
|
||||
}
|
||||
|
||||
// Use existing login tokens
|
||||
else if(arg == "useTokens") {
|
||||
if(argc != 4) {
|
||||
cerr << "Need to specify tokens!" << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
// Set tokens
|
||||
vector<std::string> tokens;
|
||||
tokens.push_back(argv[2]);
|
||||
tokens.push_back(argv[3]);
|
||||
AccountHelper::setLoginTokens(tokens);
|
||||
|
||||
// Validate them
|
||||
if(!AccountHelper::refreshUserID()) {
|
||||
cerr << "Could not use login tokens!" << endl;
|
||||
return -3;
|
||||
}
|
||||
|
||||
// Start directly main main menu
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
else {
|
||||
cerr << "Unrecognized argument!" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// First, we need to sign in user
|
||||
if(!LoginScreen().exec(!onlyGetTokens)) {
|
||||
if(!AccountHelper::signedIn() && !LoginScreen().exec(!onlyGetTokens)) {
|
||||
cerr << "Could not sign in user!" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user