Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimize mail login ux #23

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .yarn/versions/d922b9d8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@aoi-js/frontend": patch
"@aoi-js/server": patch
39 changes: 11 additions & 28 deletions apps/frontend/src/pages/login/mail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,16 @@
@click:append="preLogin"
/>

<VTextField
v-if="emailSent"
v-model="code"
prepend-inner-icon="mdi-numeric"
:label="t('term.otp-code')"
:rules="codeRules"
/>
<VOtpInput v-if="emailSent" v-model="code" />
</VCardText>

<VCardActions>
<VCardActions v-if="emailSent">
<VBtn
:disabled="!emailSent"
:disabled="code.length !== 6"
:loading="isLoading"
type="submit"
color="primary"
block
size="large"
variant="flat"
>
{{ t('pages.signin') }}
Expand All @@ -51,6 +44,7 @@ const toast = useToast()
const email = ref('')
const emailIcon = ref('mdi-send')
const emailSent = ref(false)
const emailSending = ref(false)
const code = ref('')

const emailRules = [
Expand All @@ -61,19 +55,11 @@ const emailRules = [
}
]

const codeRules = [
(value: string) => {
const re = /^[0-9]{6}$/
if (re.test(value)) return true
return t('hint.violate-code-rule')
}
]

const isLoading = ref(false)

async function preLogin() {
if (emailSent.value) return
emailSent.value = true
if (emailSending.value) return
emailSending.value = true
emailIcon.value = 'mdi-send-clock'
try {
await http.post('auth/preLogin', {
Expand All @@ -86,11 +72,12 @@ async function preLogin() {
})
toast.success(t('hint.email-sent'))
emailIcon.value = 'mdi-send-check'
emailSent.value = true
} catch (err) {
toast.error(t('hint.email-send-failed', { msg: await prettyHTTPError(err) }))
emailSent.value = false
emailIcon.value = 'mdi-send'
}
emailSending.value = false
}

async function signin(ev: SubmitEventPromise) {
Expand All @@ -108,14 +95,10 @@ async function signin(ev: SubmitEventPromise) {
}
}
})
const { token, userId } = await resp.json<{ token?: string; userId?: string }>()
const { token } = await resp.json<{ token: string }>()
toast.success(t('hint.signin-success'))
if (token) {
login(token)
router.replace('/')
} else {
router.replace(`/initial?uid=${userId}`)
}
login(token)
router.replace('/')
} catch (err) {
toast.error(t('hint.signin-wrong-credentials'))
}
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/auth/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class MailAuthProvider extends BaseAuthProvider {
private async sendCode(key: string, mail: string, purpose: string) {
if (!this.checkWhitelist(mail)) throw httpErrors.badRequest('Email address not allowed')
const ttl = await cache.ttl(key)
if (ttl > 0) throw httpErrors.tooManyRequests('Too many requests')
if (ttl > 0) throw httpErrors.tooManyRequests(`Wait for ${Math.ceil(ttl / 1000)} seconds`)
const code = rnd.generate({ length: 6, charset: 'numeric' })
await cache.setx(key, { code, mail }, 5 * 60 * 1000)
const info = await this.transporter.sendMail({
Expand Down