Skip to content

Commit

Permalink
feat: add storage recaptcha key
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Dec 23, 2022
1 parent d1c3429 commit 4b3ba16
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"cSpell.words": ["kotodama"],
"cSpell.words": [
"kotodama",
"recaptcha"
],
"i18n-ally.keystyle": "nested",
"i18n-ally.localesPaths": "locales",
"i18n-ally.sortKeys": true,
Expand Down
1 change: 1 addition & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ not-found: Not found

placeholder:
server_url: Server Url
recaptcha_v3_key: Recaptcha V3 Key (Client)
nickname: Nickname
username: Username
password: Password
Expand Down
1 change: 1 addition & 0 deletions locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ not-found: 未找到页面

placeholder:
server_url: Waline 服务端地址,如 https://waline.xx.xx
recaptcha_v3_key: Recaptcha V3 Key 客户端密钥
email: 邮箱
nickname: 昵称
username: 用户名
Expand Down
5 changes: 4 additions & 1 deletion src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export interface LoginParams {
* 2FA TODO
*/
code?: string
recaptchaV3Key?: string
/**
* recaptcha v3 verify response
*/
recaptchaV3?: string
}

export interface TokenData {
Expand Down
6 changes: 5 additions & 1 deletion src/composables/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { useUserStore } from '~/stores/user'

export interface LoginForm extends LoginParams {
serverURL: string
/**
* recaptcha v3 key
*/
recaptchaV3Key?: ''
}

export function useLogin(loginFormEl: Ref<HTMLFormElement>, loginForm: LoginForm) {
Expand All @@ -33,7 +37,7 @@ export function useLogin(loginFormEl: Ref<HTMLFormElement>, loginForm: LoginForm
const res = await login({
email: loginForm.email,
password: loginForm.password,
recaptchaV3Key: loginForm.recaptchaV3Key,
recaptchaV3: loginForm.recaptchaV3,
}, remember.value)
if (res && res.data && res.data.token) {
uStore.url = res.data.url
Expand Down
8 changes: 6 additions & 2 deletions src/modules/nprogress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type { UserModule } from '~/types'

export const install: UserModule = ({ isClient, router }) => {
if (isClient) {
router.beforeEach(() => { NProgress.start() })
router.afterEach(() => { NProgress.done() })
router.beforeEach(() => {
NProgress.start()
})
router.afterEach(() => {
NProgress.done()
})
}
}
17 changes: 15 additions & 2 deletions src/pages/login.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
import { VueRecaptcha } from 'vue-recaptcha'
import { getNsName } from '~/utils'
import { validUsername } from '~/utils/validate'
import { useUserStore } from '~/stores/user'
import type { LoginForm } from '~/composables/login'
Expand All @@ -17,6 +18,7 @@ const loginForm = reactive<LoginForm>({
code: '',
email: uStore.email,
password: '',
recaptchaV3: '',
recaptchaV3Key: '',
})
Expand Down Expand Up @@ -59,11 +61,11 @@ onMounted(() => {
const onVerify = (res: string) => {
if (!res)
return
loginForm.recaptchaV3Key = res
loginForm.recaptchaV3 = res
handleLogin()
}
const enableRecaptcha = ref(config.enableRecaptcha)
const enableRecaptcha = useStorage(getNsName('enableRecaptcha'), config.enableRecaptcha)
</script>

<template>
Expand Down Expand Up @@ -122,6 +124,17 @@ const enableRecaptcha = ref(config.enableRecaptcha)
</el-checkbox>
</el-form-item>

<el-form-item v-if="enableRecaptcha" prop="serverURL" required>
<el-input
ref="serverURLEl"
v-model="loginForm.recaptchaV3Key"
:placeholder="t('placeholder.recaptcha_v3_key')"
name="serverURL"
type="text"
autocomplete="on"
/>
</el-form-item>

<!-- <el-form-item>
<el-checkbox v-model="remember">{{ t("login.message") }}</el-checkbox>
</el-form-item> -->
Expand Down
13 changes: 8 additions & 5 deletions src/stores/user.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { acceptHMRUpdate, defineStore } from 'pinia'
import { namespace } from '~/utils'
import { getNsName } from '~/utils'

export const useUserStore = defineStore('user', () => {
/**
* waline server url
*/
const serverURL = useStorage(`${namespace}:serverURL`, '')
const token = useStorage(`${namespace}:token`, '')
const url = useStorage(`${namespace}:url`, '')
const email = useStorage(`${namespace}:email`, '')
const serverURL = useStorage(getNsName('serverURL'), '')
const token = useStorage(getNsName('token'), '')
const url = useStorage(getNsName('url'), '')
const email = useStorage(getNsName('email'), '')

const recaptchaV3Key = useStorage(getNsName('recaptchaV3Key'), '')

return {
token,
Expand All @@ -20,6 +22,7 @@ export const useUserStore = defineStore('user', () => {
url,

email,
recaptchaV3Key,
}
})

Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './helper'

export const namespace = 'kotodama'
export const getNsName = (name: string) => `${namespace}:${name}`

1 comment on commit 4b3ba16

@vercel
Copy link

@vercel vercel bot commented on 4b3ba16 Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.