feat: redirect to next param on authentication
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import type { SocialProvider } from "better-auth/social-providers";
|
import type { SocialProvider } from "better-auth/social-providers";
|
||||||
|
import { useSearchParams } from "next/navigation";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { t } from "@lingui/core/macro";
|
import { t } from "@lingui/core/macro";
|
||||||
import { Trans } from "@lingui/react/macro";
|
import { Trans } from "@lingui/react/macro";
|
||||||
@@ -160,6 +161,9 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) {
|
|||||||
const { showPopup } = usePopup();
|
const { showPopup } = usePopup();
|
||||||
const oidcProviderName = "OIDC";
|
const oidcProviderName = "OIDC";
|
||||||
|
|
||||||
|
const redirect = useSearchParams().get("next");
|
||||||
|
const callbackURL = redirect ?? "/boards";
|
||||||
|
|
||||||
// Safely get environment variables on client side to avoid hydration mismatch
|
// Safely get environment variables on client side to avoid hydration mismatch
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const credentialsAllowed =
|
const credentialsAllowed =
|
||||||
@@ -195,7 +199,7 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) {
|
|||||||
name,
|
name,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
callbackURL: "/boards",
|
callbackURL,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () =>
|
onSuccess: () =>
|
||||||
@@ -212,7 +216,7 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) {
|
|||||||
{
|
{
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
callbackURL: "/boards",
|
callbackURL,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () =>
|
onSuccess: () =>
|
||||||
@@ -229,7 +233,7 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) {
|
|||||||
await authClient.signIn.magicLink(
|
await authClient.signIn.magicLink(
|
||||||
{
|
{
|
||||||
email,
|
email,
|
||||||
callbackURL: "/boards",
|
callbackURL,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () => setIsMagicLinkSent(true, email),
|
onSuccess: () => setIsMagicLinkSent(true, email),
|
||||||
@@ -250,14 +254,14 @@ export function Auth({ setIsMagicLinkSent, isSignUp }: AuthProps) {
|
|||||||
// Use oauth2 signin for OIDC provider
|
// Use oauth2 signin for OIDC provider
|
||||||
const result = await authClient.signIn.oauth2({
|
const result = await authClient.signIn.oauth2({
|
||||||
providerId: "oidc",
|
providerId: "oidc",
|
||||||
callbackURL: "/boards",
|
callbackURL,
|
||||||
});
|
});
|
||||||
error = result.error;
|
error = result.error;
|
||||||
} else {
|
} else {
|
||||||
// Use social signin for traditional social providers
|
// Use social signin for traditional social providers
|
||||||
const result = await authClient.signIn.social({
|
const result = await authClient.signIn.social({
|
||||||
provider,
|
provider,
|
||||||
callbackURL: "/boards",
|
callbackURL,
|
||||||
});
|
});
|
||||||
error = result.error;
|
error = result.error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
import { t } from "@lingui/core/macro";
|
import { t } from "@lingui/core/macro";
|
||||||
import { Trans } from "@lingui/react/macro";
|
import { Trans } from "@lingui/react/macro";
|
||||||
import { env } from "next-runtime-env";
|
import { env } from "next-runtime-env";
|
||||||
@@ -17,6 +17,8 @@ export default function LoginPage() {
|
|||||||
const [isMagicLinkSent, setIsMagicLinkSent] = useState<boolean>(false);
|
const [isMagicLinkSent, setIsMagicLinkSent] = useState<boolean>(false);
|
||||||
const [magicLinkRecipient, setMagicLinkRecipient] = useState<string>("");
|
const [magicLinkRecipient, setMagicLinkRecipient] = useState<string>("");
|
||||||
|
|
||||||
|
const redirect = useSearchParams().get("next");
|
||||||
|
|
||||||
const handleMagicLinkSent = (value: boolean, recipient: string) => {
|
const handleMagicLinkSent = (value: boolean, recipient: string) => {
|
||||||
setIsMagicLinkSent(value);
|
setIsMagicLinkSent(value);
|
||||||
setMagicLinkRecipient(recipient);
|
setMagicLinkRecipient(recipient);
|
||||||
@@ -61,7 +63,11 @@ export default function LoginPage() {
|
|||||||
<Trans>
|
<Trans>
|
||||||
Don't have an account?{" "}
|
Don't have an account?{" "}
|
||||||
<span className="underline">
|
<span className="underline">
|
||||||
<Link href="/signup">Sign up</Link>
|
<Link
|
||||||
|
href={redirect ? `/signup?next=${redirect}` : "/signup"}
|
||||||
|
>
|
||||||
|
Sign up
|
||||||
|
</Link>
|
||||||
</span>
|
</span>
|
||||||
</Trans>
|
</Trans>
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
import { t } from "@lingui/core/macro";
|
import { t } from "@lingui/core/macro";
|
||||||
import { Trans } from "@lingui/react/macro";
|
import { Trans } from "@lingui/react/macro";
|
||||||
import { env } from "next-runtime-env";
|
import { env } from "next-runtime-env";
|
||||||
@@ -17,6 +17,8 @@ export default function SignUpPage() {
|
|||||||
const [isMagicLinkSent, setIsMagicLinkSent] = useState<boolean>(false);
|
const [isMagicLinkSent, setIsMagicLinkSent] = useState<boolean>(false);
|
||||||
const [magicLinkRecipient, setMagicLinkRecipient] = useState<string>("");
|
const [magicLinkRecipient, setMagicLinkRecipient] = useState<string>("");
|
||||||
|
|
||||||
|
const redirect = useSearchParams().get("next");
|
||||||
|
|
||||||
const { data } = authClient.useSession();
|
const { data } = authClient.useSession();
|
||||||
|
|
||||||
if (data?.user.id) router.push("/boards");
|
if (data?.user.id) router.push("/boards");
|
||||||
@@ -86,7 +88,9 @@ export default function SignUpPage() {
|
|||||||
<Trans>
|
<Trans>
|
||||||
Already have an account?{" "}
|
Already have an account?{" "}
|
||||||
<span className="underline">
|
<span className="underline">
|
||||||
<Link href="/login">Sign in</Link>
|
<Link href={redirect ? `/login?next=${redirect}` : "/login"}>
|
||||||
|
Sign in
|
||||||
|
</Link>
|
||||||
</span>
|
</span>
|
||||||
</Trans>
|
</Trans>
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user