add: verification page

This commit is contained in:
HeesungB 2022-12-01 17:33:51 +09:00
parent b4d3d51d8c
commit 3ebae75689
2 changed files with 43 additions and 4 deletions

View File

@ -1,15 +1,22 @@
import styles from "../styles/Home.module.css";
import { useEffect } from "react";
interface AuthResponse {
authUrl: string;
}
export default function Home() {
const handleSigninWithTwitter = async () => {
const { authUrl } = await (await fetch("/api/auth")).json();
const handleSignInWithTwitter = async () => {
const response: AuthResponse = await (await fetch("/api/auth")).json();
window.open(authUrl);
window.location.href = response.authUrl;
};
return (
<div className={styles.container}>
<button onClick={handleSigninWithTwitter}>Sign in with Twitter</button>
<div>
<button onClick={handleSignInWithTwitter}>Sign in with Twitter</button>
</div>
</div>
);
}

View File

@ -0,0 +1,32 @@
import { useEffect, useState } from "react";
interface AccessTokenResponse {
access_token: string;
}
export default function VerificationPage() {
const [accessToken, setAccessToken] = useState<string>();
const fetchAccessToken = async (state: string, code: string) => {
const response: AccessTokenResponse = await (
await fetch(`/api/auth/access-token?state=${state}&code=${code}`)
).json();
setAccessToken(response.access_token);
};
useEffect(() => {
const [, state, code] =
window.location.search.match(
/^(?=.*state=([^&]+)|)(?=.*code=([^&]+)|).+$/,
) || [];
fetchAccessToken(state, code);
}, []);
return (
<div>
<div>{accessToken}</div>
</div>
);
}