feat: google auth

This commit is contained in:
archer
2023-05-24 14:37:01 +08:00
parent 116e9c8d85
commit c99d6998ea
16 changed files with 150 additions and 151 deletions

View File

@@ -0,0 +1,33 @@
import axios from 'axios';
import { Obj2Query } from '../tools';
export const getClientToken = (googleVerKey: string) => {
if (!grecaptcha?.ready) return '';
return new Promise<string>((resolve, reject) => {
grecaptcha.ready(async () => {
try {
const token = await grecaptcha.execute(googleVerKey, {
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 }>(
`https://www.recaptcha.net/recaptcha/api/siteverify?${Obj2Query(data)}`
);
if (res.data.score && res.data.score >= 0.9) {
return Promise.resolve('');
}
return Promise.reject('非法环境');
};