Skip to content

Commit

Permalink
feat: new sms bind (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
thezzisu authored Dec 27, 2024
1 parent cb71d0c commit 723139b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.10.0
v22.12.0
3 changes: 3 additions & 0 deletions .yarn/versions/7cefad94.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@aoi-js/frontend": patch
"@aoi-js/server": patch
25 changes: 14 additions & 11 deletions apps/frontend/src/components/user/UserAuthSms.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
<VTextField
v-model="newPhone"
prepend-inner-icon="mdi-phone"
:readonly="codeSent"
:label="t('term.telephone')"
:rules="phoneRules"
/>
<div id="vaptcha"></div>
<VOtpInput v-if="token" v-model.trim="code" />
<VOtpInput v-if="codeSent" v-model.trim="code" />
</VCardText>
<VCardActions>
<VBtn
variant="elevated"
color="info"
:disabled="!token || !phoneValid || codeSent"
:loading="sendTask.isLoading.value"
@click="sendTask.execute(token)"
>
{{ t('action.send-otp') }}
</VBtn>
<VBtn
variant="elevated"
@click="updateTask.execute()"
Expand All @@ -22,7 +32,7 @@
</template>

<script setup lang="ts">
import { toRef } from 'vue'
import { computed, toRef } from 'vue'
import { useI18n } from 'vue-i18n'
import { useChangePhone } from '@/utils/user/sms'
Expand All @@ -33,15 +43,8 @@ const props = defineProps<{
const { t } = useI18n()
const { newPhone, code, token, sendTask, updateTask } = useChangePhone(toRef(props, 'userId'))
const phoneRules = [
(value: string) => {
const re = /^1\d{10}$/
if (re.test(value)) return true
return t('hint.violate-phone-rule')
}
]
const { newPhone, code, token, sendTask, updateTask, phoneRules, phoneValid, codeSent } =
useChangePhone(toRef(props, 'userId'))
</script>

<i18n>
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ action:
fullscreen: Fullscreen
recommend-problem: Recommend Problem
select: Select
send-otp: Send OTP

term:
content: Content
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/src/locales/zh-Hans.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ action:
fullscreen: 全屏
recommend-problem: 推荐题目
select: 选择
send-otp: 发送验证码

term:
content: 内容
Expand Down
17 changes: 14 additions & 3 deletions apps/frontend/src/utils/user/sms.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ref, type MaybeRef, toRef } from 'vue'
import { ref, type MaybeRef, toRef, computed } from 'vue'
import { useI18n } from 'vue-i18n'

import { useAsyncTask, withMessage } from '../async'
import { noMessage, useAsyncTask, withMessage } from '../async'
import { http } from '../http'
import { useVaptcha } from '../vaptcha'

Expand All @@ -11,11 +11,21 @@ export function useChangePhone(userId: MaybeRef<string>) {
const newPhone = ref('')
const code = ref('')
const userIdRef = toRef(userId)
const phoneRules = [
(value: string) => {
const re = /^1\d{10}$/
if (re.test(value)) return true
return t('hint.violate-phone-rule')
}
]
const phoneValid = computed(() => phoneRules.every((rule) => rule(newPhone.value) === true))
const app = useAppState()
const { token } = useVaptcha({ onPass: (token) => sendTask.execute(token) })
const { t } = useI18n()
const codeSent = ref(false)

const sendTask = useAsyncTask(async (token: string) => {
if (!phoneValid.value) return noMessage()
await http.post(`user/${userIdRef.value}/preBind`, {
json: {
provider: 'sms',
Expand All @@ -26,6 +36,7 @@ export function useChangePhone(userId: MaybeRef<string>) {
mfaToken: app.mfaToken
}
})
codeSent.value = true
return withMessage(t('msg.code-sent'))
})
const updateTask = useAsyncTask(async () => {
Expand All @@ -40,5 +51,5 @@ export function useChangePhone(userId: MaybeRef<string>) {
}
})
})
return { newPhone, code, token, sendTask, updateTask }
return { newPhone, code, token, sendTask, updateTask, phoneRules, phoneValid, codeSent }
}
2 changes: 1 addition & 1 deletion apps/server/src/utils/package.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import packageJson from '../../package.json' assert { type: 'json' }
import packageJson from '../../package.json' with { type: 'json' }

export { packageJson }
2 changes: 1 addition & 1 deletion docker/dockerfiles/server.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20-alpine
FROM node:22-alpine

ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
Expand Down

0 comments on commit 723139b

Please sign in to comment.