feat: migrate app routes and business modules
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
import { assertSmsCode } from '@/utils/auth/verification.js'
|
||||
import { resolveRuntimeMode, runtimeConfig } from '@/utils/runtime-config.js'
|
||||
import {
|
||||
assertAuthVerificationOperation,
|
||||
assertPasswordHash,
|
||||
normalizeOptionalValidToken,
|
||||
requireRemoteAuth,
|
||||
saveLogin
|
||||
} from './auth-contract.js'
|
||||
import { normalizeOptionalText } from './request-normalizers.js'
|
||||
import { normalizePhoneChangePayload } from './profile-contract.js'
|
||||
import {
|
||||
requestAuth,
|
||||
requestAuthVoid,
|
||||
requestStrict
|
||||
} from './request-client.js'
|
||||
|
||||
export const authApi = {
|
||||
async getCaptchaRequirement({ operationCode, subject }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
return requestAuth({
|
||||
url: `/genealogy/app/auth/verification/${encodeURIComponent(assertAuthVerificationOperation(operationCode))}/require`,
|
||||
method: 'GET',
|
||||
data: { tenantId: runtimeConfig.tenantId, subject }
|
||||
}, requestOptions)
|
||||
},
|
||||
|
||||
async sendSmsCode({ operationCode, phone, validToken }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
const normalizedValidToken = normalizeOptionalValidToken(validToken)
|
||||
return requestAuthVoid({
|
||||
url: `/genealogy/app/auth/sms/${encodeURIComponent(assertAuthVerificationOperation(operationCode))}/code`,
|
||||
method: 'POST',
|
||||
data: {
|
||||
tenantId: runtimeConfig.tenantId,
|
||||
grantType: 'sms',
|
||||
phone,
|
||||
...(normalizedValidToken ? { validToken: normalizedValidToken } : {})
|
||||
}
|
||||
}, requestOptions)
|
||||
},
|
||||
|
||||
async loginWithPassword({ phone, passwordHash, validToken }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
const normalizedValidToken = normalizeOptionalValidToken(validToken)
|
||||
const loginSession = await requestAuth({
|
||||
url: '/genealogy/app/auth/login',
|
||||
method: 'POST',
|
||||
data: {
|
||||
tenantId: runtimeConfig.tenantId,
|
||||
grantType: 'password',
|
||||
phone,
|
||||
password: assertPasswordHash(passwordHash),
|
||||
...(normalizedValidToken ? { validToken: normalizedValidToken } : {})
|
||||
}
|
||||
}, requestOptions)
|
||||
return saveLogin(loginSession)
|
||||
},
|
||||
|
||||
async loginWithSms({ phone, smsCode }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
const loginSession = await requestAuth({
|
||||
url: '/genealogy/app/auth/login/sms',
|
||||
method: 'POST',
|
||||
data: {
|
||||
tenantId: runtimeConfig.tenantId,
|
||||
grantType: 'sms',
|
||||
phone,
|
||||
smsCode: assertSmsCode(smsCode)
|
||||
}
|
||||
}, requestOptions)
|
||||
return saveLogin(loginSession)
|
||||
},
|
||||
|
||||
async registerWithPassword({ phone, passwordHash, smsCode, nickName }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
const normalizedNickName = normalizeOptionalText(nickName, '昵称')
|
||||
const loginSession = await requestAuth({
|
||||
url: '/genealogy/app/auth/register',
|
||||
method: 'POST',
|
||||
data: {
|
||||
tenantId: runtimeConfig.tenantId,
|
||||
grantType: 'password',
|
||||
phone,
|
||||
password: assertPasswordHash(passwordHash),
|
||||
smsCode: assertSmsCode(smsCode),
|
||||
...(normalizedNickName ? { nickName: normalizedNickName } : {})
|
||||
}
|
||||
}, requestOptions)
|
||||
return saveLogin(loginSession)
|
||||
},
|
||||
|
||||
async resetPassword({ phone, passwordHash, smsCode }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
return requestAuthVoid({
|
||||
url: '/genealogy/app/auth/password/reset',
|
||||
method: 'PUT',
|
||||
data: {
|
||||
tenantId: runtimeConfig.tenantId,
|
||||
grantType: 'password',
|
||||
phone,
|
||||
newPassword: assertPasswordHash(passwordHash),
|
||||
smsCode: assertSmsCode(smsCode)
|
||||
}
|
||||
}, requestOptions)
|
||||
},
|
||||
|
||||
async changePassword({ oldPasswordHash, newPasswordHash }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/password',
|
||||
method: 'PUT',
|
||||
data: {
|
||||
oldPassword: assertPasswordHash(oldPasswordHash),
|
||||
newPassword: assertPasswordHash(newPasswordHash)
|
||||
}
|
||||
}, {
|
||||
requireData: false,
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return null
|
||||
},
|
||||
|
||||
async deactivateAccount({ smsCode }, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/account/deactivate',
|
||||
method: 'POST',
|
||||
data: { smsCode: assertSmsCode(smsCode) }
|
||||
}, {
|
||||
requireData: false,
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return null
|
||||
},
|
||||
|
||||
async logout(requestOptions = {}) {
|
||||
if (resolveRuntimeMode() !== 'remote') return null
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/logout',
|
||||
method: 'DELETE'
|
||||
}, {
|
||||
requireData: false,
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return null
|
||||
},
|
||||
|
||||
async changePhone(payload, requestOptions = {}) {
|
||||
requireRemoteAuth()
|
||||
await requestStrict({
|
||||
url: '/genealogy/app/auth/phone',
|
||||
method: 'PUT',
|
||||
data: normalizePhoneChangePayload(payload)
|
||||
}, {
|
||||
requireData: false,
|
||||
requestController: requestOptions.requestController ?? null
|
||||
})
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user