Compare commits

...

2 Commits

Author SHA1 Message Date
CrescentLeaf
13edd23245 feat(protocol): register, refactor: authOrThrow 2025-11-08 18:03:19 +08:00
CrescentLeaf
bc386908f7 fix: typo 2025-11-08 18:03:19 +08:00
2 changed files with 53 additions and 7 deletions

View File

@@ -79,6 +79,19 @@ export default class LingChairClient {
access_token?: string, access_token?: string,
account?: string, account?: string,
password?: string, password?: string,
}) {
try {
this.authOrThrow(args)
return true
} catch (_) {
return false
}
}
async authOrThrow(args: {
refresh_token?: string,
access_token?: string,
account?: string,
password?: string,
}) { }) {
if ((!args.access_token && !args.refresh_token) && (!args.account && !args.password)) if ((!args.access_token && !args.refresh_token) && (!args.account && !args.password))
throw new Error('Access/Refresh token or account & password required') throw new Error('Access/Refresh token or account & password required')
@@ -90,9 +103,10 @@ export default class LingChairClient {
const re = await this.invoke('User.refreshAccessToken', { const re = await this.invoke('User.refreshAccessToken', {
refresh_token: args.refresh_token, refresh_token: args.refresh_token,
}) })
if (re.code == 200) { if (re.code == 200)
access_token = re.data!.access_token as string | undefined access_token = re.data!.access_token as string | undefined
} else return re else
throw new CallbackError(re)
} }
if (!access_token && (args.account && args.password)) { if (!access_token && (args.account && args.password)) {
@@ -100,15 +114,47 @@ export default class LingChairClient {
account: args.account, account: args.account,
password: crypto.createHash('sha256').update(args.password).digest('hex'), password: crypto.createHash('sha256').update(args.password).digest('hex'),
}) })
if (re.code == 200) { if (re.code == 200)
access_token = re.data!.access_token as string | undefined access_token = re.data!.access_token as string | undefined
} else return re else
throw new CallbackError(re)
} }
const re = await this.invoke('User.auth', { const re = await this.invoke('User.auth', {
access_token: access_token access_token: access_token
}) })
if (re.code == 200) this.access_token = access_token as string if (re.code == 200)
return re this.access_token = access_token as string
else
throw new CallbackError(re)
}
async register(args: {
nickname: string,
username?: string,
password: string,
}) {
try {
this.registerOrThrow(args)
return true
} catch (_) {
return false
}
}
async registerOrThrow({
nickname,
username,
password,
}: {
nickname: string,
username?: string,
password: string,
}) {
const re = await this.invoke('User.register', {
nickname,
username,
password,
})
if (re.code != 200)
throw new CallbackError(re)
} }
} }

View File

@@ -152,7 +152,7 @@ export default class UserApi extends BaseApi {
msg: "成功", msg: "成功",
code: 200, code: 200,
data: { data: {
userid: user.bean.id user_id: user.bean.id
}, },
} }
}) })