2023-10-23 23:33:39 +02:00
|
|
|
import { useEffect } from "react"
|
|
|
|
import { useRouter } from "next/router"
|
2022-03-16 12:18:09 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { api } from "../../tools/api"
|
|
|
|
import { Authentication, isTokens } from "../../tools/authentication"
|
|
|
|
import type { SocialMedia } from "../design/SocialMediaButton"
|
|
|
|
import { SocialMediaButton } from "../design/SocialMediaButton"
|
|
|
|
import { providers } from "../../models/OAuth"
|
2022-03-16 12:18:09 +01:00
|
|
|
|
|
|
|
export const AuthenticationSocialMedia: React.FC = () => {
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
const handleAuthentication = (
|
2023-10-23 23:33:39 +02:00
|
|
|
socialMedia: SocialMedia,
|
2022-03-16 12:18:09 +01:00
|
|
|
): (() => Promise<void>) => {
|
|
|
|
return async () => {
|
2023-10-23 23:33:39 +02:00
|
|
|
const redirect = window.location.href.replace(location.search, "")
|
2022-03-16 12:18:09 +01:00
|
|
|
const { data: url } = await api.get(
|
2023-10-23 23:33:39 +02:00
|
|
|
`/users/oauth2/${socialMedia.toLowerCase()}/signin?redirectURI=${redirect}`,
|
2022-03-16 12:18:09 +01:00
|
|
|
)
|
|
|
|
window.location.href = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const data = router.query
|
|
|
|
if (isTokens(data)) {
|
2022-08-24 17:22:55 +02:00
|
|
|
const authentication = new Authentication(data, true)
|
2022-03-16 12:18:09 +01:00
|
|
|
authentication.signin()
|
2023-10-23 23:33:39 +02:00
|
|
|
router.push("/application").catch(() => {})
|
2022-03-16 12:18:09 +01:00
|
|
|
}
|
|
|
|
}, [router])
|
|
|
|
|
|
|
|
return (
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="flex flex-col sm:w-full sm:items-center">
|
|
|
|
<div className="flex flex-col items-center justify-center space-y-6 sm:w-4/6 sm:flex-row sm:space-x-6 sm:space-y-0">
|
2022-04-08 20:59:04 +02:00
|
|
|
{providers.map((provider, index) => {
|
|
|
|
return (
|
|
|
|
<SocialMediaButton
|
|
|
|
key={index}
|
|
|
|
socialMedia={provider}
|
|
|
|
onClick={handleAuthentication(provider)}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})}
|
2022-03-16 12:18:09 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|