mirror of
https://github.com/stonith404/pingvin-share.git
synced 2024-11-15 11:50:34 +01:00
feat: auto redirect to oauth provider
This commit is contained in:
parent
8b3e28bac8
commit
7dc2e56fee
@ -4,6 +4,7 @@ import {
|
|||||||
Container,
|
Container,
|
||||||
createStyles,
|
createStyles,
|
||||||
Group,
|
Group,
|
||||||
|
Loader,
|
||||||
Paper,
|
Paper,
|
||||||
PasswordInput,
|
PasswordInput,
|
||||||
Stack,
|
Stack,
|
||||||
@ -15,7 +16,7 @@ import { useForm, yupResolver } from "@mantine/form";
|
|||||||
import { showNotification } from "@mantine/notifications";
|
import { showNotification } from "@mantine/notifications";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import React from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { TbInfoCircle } from "react-icons/tb";
|
import { TbInfoCircle } from "react-icons/tb";
|
||||||
import { FormattedMessage } from "react-intl";
|
import { FormattedMessage } from "react-intl";
|
||||||
import * as yup from "yup";
|
import * as yup from "yup";
|
||||||
@ -24,8 +25,8 @@ import useUser from "../../hooks/user.hook";
|
|||||||
import useTranslate from "../../hooks/useTranslate.hook";
|
import useTranslate from "../../hooks/useTranslate.hook";
|
||||||
import authService from "../../services/auth.service";
|
import authService from "../../services/auth.service";
|
||||||
import { getOAuthIcon, getOAuthUrl } from "../../utils/oauth.util";
|
import { getOAuthIcon, getOAuthUrl } from "../../utils/oauth.util";
|
||||||
import toast from "../../utils/toast.util";
|
|
||||||
import { safeRedirectPath } from "../../utils/router.util";
|
import { safeRedirectPath } from "../../utils/router.util";
|
||||||
|
import toast from "../../utils/toast.util";
|
||||||
|
|
||||||
const useStyles = createStyles((theme) => ({
|
const useStyles = createStyles((theme) => ({
|
||||||
signInWith: {
|
signInWith: {
|
||||||
@ -74,7 +75,9 @@ const SignInForm = ({ redirectPath }: { redirectPath: string }) => {
|
|||||||
const { refreshUser } = useUser();
|
const { refreshUser } = useUser();
|
||||||
const { classes } = useStyles();
|
const { classes } = useStyles();
|
||||||
|
|
||||||
const [oauth, setOAuth] = React.useState<string[]>([]);
|
const [oauthProviders, setOauthProviders] = useState<string[] | null>(null);
|
||||||
|
const [isRedirectingToOauthProvider, setIsRedirectingToOauthProvider] =
|
||||||
|
useState(false);
|
||||||
|
|
||||||
const validationSchema = yup.object().shape({
|
const validationSchema = yup.object().shape({
|
||||||
emailOrUsername: yup.string().required(t("common.error.field-required")),
|
emailOrUsername: yup.string().required(t("common.error.field-required")),
|
||||||
@ -118,15 +121,36 @@ const SignInForm = ({ redirectPath }: { redirectPath: string }) => {
|
|||||||
.catch(toast.axiosError);
|
.catch(toast.axiosError);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAvailableOAuth = async () => {
|
useEffect(() => {
|
||||||
const oauth = await authService.getAvailableOAuth();
|
authService
|
||||||
setOAuth(oauth.data);
|
.getAvailableOAuth()
|
||||||
};
|
.then((providers) => {
|
||||||
|
setOauthProviders(providers.data);
|
||||||
React.useEffect(() => {
|
if (
|
||||||
getAvailableOAuth().catch(toast.axiosError);
|
providers.data.length === 1 &&
|
||||||
|
config.get("oauth.disablePassword")
|
||||||
|
) {
|
||||||
|
setIsRedirectingToOauthProvider(true);
|
||||||
|
router.push(
|
||||||
|
getOAuthUrl(config.get("general.appUrl"), providers.data[0]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(toast.axiosError);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
if (!oauthProviders) return null;
|
||||||
|
|
||||||
|
if (isRedirectingToOauthProvider)
|
||||||
|
return (
|
||||||
|
<Group align="center" position="center">
|
||||||
|
<Loader size="sm" />
|
||||||
|
<Text align="center">
|
||||||
|
<FormattedMessage id="common.text.redirecting" />
|
||||||
|
</Text>
|
||||||
|
</Group>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container size={420} my={40}>
|
<Container size={420} my={40}>
|
||||||
<Title order={2} align="center" weight={900}>
|
<Title order={2} align="center" weight={900}>
|
||||||
@ -170,7 +194,7 @@ const SignInForm = ({ redirectPath }: { redirectPath: string }) => {
|
|||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
)}
|
)}
|
||||||
{oauth.length > 0 && (
|
{oauthProviders.length > 0 && (
|
||||||
<Stack mt={config.get("oauth.disablePassword") ? undefined : "xl"}>
|
<Stack mt={config.get("oauth.disablePassword") ? undefined : "xl"}>
|
||||||
{config.get("oauth.disablePassword") ? (
|
{config.get("oauth.disablePassword") ? (
|
||||||
<Group align="center" className={classes.signInWith}>
|
<Group align="center" className={classes.signInWith}>
|
||||||
@ -182,7 +206,7 @@ const SignInForm = ({ redirectPath }: { redirectPath: string }) => {
|
|||||||
</Group>
|
</Group>
|
||||||
)}
|
)}
|
||||||
<Group position="center">
|
<Group position="center">
|
||||||
{oauth.map((provider) => (
|
{oauthProviders.map((provider) => (
|
||||||
<Button
|
<Button
|
||||||
key={provider}
|
key={provider}
|
||||||
component="a"
|
component="a"
|
||||||
|
@ -631,6 +631,7 @@ export default {
|
|||||||
"common.text.link": "Link",
|
"common.text.link": "Link",
|
||||||
"common.text.navigate-to-link": "Go to the link",
|
"common.text.navigate-to-link": "Go to the link",
|
||||||
"common.text.or": "or",
|
"common.text.or": "or",
|
||||||
|
"common.text.redirecting": "Redirecting...",
|
||||||
"common.button.go-back": "Go back",
|
"common.button.go-back": "Go back",
|
||||||
"common.button.go-home": "Go home",
|
"common.button.go-home": "Go home",
|
||||||
"common.notify.copied": "Your link was copied to the clipboard",
|
"common.notify.copied": "Your link was copied to the clipboard",
|
||||||
|
Loading…
Reference in New Issue
Block a user