Skip to content

Commit ce834b0

Browse files
authored
Store user token (#14)
1 parent 908d2cc commit ce834b0

4 files changed

+30
-17
lines changed

BackendlessUser.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
BasicBackendlessRegisterUser::BasicBackendlessRegisterUser(
1212
QString _email,
1313
QString _password
14-
): email(_email), password(_password) {
14+
): email(new StringPostParam(_email)), password(new StringPostParam(_password)) {
15+
}
16+
17+
BasicBackendlessRegisterUser::~BasicBackendlessRegisterUser() {
18+
delete email;
19+
delete password;
1520
}
1621

1722
PostParams BasicBackendlessRegisterUser::getAllParams() {
1823
return {
19-
{"email", new StringPostParam(email)},
20-
{"password", new StringPostParam(password)}
24+
{"email", email},
25+
{"password", password}
2126
};
2227
}

BackendlessUser.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ struct BasicBackendlessRegisterUser: BackendlessRegisterUserRepresentable {
2727
QString _email,
2828
QString _password
2929
);
30+
~BasicBackendlessRegisterUser() override;
3031

3132
PostParams getAllParams() override;
3233

3334
protected:
34-
QString email;
35-
QString password;
35+
StringPostParam* email;
36+
StringPostParam* password;
3637
};
3738

3839
struct BackendlessSignInUser {

BackendlessUserAPI.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ BackendlessUserAPI::BackendlessUserAPI(QNetworkAccessManager* _networkAccessMana
2222
apiKey(_apiKey),
2323
endpoint(_endpoint) {
2424
readTokenFromDisk();
25-
qDebug() << userToken;
25+
qDebug() << userTokenValue;
2626
}
2727

2828
void BackendlessUserAPI::registerUser(BackendlessRegisterUserRepresentable& user) {
@@ -72,7 +72,7 @@ void BackendlessUserAPI::signInUser(QString login, QString password) {
7272
extractResult<BackendlessSignInUser>(
7373
replyValue,
7474
[&](auto user) {
75-
userToken = user.userToken;
75+
userTokenValue = user.userToken;
7676
saveTokenOnDisk();
7777
emit signInUserSuccess(user);
7878
},
@@ -88,24 +88,25 @@ void BackendlessUserAPI::signInUser(QString login, QString password) {
8888
);
8989
}
9090

91+
QString BackendlessUserAPI::tokenFilePath() {
92+
auto path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
93+
return path + "/backendless_token.txt";
94+
}
95+
9196
void BackendlessUserAPI::readTokenFromDisk() {
92-
auto path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
93-
auto fileName = path + "/backendless_token.txt";
94-
QFile file(fileName);
97+
QFile file(tokenFilePath());
9598
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
9699
QTextStream stream(&file);
97-
stream >> userToken;
100+
stream >> userTokenValue;
98101
}
99102
file.close();
100103
}
101104

102105
void BackendlessUserAPI::saveTokenOnDisk() {
103-
auto path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
104-
auto fileName = path + "/backendless_token.txt";
105-
QFile file(fileName);
106+
QFile file(tokenFilePath());
106107
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
107108
QTextStream stream(&file);
108-
stream << userToken;
109+
stream << userTokenValue;
109110
}
110111
file.close();
111112
}
@@ -114,7 +115,7 @@ void BackendlessUserAPI::validateUserToken() {
114115
request(
115116
networkAccessManager,
116117
this,
117-
endpoint + appId + "/" + apiKey + "/users/isvalidusertoken/" + userToken,
118+
endpoint + appId + "/" + apiKey + "/users/isvalidusertoken/" + userTokenValue,
118119
{
119120

120121
},
@@ -161,3 +162,7 @@ void BackendlessUserAPI::restorePassword(QString email) {
161162
}
162163
);
163164
}
165+
166+
QString BackendlessUserAPI::userToken() {
167+
return userTokenValue;
168+
}

BackendlessUserAPI.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ class BackendlessUserAPI: public QObject, public BasicAPI {
2828
void signInUser(QString, QString);
2929
void validateUserToken();
3030
void restorePassword(QString);
31+
QString userToken();
3132

3233
private:
34+
QString tokenFilePath();
3335
void readTokenFromDisk();
3436
void saveTokenOnDisk();
3537

@@ -58,7 +60,7 @@ class BackendlessUserAPI: public QObject, public BasicAPI {
5860
QString appId;
5961
QString apiKey;
6062
QString endpoint;
61-
QString userToken;
63+
QString userTokenValue;
6264
};
6365

6466
#endif

0 commit comments

Comments
 (0)