-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhcaptcha.vue
175 lines (173 loc) · 4.78 KB
/
hcaptcha.vue
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!--
=========================================================
* © 2018-2022 Ronan LE MEILLAT for Association Highcanfly
=========================================================
This website use:
- Vite, Vue3, FontAwesome 6, TailwindCss 3
- Vue Notus theme from Creative Tim (MIT License)
- And many others
-->
<template>
<div id="hcap-script" />
</template>
<script>
import {loadApiEndpointIfNotAlready} from './hcaptcha-script';
export default {
name: 'VueHcaptcha',
props: {
sitekey: {
type: String,
required: true
},
theme: {
type: String,
default: undefined
},
size: {
type: String,
default: undefined
},
tabindex: {
type: String,
default: undefined
},
language: {
type: String,
default: undefined
},
reCaptchaCompat: {
type: Boolean,
default: true
},
challengeContainer: {
type: String,
default: undefined
},
rqdata: {
type: String,
default: undefined
},
sentry: {
type: Boolean,
default: true
},
apiEndpoint: {
type: String,
default: 'https://hcaptcha.com/1/api.js'
},
endpoint: {
type: String,
default: undefined
},
reportapi: {
type: String,
default: undefined
},
assethost: {
type: String,
default: undefined
},
imghost: {
type: String,
default: undefined
},
},
data: () => {
return {
widgetId: null,
hcaptcha: null,
renderedCb : null,
};
},
mounted() {
return loadApiEndpointIfNotAlready(this.$props).then(this.onApiLoaded).catch(this.onError);
},
unmounted() {
this.teardown();
},
methods: {
teardown() {
if (this.widgetId) {
this.hcaptcha.reset(this.widgetId);
this.hcaptcha.remove(this.widgetId);
}
},
onApiLoaded() {
this.hcaptcha = window.hcaptcha;
const opt = {
sitekey: this.sitekey,
theme: this.theme,
size: this.size,
tabindex: this.tabindex,
'callback': this.onVerify,
'expired-callback': this.onExpired,
'chalexpired-callback': this.onChallengeExpired,
'error-callback': this.onError,
'open-callback': this.onOpen,
'close-callback': this.onClose
};
if (this.challengeContainer) {
opt['challenge-container'] = this.challengeContainer;
}
this.widgetId = this.hcaptcha.render(this.$el, opt);
if (this.rqdata) {
this.hcaptcha.setData(this.widgetId, {rqdata: this.rqdata});
}
this.onRendered();
},
execute() {
if (this.widgetId) {
this.hcaptcha.execute(this.widgetId);
this.onExecuted();
} else {
// execute after el is rendered
// we use a custom cb since `$on` was removed in vue3
this.renderedCb = () => {
this.renderedCb = null;
this.execute();
};
}
},
reset() {
if (this.widgetId) {
this.hcaptcha.reset(this.widgetId);
this.onReset();
} else {
this.$emit('error', 'Element is not rendered yet and thus cannot reset it. Wait for `rendered` event to safely call reset.');
}
},
onRendered() {
this.$emit('rendered');
this.renderedCb && this.renderedCb();
},
onExecuted() {
this.$emit('executed');
},
onReset() {
this.$emit('reset');
},
onError(e) {
this.$emit('error', e);
this.reset();
},
onVerify() {
const token = this.hcaptcha.getResponse(this.widgetId);
const eKey = this.hcaptcha.getRespKey(this.widgetId);
this.$emit('verify', token, eKey);
},
onExpired() {
this.$emit('expired');
},
onChallengeExpired() {
// vue3 will transform this `camelCase` event name into `kebab-case`
this.$emit('challengeExpired');
},
onOpen() {
this.$emit('opened');
},
onClose() {
this.$emit('closed');
}
}
};
</script>