@saltcorn/mobile-app 1.7.0-alpha.0 → 1.7.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -2
- package/share_extension_files/Info.plist +20 -2
- package/share_extension_files/ShareViewController.swift +117 -88
- package/src/helpers/api.js +67 -35
- package/src/helpers/auth.js +127 -116
- package/src/helpers/db_schema.js +6 -4
- package/src/helpers/file_system.js +3 -0
- package/src/helpers/offline_mode.js +9 -13
- package/src/index.js +5 -5
- package/src/init.js +41 -42
- package/src/routing/mocks/request.js +1 -1
- package/src/routing/routes/notifications.js +34 -10
- package/src/routing/routes/view.js +9 -3
- package/src/routing/utils.js +1 -1
- package/www/js/iframe_view_utils.js +131 -19
package/src/helpers/auth.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/*global saltcorn*/
|
|
2
2
|
|
|
3
|
-
import { jwtDecode } from "jwt-decode";
|
|
4
3
|
import i18next from "i18next";
|
|
5
4
|
import { apiCall } from "./api";
|
|
6
5
|
import { router } from "../routing/index";
|
|
@@ -15,50 +14,57 @@ import {
|
|
|
15
14
|
} from "./common";
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
|
-
*
|
|
17
|
+
* fetches a CSRF token and caches it in mobileConfig.csrfToken.
|
|
18
|
+
* Call again after every login/logout, since both reset it.
|
|
19
|
+
*/
|
|
20
|
+
export async function refreshCsrfToken() {
|
|
21
|
+
const response = await apiCall({ method: "GET", path: "/auth/csrf-token" });
|
|
22
|
+
const config = saltcorn.data.state.getState().mobileConfig;
|
|
23
|
+
config.csrfToken = response?.data?.csrfToken;
|
|
24
|
+
return config.csrfToken;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* internal helper for the normal login/signup
|
|
19
29
|
* @param {any} param0
|
|
20
30
|
* @returns
|
|
21
31
|
*/
|
|
22
|
-
async function loginRequest({ email, password, isSignup,
|
|
23
|
-
|
|
32
|
+
async function loginRequest({ email, password, isSignup, code }) {
|
|
33
|
+
await refreshCsrfToken();
|
|
34
|
+
const opts = code
|
|
24
35
|
? {
|
|
25
|
-
method: "
|
|
26
|
-
path: "/auth/
|
|
36
|
+
method: "POST",
|
|
37
|
+
path: "/auth/session-from-code",
|
|
38
|
+
body: { code },
|
|
27
39
|
}
|
|
28
40
|
: isSignup
|
|
29
41
|
? {
|
|
30
42
|
method: "POST",
|
|
31
43
|
path: "/auth/signup",
|
|
32
|
-
body: {
|
|
33
|
-
email,
|
|
34
|
-
password,
|
|
35
|
-
},
|
|
44
|
+
body: { email, password },
|
|
36
45
|
}
|
|
37
46
|
: {
|
|
38
47
|
method: "POST",
|
|
39
|
-
path: "/auth/login-with/
|
|
40
|
-
body: {
|
|
41
|
-
email,
|
|
42
|
-
password,
|
|
43
|
-
},
|
|
48
|
+
path: "/auth/login-with/session",
|
|
49
|
+
body: { email, password },
|
|
44
50
|
};
|
|
45
51
|
const response = await apiCall(opts);
|
|
46
52
|
return response.data;
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
/**
|
|
50
|
-
* internal helper to process a
|
|
51
|
-
*
|
|
56
|
+
* internal helper to process a successful session login response
|
|
57
|
+
* ({ success: true, user }) and persist it locally
|
|
52
58
|
*/
|
|
53
|
-
const
|
|
54
|
-
const token = jwtDecode(tokenStr);
|
|
55
|
-
const user = token.user;
|
|
59
|
+
const handleSessionLogin = async (user, config) => {
|
|
56
60
|
config.user = user;
|
|
57
61
|
config.isPublicUser = false;
|
|
58
62
|
config.isOfflineMode = false;
|
|
63
|
+
config.hasSession = true;
|
|
59
64
|
await insertUser(user);
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
// login regenerated the session, so the previously fetched token is stale
|
|
66
|
+
await refreshCsrfToken();
|
|
67
|
+
await persistSession(config);
|
|
62
68
|
i18next.changeLanguage(user.language);
|
|
63
69
|
};
|
|
64
70
|
|
|
@@ -101,22 +107,22 @@ const getEntryPoint = (config) => {
|
|
|
101
107
|
};
|
|
102
108
|
|
|
103
109
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
110
|
+
* Normal login/signup use email and password. An OAuth-provider login
|
|
111
|
+
* (e.g. google-auth's finishLogin) instead passes the short-lived code
|
|
112
|
+
* its deep-link callback was handed, which stands in for a session here.
|
|
106
113
|
* @param {*} param0
|
|
107
114
|
*/
|
|
108
115
|
export async function login({ email, password, isSignup, token }) {
|
|
109
|
-
const loginResult =
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (typeof loginResult === "string") {
|
|
116
|
+
const loginResult = await loginRequest({
|
|
117
|
+
email,
|
|
118
|
+
password,
|
|
119
|
+
isSignup,
|
|
120
|
+
code: token,
|
|
121
|
+
});
|
|
122
|
+
if (loginResult?.success && loginResult?.user) {
|
|
117
123
|
const alerts = [];
|
|
118
124
|
const config = saltcorn.data.state.getState().mobileConfig;
|
|
119
|
-
await
|
|
125
|
+
await handleSessionLogin(loginResult.user, config);
|
|
120
126
|
if (config.allowOfflineMode) alerts.push(await initialSync(config));
|
|
121
127
|
await tryInitPush(config);
|
|
122
128
|
await tryInitBackgroundSync(config);
|
|
@@ -129,14 +135,31 @@ export async function login({ email, password, isSignup, token }) {
|
|
|
129
135
|
});
|
|
130
136
|
|
|
131
137
|
// open first page
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
try {
|
|
139
|
+
const entryPoint = getEntryPoint(config);
|
|
140
|
+
addRoute({ route: entryPoint, query: undefined });
|
|
141
|
+
const page = await router.resolve({
|
|
142
|
+
pathname: entryPoint,
|
|
143
|
+
fullWrap: true,
|
|
144
|
+
alerts,
|
|
145
|
+
});
|
|
146
|
+
if (page.content) await replaceIframe(page.content, page.isFile);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
// Logged in fine, but this user can't open the app's entry point
|
|
149
|
+
// (e.g. its role doesn't allow it) - say so instead of leaving
|
|
150
|
+
// them stuck on the login page with no explanation.
|
|
151
|
+
showToasts([
|
|
152
|
+
{
|
|
153
|
+
type: "error",
|
|
154
|
+
msg: i18next.t("Logged in as %s, but unable to open the app: %s", {
|
|
155
|
+
postProcess: "sprintf",
|
|
156
|
+
sprintf: [config.user.email, error.message || "unknown error"],
|
|
157
|
+
}),
|
|
158
|
+
},
|
|
159
|
+
]);
|
|
160
|
+
}
|
|
161
|
+
} else if (loginResult?.error) {
|
|
162
|
+
showToasts([{ type: "error", msg: loginResult.error }]);
|
|
140
163
|
} else if (loginResult?.alerts) {
|
|
141
164
|
showToasts(loginResult?.alerts);
|
|
142
165
|
} else {
|
|
@@ -146,44 +169,39 @@ export async function login({ email, password, isSignup, token }) {
|
|
|
146
169
|
|
|
147
170
|
export async function publicLogin(entryPoint) {
|
|
148
171
|
try {
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
} else if (loginResult?.alerts) {
|
|
183
|
-
showToasts(loginResult?.alerts);
|
|
184
|
-
} else {
|
|
185
|
-
throw new Error("The login failed.");
|
|
186
|
-
}
|
|
172
|
+
const config = saltcorn.data.state.getState().mobileConfig;
|
|
173
|
+
// no login needed for public, but a CSRF token is still cached upfront.
|
|
174
|
+
await refreshCsrfToken();
|
|
175
|
+
config.user = {
|
|
176
|
+
role_id: 100,
|
|
177
|
+
email: "public",
|
|
178
|
+
language: "en",
|
|
179
|
+
};
|
|
180
|
+
config.isPublicUser = true;
|
|
181
|
+
config.hasSession = true;
|
|
182
|
+
await persistSession(config);
|
|
183
|
+
i18next.changeLanguage(config.user.language);
|
|
184
|
+
addRoute({
|
|
185
|
+
route: entryPoint,
|
|
186
|
+
query: undefined,
|
|
187
|
+
});
|
|
188
|
+
const page = await router.resolve({
|
|
189
|
+
pathname: entryPoint,
|
|
190
|
+
fullWrap: true,
|
|
191
|
+
alerts: [
|
|
192
|
+
{
|
|
193
|
+
type: "success",
|
|
194
|
+
msg: i18next.t("Welcome to %s!", {
|
|
195
|
+
postProcess: "sprintf",
|
|
196
|
+
sprintf: [
|
|
197
|
+
saltcorn.data.state.getState().getConfig("site_name") ||
|
|
198
|
+
"Saltcorn",
|
|
199
|
+
],
|
|
200
|
+
}),
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
});
|
|
204
|
+
if (page.content) await replaceIframe(page.content, page.isFile);
|
|
187
205
|
} catch (error) {
|
|
188
206
|
console.error(error);
|
|
189
207
|
showToasts([
|
|
@@ -203,9 +221,11 @@ export async function logout() {
|
|
|
203
221
|
await tryStopBackgroundSync();
|
|
204
222
|
const response = await apiCall({ method: "GET", path: "/auth/logout" });
|
|
205
223
|
if (response.data.success) {
|
|
206
|
-
await
|
|
224
|
+
await clearSession();
|
|
207
225
|
clearHistory();
|
|
208
|
-
config.
|
|
226
|
+
config.csrfToken = undefined;
|
|
227
|
+
config.hasSession = false;
|
|
228
|
+
config.isPublicUser = false;
|
|
209
229
|
const page = await router.resolve({
|
|
210
230
|
pathname: "get/auth/login",
|
|
211
231
|
entryView: config.entry_point,
|
|
@@ -232,43 +252,34 @@ export async function insertUser({ id, email, role_id, language }) {
|
|
|
232
252
|
);
|
|
233
253
|
}
|
|
234
254
|
|
|
235
|
-
export async function
|
|
236
|
-
await saltcorn.data.db.deleteWhere("
|
|
255
|
+
export async function clearSession() {
|
|
256
|
+
await saltcorn.data.db.deleteWhere("session_table");
|
|
237
257
|
}
|
|
238
258
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
async function
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
const config = saltcorn.data.state.getState().mobileConfig;
|
|
252
|
-
await setJwt(response.data);
|
|
253
|
-
config.jwt = response.data;
|
|
254
|
-
}
|
|
255
|
-
} catch (e) {
|
|
256
|
-
console.error("JWT renewal failed:", e.message);
|
|
257
|
-
}
|
|
259
|
+
/**
|
|
260
|
+
* persists the current mobileConfig session state (csrf token, user,
|
|
261
|
+
* public/authenticated flag) so the app can restore it at next boot
|
|
262
|
+
* without a network call (used e.g. when starting offline).
|
|
263
|
+
*/
|
|
264
|
+
export async function persistSession(config) {
|
|
265
|
+
await clearSession();
|
|
266
|
+
await saltcorn.data.db.insert("session_table", {
|
|
267
|
+
csrfToken: config.csrfToken,
|
|
268
|
+
userJson: config.isPublicUser ? null : JSON.stringify(config.user),
|
|
269
|
+
isPublicUser: !!config.isPublicUser,
|
|
270
|
+
});
|
|
258
271
|
}
|
|
259
272
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return true;
|
|
273
|
-
} else return false;
|
|
273
|
+
/**
|
|
274
|
+
* checks with the server whether the current session cookie is still valid,
|
|
275
|
+
* and refreshes the cached CSRF token (its secret lives in the session, so
|
|
276
|
+
* it's fine to keep reusing the same token as long as the session is alive).
|
|
277
|
+
*/
|
|
278
|
+
export async function checkSession() {
|
|
279
|
+
const response = await apiCall({
|
|
280
|
+
method: "GET",
|
|
281
|
+
path: "/auth/authenticated",
|
|
282
|
+
timeout: 10000,
|
|
283
|
+
});
|
|
284
|
+
return !!response?.data?.authenticated;
|
|
274
285
|
}
|
package/src/helpers/db_schema.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*global saltcorn */
|
|
2
2
|
|
|
3
3
|
const historyFile = "update_history";
|
|
4
|
-
const
|
|
4
|
+
const sessionTableName = "session_table";
|
|
5
5
|
|
|
6
6
|
import { readFile, fileExists, writeJSON } from "./file_system";
|
|
7
7
|
import { Directory } from "@capacitor/filesystem";
|
|
@@ -206,8 +206,10 @@ export async function updateDb(tablesJSON) {
|
|
|
206
206
|
});
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
export async function
|
|
210
|
-
await saltcorn.data.db.query(`CREATE TABLE IF NOT EXISTS ${
|
|
211
|
-
|
|
209
|
+
export async function createSessionTable() {
|
|
210
|
+
await saltcorn.data.db.query(`CREATE TABLE IF NOT EXISTS ${sessionTableName} (
|
|
211
|
+
csrfToken VARCHAR(500),
|
|
212
|
+
userJson VARCHAR(2000),
|
|
213
|
+
isPublicUser BOOLEAN
|
|
212
214
|
)`);
|
|
213
215
|
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Filesystem, Encoding, Directory } from "@capacitor/filesystem";
|
|
2
|
+
// side-effect only: registers the plugin so it works on the web platform too,
|
|
3
|
+
// same as Filesystem above (native platforms pick it up either way)
|
|
4
|
+
import "@capacitor/file-transfer";
|
|
2
5
|
|
|
3
6
|
export async function writeFile(name, directory, content) {
|
|
4
7
|
try {
|
|
@@ -59,11 +59,11 @@ const insertRemoteData = async (table, rows, syncTimestamp) => {
|
|
|
59
59
|
const ref = _sync_info_tbl_ref_,
|
|
60
60
|
last_modified = _sync_info_tbl_last_modified_;
|
|
61
61
|
await saltcorn.data.db.insert(tblName, rest, { replace: true });
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
// ref is inlined as a literal, not passed as a param, so numeric-looking
|
|
63
|
+
// refs like "19" don't get silently turned into "19.0" on iOS.
|
|
64
|
+
const { rows: infos } = await saltcorn.data.db.query(
|
|
65
|
+
`select 1 from "${saltcorn.data.db.sqlsanitize(tblName)}_sync_info"
|
|
66
|
+
where ref=${sqlRef(rest[pkName])}`
|
|
67
67
|
);
|
|
68
68
|
if (infos.length > 0) {
|
|
69
69
|
await saltcorn.data.db.query(
|
|
@@ -72,14 +72,10 @@ const insertRemoteData = async (table, rows, syncTimestamp) => {
|
|
|
72
72
|
where ref=${sqlRef(rest[pkName])}`
|
|
73
73
|
);
|
|
74
74
|
} else {
|
|
75
|
-
await saltcorn.data.db.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
last_modified: syncTimestamp,
|
|
80
|
-
deleted: false,
|
|
81
|
-
modified_local: false,
|
|
82
|
-
}
|
|
75
|
+
await saltcorn.data.db.query(
|
|
76
|
+
`insert into "${saltcorn.data.db.sqlsanitize(tblName)}_sync_info"
|
|
77
|
+
(ref, last_modified, deleted, modified_local)
|
|
78
|
+
values (${sqlRef(ref)}, ${syncTimestamp}, false, false)`
|
|
83
79
|
);
|
|
84
80
|
}
|
|
85
81
|
}
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { init } from "./init";
|
|
1
|
+
import { init, postShare } from "./init";
|
|
2
2
|
import * as api from "./helpers/api";
|
|
3
3
|
import * as auth from "./helpers/auth";
|
|
4
4
|
import * as common from "./helpers/common";
|
|
@@ -12,7 +12,7 @@ import { router } from "./routing/index";
|
|
|
12
12
|
let notifications = undefined;
|
|
13
13
|
try {
|
|
14
14
|
notifications = require("./helpers/notifications");
|
|
15
|
-
console.log("Notifications module available");
|
|
15
|
+
console.log("Notifications module available");
|
|
16
16
|
} catch (err) {
|
|
17
17
|
console.log("Notifications module not available");
|
|
18
18
|
}
|
|
@@ -21,9 +21,8 @@ try {
|
|
|
21
21
|
let backgroundSync = undefined;
|
|
22
22
|
try {
|
|
23
23
|
backgroundSync = require("./helpers/background_sync");
|
|
24
|
-
console.log("Background sync module available");
|
|
25
|
-
}
|
|
26
|
-
catch (err) {
|
|
24
|
+
console.log("Background sync module available");
|
|
25
|
+
} catch (err) {
|
|
27
26
|
console.log("Background sync module not available");
|
|
28
27
|
}
|
|
29
28
|
|
|
@@ -38,6 +37,7 @@ context.keys().forEach((key) => {
|
|
|
38
37
|
|
|
39
38
|
export const mobileApp = {
|
|
40
39
|
init,
|
|
40
|
+
postShare,
|
|
41
41
|
api,
|
|
42
42
|
auth,
|
|
43
43
|
common,
|
package/src/init.js
CHANGED
|
@@ -11,9 +11,9 @@ import {
|
|
|
11
11
|
createSyncInfoTables,
|
|
12
12
|
dbUpdateNeeded,
|
|
13
13
|
updateDb,
|
|
14
|
-
|
|
14
|
+
createSessionTable,
|
|
15
15
|
} from "./helpers/db_schema.js";
|
|
16
|
-
import { publicLogin,
|
|
16
|
+
import { publicLogin, checkSession } from "./helpers/auth.js";
|
|
17
17
|
import { router } from "./routing/index.js";
|
|
18
18
|
import {
|
|
19
19
|
replaceIframe,
|
|
@@ -30,7 +30,6 @@ import { readJSONCordova, readTextCordova } from "./helpers/file_system.js";
|
|
|
30
30
|
|
|
31
31
|
import i18next from "i18next";
|
|
32
32
|
import i18nextSprintfPostProcessor from "i18next-sprintf-postprocessor";
|
|
33
|
-
import { jwtDecode } from "jwt-decode";
|
|
34
33
|
|
|
35
34
|
import { Network } from "@capacitor/network";
|
|
36
35
|
import { App } from "@capacitor/app";
|
|
@@ -152,19 +151,34 @@ const handlePluginHeaders = async (plugins) => {
|
|
|
152
151
|
}
|
|
153
152
|
};
|
|
154
153
|
|
|
155
|
-
const
|
|
156
|
-
const rows = await saltcorn.data.db.select("
|
|
157
|
-
return rows?.length > 0 ? rows[0]
|
|
154
|
+
const getSession = async () => {
|
|
155
|
+
const rows = await saltcorn.data.db.select("session_table");
|
|
156
|
+
return rows?.length > 0 ? rows[0] : null;
|
|
158
157
|
};
|
|
159
158
|
|
|
160
|
-
const
|
|
161
|
-
if (!(await saltcorn.data.db.tableExists("
|
|
162
|
-
await
|
|
159
|
+
const initSession = async () => {
|
|
160
|
+
if (!(await saltcorn.data.db.tableExists("session_table"))) {
|
|
161
|
+
await createSessionTable();
|
|
163
162
|
} else {
|
|
164
|
-
const
|
|
165
|
-
if (
|
|
163
|
+
const row = await getSession();
|
|
164
|
+
if (row?.csrfToken) {
|
|
166
165
|
const state = saltcorn.data.state.getState();
|
|
167
|
-
state.mobileConfig.
|
|
166
|
+
state.mobileConfig.csrfToken = row.csrfToken;
|
|
167
|
+
state.mobileConfig.isPublicUser = !!row.isPublicUser;
|
|
168
|
+
state.mobileConfig.hasSession = true;
|
|
169
|
+
if (row.isPublicUser) {
|
|
170
|
+
state.mobileConfig.user = {
|
|
171
|
+
role_id: 100,
|
|
172
|
+
email: "public",
|
|
173
|
+
language: "en",
|
|
174
|
+
};
|
|
175
|
+
} else if (row.userJson) {
|
|
176
|
+
try {
|
|
177
|
+
state.mobileConfig.user = JSON.parse(row.userJson);
|
|
178
|
+
} catch {
|
|
179
|
+
// corrupted local row, fall back to a network check
|
|
180
|
+
}
|
|
181
|
+
}
|
|
168
182
|
}
|
|
169
183
|
}
|
|
170
184
|
};
|
|
@@ -210,7 +224,7 @@ const showErrorPage = async (error) => {
|
|
|
210
224
|
const onResume = async () => {
|
|
211
225
|
if (typeof saltcorn === "undefined") return;
|
|
212
226
|
const mobileConfig = saltcorn.data.state.getState().mobileConfig;
|
|
213
|
-
if (mobileConfig?.
|
|
227
|
+
if (mobileConfig?.hasSession) {
|
|
214
228
|
const netStatus = await Network.getStatus();
|
|
215
229
|
mobileConfig.networkState = netStatus.connectionType;
|
|
216
230
|
if (
|
|
@@ -226,8 +240,8 @@ const onResume = async () => {
|
|
|
226
240
|
await showErrorPage(error);
|
|
227
241
|
}
|
|
228
242
|
} else if (mobileConfig.networkState !== "none") {
|
|
229
|
-
if (!mobileConfig.isOfflineMode && !
|
|
230
|
-
await
|
|
243
|
+
if (!mobileConfig.isOfflineMode && !mobileConfig.isPublicUser) {
|
|
244
|
+
await checkSession();
|
|
231
245
|
}
|
|
232
246
|
if (mobileConfig.allowOfflineMode && mobileConfig.syncOnAppResume) {
|
|
233
247
|
try {
|
|
@@ -240,21 +254,6 @@ const onResume = async () => {
|
|
|
240
254
|
}
|
|
241
255
|
};
|
|
242
256
|
|
|
243
|
-
const isPublicJwt = (jwt) => {
|
|
244
|
-
try {
|
|
245
|
-
if (!jwt) return false;
|
|
246
|
-
const decoded = jwtDecode(jwt);
|
|
247
|
-
return decoded.sub === "public";
|
|
248
|
-
} catch (error) {
|
|
249
|
-
console.log(
|
|
250
|
-
`Unable to inspect '${jwt}': ${
|
|
251
|
-
error.message ? error.message : "Unknown error"
|
|
252
|
-
}`
|
|
253
|
-
);
|
|
254
|
-
return false;
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
|
|
258
257
|
const isPublicEntryPoint = async (entryPoint) => {
|
|
259
258
|
if (!entryPoint) return false;
|
|
260
259
|
try {
|
|
@@ -309,7 +308,7 @@ const notEmpty = (shareData) => {
|
|
|
309
308
|
}
|
|
310
309
|
};
|
|
311
310
|
|
|
312
|
-
const postShare = async (shareData) => {
|
|
311
|
+
export const postShare = async (shareData) => {
|
|
313
312
|
const page = await router.resolve({
|
|
314
313
|
pathname: "post/notifications/share",
|
|
315
314
|
shareData,
|
|
@@ -390,7 +389,7 @@ export async function init(mobileConfig) {
|
|
|
390
389
|
try {
|
|
391
390
|
const url = event.url;
|
|
392
391
|
if (url.startsWith("mobileapp://auth/callback")) {
|
|
393
|
-
const token = new URL(url).searchParams.get("
|
|
392
|
+
const token = new URL(url).searchParams.get("code");
|
|
394
393
|
const method = new URL(url).searchParams.get("method");
|
|
395
394
|
const methods = saltcorn.data.state.getState().auth_methods;
|
|
396
395
|
if (!methods[method])
|
|
@@ -429,7 +428,7 @@ export async function init(mobileConfig) {
|
|
|
429
428
|
await updateDb(tablesJSON);
|
|
430
429
|
}
|
|
431
430
|
await createSyncInfoTables(mobileConfig.synchedTables);
|
|
432
|
-
await
|
|
431
|
+
await initSession();
|
|
433
432
|
await state.refresh_tables();
|
|
434
433
|
await state.refresh_views();
|
|
435
434
|
await state.refresh_pages();
|
|
@@ -455,14 +454,16 @@ export async function init(mobileConfig) {
|
|
|
455
454
|
Network.addListener("networkStatusChange", networkChangeCallback);
|
|
456
455
|
|
|
457
456
|
const networkDisabled = state.mobileConfig.networkState === "none";
|
|
458
|
-
const
|
|
457
|
+
const hasSession = !!state.mobileConfig.hasSession;
|
|
458
|
+
const isPublicUser = !!state.mobileConfig.isPublicUser;
|
|
459
459
|
const alerts = [];
|
|
460
|
-
if (
|
|
461
|
-
|
|
460
|
+
if (
|
|
461
|
+
hasSession &&
|
|
462
|
+
!isPublicUser &&
|
|
463
|
+
(networkDisabled || (await checkSession()))
|
|
464
|
+
) {
|
|
465
|
+
// already logged in, continue - mobileConfig.user was restored by initSession()
|
|
462
466
|
const mobileConfig = state.mobileConfig;
|
|
463
|
-
const decodedJwt = jwtDecode(mobileConfig.jwt);
|
|
464
|
-
mobileConfig.user = decodedJwt.user;
|
|
465
|
-
mobileConfig.isPublicUser = false;
|
|
466
467
|
await i18next.changeLanguage(mobileConfig.user.language);
|
|
467
468
|
if (mobileConfig.allowOfflineMode) {
|
|
468
469
|
const { offlineUser } = (await getLastOfflineSession()) || {};
|
|
@@ -536,11 +537,9 @@ export async function init(mobileConfig) {
|
|
|
536
537
|
});
|
|
537
538
|
}
|
|
538
539
|
if (page.content) await replaceIframe(page.content, page.isFile);
|
|
539
|
-
} else if (
|
|
540
|
+
} else if (hasSession && isPublicUser) {
|
|
540
541
|
// already logged in as public
|
|
541
542
|
const config = state.mobileConfig;
|
|
542
|
-
config.user = { role_id: 100, email: "public", language: "en" };
|
|
543
|
-
config.isPublicUser = true;
|
|
544
543
|
i18next.changeLanguage(config.user.language);
|
|
545
544
|
const entryPoint = getEntryPoint(100, state, state.mobileConfig);
|
|
546
545
|
if (!entryPoint) await showLogin(alerts);
|
|
@@ -29,7 +29,7 @@ export function MobileRequest({
|
|
|
29
29
|
}),
|
|
30
30
|
isAuthenticated: () => {
|
|
31
31
|
const mobileCfg = saltcorn.data.state.getState().mobileConfig;
|
|
32
|
-
return mobileCfg && mobileCfg.
|
|
32
|
+
return mobileCfg && mobileCfg.hasSession && !mobileCfg.isPublicUser;
|
|
33
33
|
},
|
|
34
34
|
getLocale: () => {
|
|
35
35
|
const mobileCfg = saltcorn.data.state.getState().mobileConfig;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { wrapContents } from "../utils";
|
|
4
4
|
import { MobileRequest } from "../mocks/request";
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { checkSession } from "../../helpers/auth.js";
|
|
7
7
|
import { apiCall } from "../../helpers/api";
|
|
8
8
|
|
|
9
9
|
const buildErrMsg = (msg) => {
|
|
@@ -24,21 +24,45 @@ const buildSuccessMsg = (msg) => {
|
|
|
24
24
|
);
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
// files have a real type (e.g. image/png); text/link shares are "text/plain"
|
|
28
|
+
const isFileItem = (item) =>
|
|
29
|
+
item?.type && item.type !== "text/plain" && item.url;
|
|
30
|
+
|
|
27
31
|
export const postShare = async (context) => {
|
|
28
32
|
let content = "";
|
|
29
33
|
const mobileCfg = saltcorn.data.state.getState().mobileConfig;
|
|
30
34
|
if (mobileCfg.networkState === "none")
|
|
31
35
|
content = buildErrMsg("No network connection");
|
|
32
|
-
else if (!(await
|
|
33
|
-
content = buildErrMsg("Not authenticated");
|
|
36
|
+
else if (!(await checkSession())) content = buildErrMsg("Not authenticated");
|
|
34
37
|
else {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
body
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
try {
|
|
39
|
+
const { title, description, type, url, additionalItems } =
|
|
40
|
+
context.shareData || {};
|
|
41
|
+
let body = context.shareData;
|
|
42
|
+
if (isFileItem({ type, url })) {
|
|
43
|
+
const items = [{ title, type, url }, ...(additionalItems || [])];
|
|
44
|
+
// title is the real on-disk filename, location is where to find it
|
|
45
|
+
const files = [];
|
|
46
|
+
for (const item of items) {
|
|
47
|
+
const uploaded = await saltcorn.data.models.File.uploadFromPath(
|
|
48
|
+
item.url,
|
|
49
|
+
item.title || "shared-file",
|
|
50
|
+
item.type
|
|
51
|
+
);
|
|
52
|
+
files.push({ title: uploaded.filename, location: uploaded.location });
|
|
53
|
+
}
|
|
54
|
+
body = { description, files };
|
|
55
|
+
}
|
|
56
|
+
const response = await apiCall({
|
|
57
|
+
method: "POST",
|
|
58
|
+
path: "/notifications/share-handler",
|
|
59
|
+
body,
|
|
60
|
+
});
|
|
61
|
+
if (response.error) content = buildErrMsg(response.error);
|
|
62
|
+
else content = buildSuccessMsg("Shared successfully");
|
|
63
|
+
} catch (error) {
|
|
64
|
+
content = buildErrMsg(error.message || "Error sharing");
|
|
65
|
+
}
|
|
42
66
|
}
|
|
43
67
|
return await wrapContents(
|
|
44
68
|
saltcorn.markup.tags.div(
|