From c9f4bad7071709304f4bbc473e81f6f307b8940a Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Wed, 16 Aug 2023 18:48:38 +0800 Subject: [PATCH] fix: file --- client/src/utils/plugin/google.ts | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 client/src/utils/plugin/google.ts diff --git a/client/src/utils/plugin/google.ts b/client/src/utils/plugin/google.ts new file mode 100644 index 000000000..513883c71 --- /dev/null +++ b/client/src/utils/plugin/google.ts @@ -0,0 +1,37 @@ +import axios from 'axios'; +import { Obj2Query } from '../tools'; + +export const getClientToken = (googleClientVerKey?: string) => { + if (!googleClientVerKey || typeof window.grecaptcha === 'undefined' || !window.grecaptcha?.ready) + return ''; + return new Promise((resolve, reject) => { + window.grecaptcha.ready(async () => { + try { + const token = await window.grecaptcha.execute(googleClientVerKey, { + action: 'submit' + }); + resolve(token); + } catch (error) { + reject(error); + } + }); + }); +}; + +// service run +export const authGoogleToken = async (data: { + secret: string; + response: string; + remoteip?: string; +}) => { + const res = await axios.post<{ + score?: number; + success: boolean; + 'error-codes': string[]; + }>(`https://www.recaptcha.net/recaptcha/api/siteverify?${Obj2Query(data)}`); + + if (res.data.success) { + return Promise.resolve(''); + } + return Promise.reject(res?.data?.['error-codes']?.[0] || '非法环境'); +};