Created
May 16, 2017 06:11
-
-
Save liuzhuan/59ea715e7b07558fcdfa059e07f1ae46 to your computer and use it in GitHub Desktop.
Pseudo code for passport sdk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SESSION_STATUS = { | |
NOT_LOGIN: 1, | |
LOGIN: 2, | |
ACCESS_TOKEN_EXPIRED: 3, | |
REFRESH_TOKEN_EXPIRED: 4 | |
}; | |
let refresh_timer = null; | |
function checkAuth(callback) { | |
if (noToken) { | |
callback(SESSION_STATUS.NOT_LOGIN, null); | |
return; | |
} | |
if (expired()) { | |
refreshToken((error, token) => { | |
if (error == SESSION_STATUS.REFRESH_TOKEN_EXPIRED) { | |
jumpToLogin(); | |
return; | |
} | |
callback(null, token); | |
}); | |
return; | |
} | |
callback(null, token); | |
nextRefreshToken(); | |
} | |
function nextRefreshToken() { | |
clearTimeout(refresh_timer); | |
var t = getNextRefreshTime(); | |
refresh_timer = setTimeout(function(){ | |
refreshToken(null); | |
}, t); | |
} | |
function jumpToLogin() { | |
console.log('show me the login!'); | |
} | |
function refreshToken(callback) { | |
const valid = doRefresh(); | |
if (valid == false) { | |
callback(SESSION_STATUS.REFRESH_TOKEN_EXPIRED, null); | |
return; | |
} | |
updateLocalSession(session); | |
nextRefreshToken(); | |
callback(null, token); | |
} | |
function login() { | |
if (fail) { | |
console.log('login fail!'); | |
return; | |
} | |
updateLocalSession(); | |
nextRefreshToken(); | |
jumpBack(); | |
} | |
function jumpBack() { | |
console.log('Go back where you come!'); | |
} | |
function updateLocalSession(session) { | |
setExpiredTimestamp(); | |
wx.setStorageSync(key, session); | |
} | |
function setExpiredTimestamp() { | |
console.log('calculate the expired timestamp'); | |
} | |
function getExpiredTimestamp() { | |
return '1223'; | |
} | |
function expired() { | |
return expired?true: false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment