Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
feat: optimize login logic
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Dec 25, 2020
1 parent 3fc1f0d commit b3efd2a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
1 change: 0 additions & 1 deletion campusphere/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ exports.signApp = class signApp extends (
headers,
body: JSON.stringify({}),
})
log.object(this.headers)
if (res.headers.hasOwnProperty('set-cookie')) return true
const signQ = await res.json()
this.curTask = signQ.datas.unSignedTasks[0]
Expand Down
1 change: 0 additions & 1 deletion crawler/casLogIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ module.exports = async (school, user) => {
log.success(`用户${name}: Login Success`)
} else {
log.error(`${res.statusText}: ${name}`)
log.object(res.headers)
return null
}

Expand Down
50 changes: 26 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,38 @@ let cookie

// purely for handleCookie func
let storeCookiePath, sign

/* get|store|update cookie synchronizedly */
async function handleCookie() {
for (let i of users) {
storeCookiePath = `cookie.${i.alias || i.username}`
await handleLogin(i, conf.get(storeCookiePath))
}
}

async function handleLogin(i, storedCookie) {
const name = i.alias || i.username

if (!conf.get(storeCookiePath)) {
await reLogin(i)
// Check if the cookie is user-provided
if (!i.cookie) {
// Check if the cookie is stored
if (!storedCookie) {
cookie = await login(school, i)
storeCookie(storeCookiePath, i, cookie)
} else {
storeCookie(storeCookiePath, i)
}

// Check if the cookie is eligible, if not, reLogin 1 more time
sign = new signApp(school, i)
const isNeedLogIn = await sign.signInfo(cookie)
if (isNeedLogIn) {
await reLogin(i)
try {
cookie.campusphere
} catch (err) {
log.error(`${name}: exit(1)`)
}
log.warning(`${name}: cookie is not eligible, reLogin`)
cookie = await login(school, i)
storeCookie(storeCookiePath, i, cookie)
}
} else {
log.success(`${name}: Using user provided cookie`)
}
}

Expand All @@ -55,27 +67,17 @@ async function signIn(i) {
await sign.signWithForm()
}

async function reLogin(i) {
function storeCookie(path, i, set) {
const name = i.alias || i.username

if (!i.cookie) {
cookie = await login(school, i)
if (cookie) {
conf.set(storeCookiePath, cookie)
log.success(`${name}: Cookie stored to local storage`)
}
if (set) {
conf.set(storeCookiePath, set)
log.success(`${name}: Cookie stored to local storage`)
} else {
cookie = { campusphere: i.cookie }
log.success(`${name}: Using user provided cookie`)
cookie = conf.get(path)
log.success(`${name}: Using stored cookie`)
}
}

function storeCookie(path, i) {
const name = i.alias || i.username
cookie = conf.get(path)
log.success(`${name}: Using stored cookie`)
}

async function sleep(timeout) {
return new Promise(r => setTimeout(r, timeout * 1000 * 60))
}
Expand Down
4 changes: 3 additions & 1 deletion init.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ class School {
school = new School(conf).init()
}
if (process.argv[2].match(/(rm|--remove)/)) {
conf.delete(process.argv[3])
const target = process.argv[3]
if (target === 'all') conf.clear()
conf.delete(target)
}
})()

0 comments on commit b3efd2a

Please sign in to comment.