mirror of
https://github.com/stonith404/pingvin-share.git
synced 2025-01-29 01:28:59 -05:00
fix: invalid redirection after jwt expiry
This commit is contained in:
parent
4e840ecd29
commit
82f204e8a9
5 changed files with 37 additions and 14 deletions
|
@ -114,7 +114,7 @@ export class AuthService {
|
||||||
refreshTokenId,
|
refreshTokenId,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
expiresIn: "10s",
|
expiresIn: "15min",
|
||||||
secret: this.config.get("JWT_SECRET"),
|
secret: this.config.get("JWT_SECRET"),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -11,15 +11,21 @@ import {
|
||||||
import { useForm, yupResolver } from "@mantine/form";
|
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 React from "react";
|
import React from "react";
|
||||||
import { TbInfoCircle } from "react-icons/tb";
|
import { TbInfoCircle } from "react-icons/tb";
|
||||||
import * as yup from "yup";
|
import * as yup from "yup";
|
||||||
import useConfig from "../../hooks/config.hook";
|
import useConfig from "../../hooks/config.hook";
|
||||||
|
import useUser from "../../hooks/user.hook";
|
||||||
import authService from "../../services/auth.service";
|
import authService from "../../services/auth.service";
|
||||||
|
import userService from "../../services/user.service";
|
||||||
import toast from "../../utils/toast.util";
|
import toast from "../../utils/toast.util";
|
||||||
|
|
||||||
const SignInForm = () => {
|
const SignInForm = ({ redirectPath }: { redirectPath: string }) => {
|
||||||
const config = useConfig();
|
const config = useConfig();
|
||||||
|
const router = useRouter();
|
||||||
|
const { setUser } = useUser();
|
||||||
|
|
||||||
const [showTotp, setShowTotp] = React.useState(false);
|
const [showTotp, setShowTotp] = React.useState(false);
|
||||||
const [loginToken, setLoginToken] = React.useState("");
|
const [loginToken, setLoginToken] = React.useState("");
|
||||||
|
|
||||||
|
@ -42,10 +48,10 @@ const SignInForm = () => {
|
||||||
validate: yupResolver(validationSchema),
|
validate: yupResolver(validationSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
const signIn = (email: string, password: string) => {
|
const signIn = async (email: string, password: string) => {
|
||||||
authService
|
await authService
|
||||||
.signIn(email, password)
|
.signIn(email, password)
|
||||||
.then((response) => {
|
.then(async (response) => {
|
||||||
if (response.data["loginToken"]) {
|
if (response.data["loginToken"]) {
|
||||||
// Prompt the user to enter their totp code
|
// Prompt the user to enter their totp code
|
||||||
setShowTotp(true);
|
setShowTotp(true);
|
||||||
|
@ -58,7 +64,8 @@ const SignInForm = () => {
|
||||||
});
|
});
|
||||||
setLoginToken(response.data["loginToken"]);
|
setLoginToken(response.data["loginToken"]);
|
||||||
} else {
|
} else {
|
||||||
window.location.replace("/");
|
setUser(await userService.getCurrentUser());
|
||||||
|
router.replace(redirectPath);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(toast.axiosError);
|
.catch(toast.axiosError);
|
||||||
|
|
|
@ -10,13 +10,18 @@ import {
|
||||||
} from "@mantine/core";
|
} from "@mantine/core";
|
||||||
import { useForm, yupResolver } from "@mantine/form";
|
import { useForm, yupResolver } from "@mantine/form";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
import { useRouter } from "next/router";
|
||||||
import * as yup from "yup";
|
import * as yup from "yup";
|
||||||
import useConfig from "../../hooks/config.hook";
|
import useConfig from "../../hooks/config.hook";
|
||||||
|
import useUser from "../../hooks/user.hook";
|
||||||
import authService from "../../services/auth.service";
|
import authService from "../../services/auth.service";
|
||||||
|
import userService from "../../services/user.service";
|
||||||
import toast from "../../utils/toast.util";
|
import toast from "../../utils/toast.util";
|
||||||
|
|
||||||
const SignUpForm = () => {
|
const SignUpForm = () => {
|
||||||
const config = useConfig();
|
const config = useConfig();
|
||||||
|
const router = useRouter();
|
||||||
|
const { setUser } = useUser();
|
||||||
|
|
||||||
const validationSchema = yup.object().shape({
|
const validationSchema = yup.object().shape({
|
||||||
email: yup.string().email().required(),
|
email: yup.string().email().required(),
|
||||||
|
@ -33,10 +38,13 @@ const SignUpForm = () => {
|
||||||
validate: yupResolver(validationSchema),
|
validate: yupResolver(validationSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
const signUp = (email: string, username: string, password: string) => {
|
const signUp = async (email: string, username: string, password: string) => {
|
||||||
authService
|
await authService
|
||||||
.signUp(email, username, password)
|
.signUp(email, username, password)
|
||||||
.then(() => window.location.replace("/"))
|
.then(async () => {
|
||||||
|
setUser(await userService.getCurrentUser());
|
||||||
|
router.replace("/");
|
||||||
|
})
|
||||||
.catch(toast.axiosError);
|
.catch(toast.axiosError);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,13 @@ export async function middleware(request: NextRequest) {
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const rule of rules) {
|
for (const rule of rules) {
|
||||||
if (rule.condition)
|
if (rule.condition) {
|
||||||
return NextResponse.redirect(new URL(rule.path, request.url));
|
let { path } = rule;
|
||||||
|
|
||||||
|
if (path == "/auth/signIn") {
|
||||||
|
path = path + "?redirect=" + encodeURIComponent(route);
|
||||||
|
}
|
||||||
|
return NextResponse.redirect(new URL(path, request.url));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,20 +5,22 @@ import Meta from "../../components/Meta";
|
||||||
import useUser from "../../hooks/user.hook";
|
import useUser from "../../hooks/user.hook";
|
||||||
|
|
||||||
const SignIn = () => {
|
const SignIn = () => {
|
||||||
const router = useRouter();
|
|
||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const redirectPath = (router.query.redirect as string) ?? "/upload";
|
||||||
|
|
||||||
// If the access token is expired, the middleware redirects to this page.
|
// If the access token is expired, the middleware redirects to this page.
|
||||||
// If the refresh token is still valid, the user will be redirected to the home page.
|
// If the refresh token is still valid, the user will be redirected to the home page.
|
||||||
if (user) {
|
if (user) {
|
||||||
router.replace("/");
|
router.replace(redirectPath);
|
||||||
return <LoadingOverlay overlayOpacity={1} visible />;
|
return <LoadingOverlay overlayOpacity={1} visible />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Meta title="Sign In" />
|
<Meta title="Sign In" />
|
||||||
<SignInForm />
|
<SignInForm redirectPath={redirectPath} />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue