Skip to content

Commit 65bef13

Browse files
committed
add login
1 parent 164fab2 commit 65bef13

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

src/DataService.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@ export default function DataService() {
66
const url = deploy ? 'http://php-server-notes.std-1033.ist.mospolytech.ru/' : 'http://php-server-notes/'
77
//let recuestCount = 1;
88

9+
////////////////////////////////////////////////////////////
10+
function login() {
11+
let submit = null
12+
function tryLogin() {
13+
new Promise((res, rej) => {
14+
checkLogin(prompt("Введите логин", user || "")).then(res, rej)
15+
}).then(() => {
16+
submit()
17+
console.log('Login: ', user)
18+
}, tryLogin)
19+
}
20+
return new Promise((resolve) => {
21+
submit = resolve
22+
tryLogin()
23+
})
24+
}
25+
26+
function checkLogin(str) {
27+
return new Promise((res, rej) => {
28+
try {
29+
user = str.replace(/@|;|:|\.|,|\/|\\|\||\$|\?|!|#|%|\*|\^|\+|=|\[|\]| |\\ |«|<|>/gi, "").trim()
30+
user && user.length > 3 && user.length < 20 && user === str
31+
? res()
32+
: rej()
33+
} catch {
34+
rej()
35+
}
36+
})
37+
}
38+
////////////////////////////////////////////////////////////
39+
40+
////////////////////////////////////////////////////////////
941
function request(target, data) {
1042
//const rc = recuestCount++
1143
//console.log(` \nrequest ${rc} - "${target}" started \n params - user:"${user}" data:"${data}"`)
@@ -31,13 +63,6 @@ export default function DataService() {
3163
////////////////////////////////////////////////////////////
3264

3365
////////////////////////////////////////////////////////////
34-
function requestUser() {
35-
return request('ip', null).then(prom => {
36-
user = String(prom) || "default"
37-
console.log("user ", user)
38-
})
39-
}
40-
4166
function requestGetData() {
4267
return request('getData', null)
4368
}
@@ -68,7 +93,7 @@ export default function DataService() {
6893
function loadData() {
6994
return new Promise((res, rej) => {
7095
(user === null
71-
? requestUser(null)
96+
? login(null)
7297
: Promise.resolve(null))
7398
.then(() => requestGetData(null))
7499
.then((d) => {
@@ -81,13 +106,13 @@ export default function DataService() {
81106
}
82107

83108
function postData(postData) {
84-
(user === null
85-
? loadData(null)
86-
: Promise.resolve(null))
87-
.then((data) => {
88-
let pDat = postData === null ? (data || []) : postData
89-
requestPostData(pDat)
90-
})
109+
if (user !== null) {
110+
loadData()
111+
.then((data) => {
112+
let pDat = postData === null ? (data || []) : postData
113+
requestPostData(pDat)
114+
})
115+
}
91116
}
92117
////////////////////////////////////////////////////////////
93118

0 commit comments

Comments
 (0)