171 lines
4.3 KiB
JavaScript
171 lines
4.3 KiB
JavaScript
import {
|
|
buildUserFromRegistrationApplication,
|
|
normalizeUser
|
|
} from './flow'
|
|
|
|
function getText(language, zhText, enText) {
|
|
return String(language || 'zh').trim().toLowerCase() === 'en' ? enText : zhText
|
|
}
|
|
|
|
function getResponseData(response = {}) {
|
|
if (response && response.data && typeof response.data === 'object') {
|
|
return response.data
|
|
}
|
|
return response || {}
|
|
}
|
|
|
|
export function matchRegisteredUser(user = {}, account = '') {
|
|
const source = user || {}
|
|
const currentAccount = String(account || '').trim()
|
|
const candidateAccounts = [
|
|
source.account,
|
|
source.username,
|
|
source.email
|
|
].map(value => String(value || '').trim()).filter(Boolean)
|
|
|
|
if (!currentAccount || !candidateAccounts.length) {
|
|
return null
|
|
}
|
|
|
|
if (!candidateAccounts.includes(currentAccount)) {
|
|
return null
|
|
}
|
|
|
|
return source
|
|
}
|
|
|
|
export function pickLatestRegistration(response = {}, options = {}) {
|
|
const source = options || {}
|
|
const account = String(source.account || '').trim()
|
|
const responseData = getResponseData(response)
|
|
const list = Array.isArray(responseData.data)
|
|
? responseData.data
|
|
: (Array.isArray(responseData) ? responseData : [])
|
|
|
|
const matchedList = list.filter(item => {
|
|
if (!item) {
|
|
return false
|
|
}
|
|
return item.account === account || item.email === account
|
|
})
|
|
const targetList = matchedList.length ? matchedList : list
|
|
|
|
return targetList[0] || null
|
|
}
|
|
|
|
export function extractLoginSession(response = {}, language = 'zh') {
|
|
const responseData = getResponseData(response)
|
|
const nestedData = responseData && responseData.data && typeof responseData.data === 'object'
|
|
? responseData.data
|
|
: {}
|
|
const token = responseData.accessToken
|
|
|| responseData.token
|
|
|| nestedData.accessToken
|
|
|| nestedData.token
|
|
|| response.accessToken
|
|
|| response.token
|
|
|| ''
|
|
const expires = responseData.expires
|
|
|| responseData.expireTime
|
|
|| nestedData.expires
|
|
|| nestedData.expireTime
|
|
|| response.expires
|
|
|| response.expireTime
|
|
|| ''
|
|
|
|
if (!token) {
|
|
throw new Error(getText(language, '登录成功但未返回 accessToken', 'Login succeeded but accessToken is missing'))
|
|
}
|
|
|
|
return {
|
|
token,
|
|
expires
|
|
}
|
|
}
|
|
|
|
export function buildHandledLoginError(language = 'zh', options = {}) {
|
|
const source = options || {}
|
|
return {
|
|
title: source.title || '',
|
|
navigateToAuthResult: source.navigateToAuthResult === true
|
|
}
|
|
}
|
|
|
|
export function buildLoginSessionUser(options = {}) {
|
|
const source = options || {}
|
|
const latestRegistration = source.latestRegistration || null
|
|
const matchedLocalUser = source.matchedLocalUser || null
|
|
const account = source.account || ''
|
|
const password = source.password || ''
|
|
const language = source.language || 'zh'
|
|
|
|
if (latestRegistration) {
|
|
return buildUserFromRegistrationApplication(latestRegistration, {
|
|
password,
|
|
language
|
|
})
|
|
}
|
|
|
|
if (matchedLocalUser) {
|
|
return matchedLocalUser
|
|
}
|
|
|
|
return normalizeUser({
|
|
account,
|
|
username: account,
|
|
password
|
|
})
|
|
}
|
|
|
|
export function resolveLoginPreparation(options = {}) {
|
|
const source = options || {}
|
|
const language = source.language || 'zh'
|
|
const matchedLocalUser = source.matchedLocalUser || null
|
|
const latestRegistration = source.latestRegistration || null
|
|
const account = source.account || ''
|
|
const password = source.password || ''
|
|
|
|
if (!latestRegistration) {
|
|
return {
|
|
ok: true,
|
|
sessionUser: buildLoginSessionUser({
|
|
matchedLocalUser,
|
|
account,
|
|
password,
|
|
language
|
|
})
|
|
}
|
|
}
|
|
|
|
if (latestRegistration.approvalStatus === 0) {
|
|
return {
|
|
ok: false,
|
|
handledError: buildHandledLoginError(language, {
|
|
title: getText(language, '注册申请审核中,暂不能登录', 'Registration is under review'),
|
|
navigateToAuthResult: true
|
|
})
|
|
}
|
|
}
|
|
|
|
if (latestRegistration.approvalStatus === -1) {
|
|
return {
|
|
ok: false,
|
|
handledError: buildHandledLoginError(language, {
|
|
title: latestRegistration.approveReason || getText(language, '注册申请已驳回', 'Rejected'),
|
|
navigateToAuthResult: true
|
|
})
|
|
}
|
|
}
|
|
|
|
return {
|
|
ok: true,
|
|
sessionUser: buildLoginSessionUser({
|
|
latestRegistration,
|
|
matchedLocalUser,
|
|
account,
|
|
password,
|
|
language
|
|
})
|
|
}
|
|
}
|