summaryrefslogtreecommitdiff
path: root/packages/client/src/pages/admin/bot-protection.vue
blob: 82ab1553178d8775b97bcdd6f9dc05f143c5fcd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
<div>
	<FormSuspense :p="init">
		<div class="_formRoot">
			<FormRadios v-model="provider" class="_formBlock">
				<option :value="null">{{ $ts.none }} ({{ $ts.notRecommended }})</option>
				<option value="hcaptcha">hCaptcha</option>
				<option value="recaptcha">reCAPTCHA</option>
			</FormRadios>

			<template v-if="provider === 'hcaptcha'">
				<FormInput v-model="hcaptchaSiteKey" class="_formBlock">
					<template #prefix><i class="fas fa-key"></i></template>
					<template #label>{{ $ts.hcaptchaSiteKey }}</template>
				</FormInput>
				<FormInput v-model="hcaptchaSecretKey" class="_formBlock">
					<template #prefix><i class="fas fa-key"></i></template>
					<template #label>{{ $ts.hcaptchaSecretKey }}</template>
				</FormInput>
				<FormSlot class="_formBlock">
					<template #label>{{ $ts.preview }}</template>
					<MkCaptcha provider="hcaptcha" :sitekey="hcaptchaSiteKey || '10000000-ffff-ffff-ffff-000000000001'"/>
				</FormSlot>
			</template>
			<template v-else-if="provider === 'recaptcha'">
				<FormInput v-model="recaptchaSiteKey" class="_formBlock">
					<template #prefix><i class="fas fa-key"></i></template>
					<template #label>{{ $ts.recaptchaSiteKey }}</template>
				</FormInput>
				<FormInput v-model="recaptchaSecretKey" class="_formBlock">
					<template #prefix><i class="fas fa-key"></i></template>
					<template #label>{{ $ts.recaptchaSecretKey }}</template>
				</FormInput>
				<FormSlot v-if="recaptchaSiteKey" class="_formBlock">
					<template #label>{{ $ts.preview }}</template>
					<MkCaptcha provider="recaptcha" :sitekey="recaptchaSiteKey"/>
				</FormSlot>
			</template>

			<FormButton primary @click="save"><i class="fas fa-save"></i> {{ $ts.save }}</FormButton>
		</div>
	</FormSuspense>
</div>
</template>

<script lang="ts">
import { defineAsyncComponent, defineComponent } from 'vue';
import FormRadios from '@/components/form/radios.vue';
import FormInput from '@/components/form/input.vue';
import FormButton from '@/components/ui/button.vue';
import FormSuspense from '@/components/form/suspense.vue';
import FormSlot from '@/components/form/slot.vue';
import * as os from '@/os';
import * as symbols from '@/symbols';
import { fetchInstance } from '@/instance';

export default defineComponent({
	components: {
		FormRadios,
		FormInput,
		FormButton,
		FormSuspense,
		FormSlot,
		MkCaptcha: defineAsyncComponent(() => import('@/components/captcha.vue')),
	},

	emits: ['info'],

	data() {
		return {
			[symbols.PAGE_INFO]: {
				title: this.$ts.botProtection,
				icon: 'fas fa-shield-alt'
			},
			provider: null,
			enableHcaptcha: false,
			hcaptchaSiteKey: null,
			hcaptchaSecretKey: null,
			enableRecaptcha: false,
			recaptchaSiteKey: null,
			recaptchaSecretKey: null,
		}
	},

	methods: {
		async init() {
			const meta = await os.api('meta', { detail: true });
			this.enableHcaptcha = meta.enableHcaptcha;
			this.hcaptchaSiteKey = meta.hcaptchaSiteKey;
			this.hcaptchaSecretKey = meta.hcaptchaSecretKey;
			this.enableRecaptcha = meta.enableRecaptcha;
			this.recaptchaSiteKey = meta.recaptchaSiteKey;
			this.recaptchaSecretKey = meta.recaptchaSecretKey;

			this.provider = this.enableHcaptcha ? 'hcaptcha' : this.enableRecaptcha ? 'recaptcha' : null;

			this.$watch(() => this.provider, () => {
				this.enableHcaptcha = this.provider === 'hcaptcha';
				this.enableRecaptcha = this.provider === 'recaptcha';
			});
		},
	
		save() {
			os.apiWithDialog('admin/update-meta', {
				enableHcaptcha: this.enableHcaptcha,
				hcaptchaSiteKey: this.hcaptchaSiteKey,
				hcaptchaSecretKey: this.hcaptchaSecretKey,
				enableRecaptcha: this.enableRecaptcha,
				recaptchaSiteKey: this.recaptchaSiteKey,
				recaptchaSecretKey: this.recaptchaSecretKey,
			}).then(() => {
				fetchInstance();
			});
		}
	}
});
</script>