Skip to content

Commit

Permalink
fix: login customdata 转为 object 类型
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoyafng committed Oct 12, 2024
1 parent 2504efc commit 49797f0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
9 changes: 8 additions & 1 deletion packages/guard-core-v4/src/Login/core/withPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,18 @@ export const LoginWithPassword = (props: LoginWithPasswordProps) => {
let password = loginData.password
let captchaCode = loginData.captchaCode && loginData.captchaCode.trim()

let keyValueArray = getUserRegisterParams() || []

const customData = keyValueArray.reduce((acc: any, curr) => {
acc[curr.key] = curr.value
return acc
}, {})

let body = {
account: account,
password: await encrypt!(password, props.publicKey),
captchaCode,
customData: config?.isHost ? getUserRegisterParams() : undefined,
customData: config?.isHost ? customData : undefined,
autoRegister: props.autoRegister,
withCustomData: false,
agreementIds: agreements.length
Expand Down
16 changes: 14 additions & 2 deletions packages/guard-core-v4/src/Login/core/withVerifyCode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,16 @@ const LoginWithVerifyCode = (props: any) => {
}, [form, currentMethod, captchaCheck])

const loginByPhoneCode = async (values: any) => {
let keyValueArray = getUserRegisterParams() || []

const customData = keyValueArray.reduce((acc: any, curr) => {
acc[curr.key] = curr.value
return acc
}, {})
const reqContent: any = {
phone: values.phoneNumber,
code: values.code,
customData: config?.isHost ? getUserRegisterParams() : undefined,
customData: config?.isHost ? customData : undefined,
autoRegister: autoRegister,
withCustomData: false,
agreementIds: agreements.length ? acceptedAgreementIds.current : undefined
Expand Down Expand Up @@ -324,10 +330,16 @@ const LoginWithVerifyCode = (props: any) => {

// 邮箱验证码登录
const loginByEmailCode = async (values: any) => {
let keyValueArray = getUserRegisterParams() || []

const customData = keyValueArray.reduce((acc: any, curr) => {
acc[curr.key] = curr.value
return acc
}, {})
const reqContent = {
email: values.identify,
code: values.code,
customData: config?.isHost ? getUserRegisterParams() : undefined,
customData: config?.isHost ? customData : undefined,
autoRegister: autoRegister,
withCustomData: false,
agreementIds: agreements.length ? acceptedAgreementIds.current : undefined
Expand Down
45 changes: 29 additions & 16 deletions packages/guard-core-v4/src/_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,42 +210,55 @@ export function deepMerge<T extends object = any>(
/**
* @description 在托管页下上传query.login_page_context中指定的用户自定义字段进行补全(/oidc/auth发起的认证只会携带 login_page_context)
*/

export const getUserRegisterParams = () => {
let customData: any[] = []
let customData: Array<{ key: string; value: any }> = []

// 解析查询字符串
const query = qs.parse(window.location.search, {
ignoreQueryPrefix: true
ignoreQueryPrefix: true // 去掉 '?' 前缀
})

let loginPageContext: any = query.login_page_context

// loginPageContext 从字符串转换为 JSON 对象
// 尝试将 loginPageContext 从字符串转换为 JSON 对象
if (typeof loginPageContext === 'string') {
try {
loginPageContext = JSON.parse(loginPageContext)
} catch (error) {
console.error('Invalid JSON in login_page_context:', error)
}
}
const type = Object.prototype.toString.call(loginPageContext)
if (loginPageContext && type.includes('Array')) {
// 遍历数组 b,将每个对象的 key-value 转换为 { key: keyName, value: keyValue } 的形式

// 提取键值对函数,避免代码重复
const extractKeyValue = (obj: any) => {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
customData.push({ key: key, value: obj[key] })
}
}
}

// 判断 loginPageContext 是数组还是对象
if (Array.isArray(loginPageContext)) {
// 遍历数组,将每个对象转换为 { key, value }
loginPageContext.forEach((item: any) => {
let obj = item
try {
obj = JSON.parse(item)
const obj = typeof item === 'string' ? JSON.parse(item) : item
extractKeyValue(obj)
} catch (error) {
console.error('Invalid JSON in login_page_context:', error)
}
for (let key in obj) {
customData.push({ key: key, value: obj[key] })
console.error('Invalid JSON in array item:', error)
}
})
} else if (loginPageContext && type.includes('Object')) {
for (let key in loginPageContext) {
customData.push({ key: key, value: loginPageContext[key] })
}
} else if (
typeof loginPageContext === 'object' &&
loginPageContext !== null
) {
// 如果是对象,直接提取键值对
extractKeyValue(loginPageContext)
}

// 返回自定义数据
return customData
}

Expand Down

0 comments on commit 49797f0

Please sign in to comment.