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

@@ -10,7 +10,7 @@ import NProgress from 'nprogress'; //nprogress module
import Router from 'next/router';
import 'nprogress/nprogress.css';
import '../styles/reset.scss';
import { useToast } from '@/hooks/useToast';
import { useGlobalStore } from '@/store/global';
//Binding events.
Router.events.on('routeChangeStart', () => NProgress.start());
@@ -29,23 +29,20 @@ const queryClient = new QueryClient({
});
export default function App({ Component, pageProps }: AppProps) {
const { toast } = useToast();
// 校验是否支持 click 事件
const {
loadInitData,
initData: { googleVerKey }
} = useGlobalStore();
useEffect(() => {
if (typeof document.createElement('div').click !== 'function') {
toast({
title: '你的浏览器版本过低',
status: 'warning'
});
}
}, [toast]);
loadInitData();
}, []);
return (
<>
<Head>
<title>Fast GPT</title>
<meta name="description" content="Generated by Fast GPT" />
<meta
name="viewport"
content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no, viewport-fit=cover"
@@ -55,6 +52,12 @@ export default function App({ Component, pageProps }: AppProps) {
<Script src="/js/qrcode.min.js" strategy="lazyOnload"></Script>
<Script src="/js/pdf.js" strategy="lazyOnload"></Script>
<Script src="/js/html2pdf.bundle.min.js" strategy="lazyOnload"></Script>
{googleVerKey && (
<Script
src={`https://www.recaptcha.net/recaptcha/api.js?render=${googleVerKey}`}
strategy="lazyOnload"
></Script>
)}
<Script src="/js/particles.js"></Script>
<QueryClientProvider client={queryClient}>
<ChakraProvider theme={theme}>

View File

@@ -62,5 +62,5 @@ export function gpt_chatItemTokenSlice({
}
}
return result;
return result.length === 0 && messages[0] ? [messages[0]] : result;
}

View File

@@ -2,10 +2,16 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
export type InitDateResponse = {
beianText: string;
googleVerKey: string;
};
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
jsonRes(res, {
jsonRes<InitDateResponse>(res, {
data: {
beianText: process.env.SAFE_BEIAN_TEXT || ''
beianText: process.env.SAFE_BEIAN_TEXT || '',
googleVerKey: process.env.CLIENT_GOOGLE_VER_TOKEN || ''
}
});
}

View File

@@ -7,15 +7,29 @@ import { sendPhoneCode, sendEmailCode } from '@/service/utils/sendNote';
import { UserAuthTypeEnum } from '@/constants/common';
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('123456789', 6);
import { authGoogleToken } from '@/utils/plugin/google';
import requestIp from 'request-ip';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { username, type } = req.query as { username: string; type: `${UserAuthTypeEnum}` };
const { username, type, googleToken } = req.body as {
username: string;
type: `${UserAuthTypeEnum}`;
googleToken: string;
};
if (!username || !type) {
throw new Error('缺少参数');
}
// google auth
process.env.SERVICE_GOOGLE_VER_TOKEN &&
(await authGoogleToken({
secret: process.env.SERVICE_GOOGLE_VER_TOKEN,
response: googleToken,
remoteip: requestIp.getClientIp(req) || undefined
}));
await connectToDatabase();
const code = nanoid();

View File

@@ -291,7 +291,7 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => {
}, 100);
try {
await gptChatPrompt(newChatList.slice(-2));
await gptChatPrompt(newChatList.slice(newChatList.length - 2));
} catch (err: any) {
toast({
title: typeof err === 'string' ? err : err?.message || '聊天出错了~',

View File

@@ -2,8 +2,6 @@ import React, { useEffect } from 'react';
import { Card, Box, Link, Flex, Image, Button } from '@chakra-ui/react';
import Markdown from '@/components/Markdown';
import { useMarkdown } from '@/hooks/useMarkdown';
import { getFilling } from '@/api/system';
import { useQuery } from '@tanstack/react-query';
import { useRouter } from 'next/router';
import { useGlobalStore } from '@/store/global';
@@ -13,7 +11,10 @@ const Home = () => {
const router = useRouter();
const { inviterId } = router.query as { inviterId: string };
const { data } = useMarkdown({ url: '/intro.md' });
const { isPc } = useGlobalStore();
const {
isPc,
initData: { beianText }
} = useGlobalStore();
useEffect(() => {
if (inviterId) {
@@ -21,8 +22,6 @@ const Home = () => {
}
}, [inviterId]);
const { data: { beianText = '' } = {} } = useQuery(['init'], getFilling);
/* 加载动画 */
useEffect(() => {
setTimeout(() => {

View File

@@ -124,7 +124,8 @@ const InputDataModal = ({
<ModalCloseButton />
<Box
display={['block', 'flex']}
display={'flex'}
flexDirection={['column', 'row']}
flex={'1 0 0'}
h={['100%', 0]}
overflow={'overlay'}