-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e89323a
commit 8eabd7a
Showing
6 changed files
with
217 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { API } from "@/utils/api"; | ||
import axios from "axios"; | ||
|
||
type ValidateCaptchaResponseType = { | ||
success: boolean; | ||
}; | ||
|
||
export function validateCaptchaResponse(response: string) { | ||
return axios.post<ValidateCaptchaResponseType>(API.verify(), { | ||
responseToken: response, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* This file is part of SudoBot Dashboard. | ||
* | ||
* Copyright (C) 2021-2023 OSN Developers. | ||
* | ||
* SudoBot Dashboard is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* SudoBot Dashboard is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with SudoBot Dashboard. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Captcha from "@/components/Verify/Captcha"; | ||
import { Metadata } from "next"; | ||
import Script from "next/script"; | ||
import { FC } from "react"; | ||
|
||
export const metadata: Metadata = { | ||
title: "Verify - SudoBot", | ||
description: "Log into SudoBot's control panel.", | ||
}; | ||
|
||
const VerifyPage: FC = () => { | ||
return ( | ||
<main className="min-h-[90vh] flex justify-center items-center"> | ||
<Script | ||
src="https://www.google.com/recaptcha/api.js" | ||
async | ||
defer | ||
></Script> | ||
|
||
<div> | ||
<h1 className="text-3xl md:text-4xl text-center pt-5 md:pt-0"> | ||
Verify | ||
</h1> | ||
<p className="text-center text-[#999] pb-[20px] md:pb-[50px] pt-3"> | ||
Please verify yourself to continue. | ||
</p> | ||
|
||
<div className="flex justify-center items-center"> | ||
<form | ||
method="POST" | ||
action="#" | ||
className="bg-[#222] rounded-lg p-3" | ||
> | ||
<div className="flex"> | ||
<Captcha /> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
}; | ||
|
||
export default VerifyPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"use client"; | ||
|
||
import { validateCaptchaResponse } from "@/api/routes/verify"; | ||
import { | ||
Alert, | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogContentText, | ||
DialogTitle, | ||
} from "@mui/material"; | ||
import { AxiosError } from "axios"; | ||
import { FC, useEffect, useState } from "react"; | ||
|
||
const recaptchaSuccessCallbackName = "recaptchaSuccess"; | ||
|
||
declare global { | ||
interface Window | ||
extends Record< | ||
typeof recaptchaSuccessCallbackName, | ||
Function | undefined | ||
> {} | ||
} | ||
|
||
const Captcha: FC = () => { | ||
const [isSuccess, setIsSuccess] = useState(false); | ||
const [isAlertOpen, setIsAlertOpen] = useState(true); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const onSuccess = async (responseToken: string) => { | ||
try { | ||
const response = await validateCaptchaResponse(responseToken); | ||
|
||
if (!response.data.success) { | ||
throw new AxiosError( | ||
"Request unsuccessful", | ||
response.status.toString(), | ||
undefined, | ||
undefined, | ||
response | ||
); | ||
} | ||
|
||
setIsSuccess(true); | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
setError( | ||
error.response?.data.error ?? | ||
"We were unable to verify you." | ||
); | ||
} else { | ||
console.log(error); | ||
setError( | ||
"An internal error has occurred. Please try again later." | ||
); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window[recaptchaSuccessCallbackName] = onSuccess; | ||
|
||
return () => { | ||
delete window[recaptchaSuccessCallbackName]; | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div className=" flex justify-center items-center w-[100%] flex-col gap-5"> | ||
{error && ( | ||
<Alert severity="error" className="w-[100%]"> | ||
{error} | ||
</Alert> | ||
)} | ||
{isSuccess && ( | ||
<Dialog | ||
open={isAlertOpen} | ||
onClose={() => setIsAlertOpen(false)} | ||
aria-labelledby="alert-dialog-title" | ||
aria-describedby="alert-dialog-description" | ||
> | ||
<DialogTitle id="alert-dialog-title"> | ||
{"Verification Completed"} | ||
</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText id="alert-dialog-description"> | ||
We've successfully verified that you're not a robot. | ||
You’ll be authorized shortly. | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={() => setIsAlertOpen(false)}> | ||
OK | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
)} | ||
<div | ||
className="g-recaptcha invert" | ||
data-sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY} | ||
data-callback="recaptchaSuccess" | ||
></div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Captcha; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters