diff --git a/web/src/Setting.js b/web/src/Setting.js index 6b793c2a..5752064f 100644 --- a/web/src/Setting.js +++ b/web/src/Setting.js @@ -500,7 +500,7 @@ export function getFriendlyFileSize(size) { return `${num} ${"KMGTPEZY"[i - 1]}B`; } -function getRandomInt(s) { +function getHashInt(s) { let hash = 0; if (s.length !== 0) { for (let i = 0; i < s.length; i++) { @@ -510,16 +510,16 @@ function getRandomInt(s) { } } + if (hash < 0) { + hash = -hash; + } return hash; } export function getAvatarColor(s) { const colorList = ["#f56a00", "#7265e6", "#ffbf00", "#00a2ae"]; - let random = getRandomInt(s); - if (random < 0) { - random = -random; - } - return colorList[random % 4]; + const hash = getHashInt(s); + return colorList[hash % 4]; } export function getLanguage() { diff --git a/web/src/auth/Provider.js b/web/src/auth/Provider.js index 194c1679..d7d291ee 100644 --- a/web/src/auth/Provider.js +++ b/web/src/auth/Provider.js @@ -177,7 +177,9 @@ export function getAuthUrl(application, provider, method) { let endpoint = authInfo[provider.type].endpoint; const redirectUri = `${window.location.origin}/callback`; const scope = authInfo[provider.type].scope; - const state = Util.getStateFromQueryParams(application.name, provider.name, method); + + const isShortState = provider.type === "WeChat" && navigator.userAgent.includes("MicroMessenger"); + const state = Util.getStateFromQueryParams(application.name, provider.name, method, isShortState); if (provider.type === "Google") { return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; diff --git a/web/src/auth/Util.js b/web/src/auth/Util.js index d0bfc851..d57f8e65 100644 --- a/web/src/auth/Util.js +++ b/web/src/auth/Util.js @@ -126,15 +126,27 @@ export function getOAuthGetParameters(params) { } } -export function getStateFromQueryParams(applicationName, providerName, method) { +export function getStateFromQueryParams(applicationName, providerName, method, isShortState) { let query = window.location.search; query = `${query}&application=${applicationName}&provider=${providerName}&method=${method}`; if (method === "link") { query = `${query}&from=${window.location.pathname}`; } - return btoa(query); + + if (!isShortState) { + return btoa(query); + } else { + const state = providerName; + sessionStorage.setItem(state, query); + return state; + } } export function getQueryParamsFromState(state) { - return atob(state); + const query = sessionStorage.getItem(state); + if (query === null) { + return atob(state); + } else { + return query; + } }