feat(protocol): register, refactor: authOrThrow
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user