refactor: centralize auth route constants and update references

This commit is contained in:
javayhu 2025-02-24 00:59:25 +08:00
parent 249efb5e32
commit 6d8b09ba3d
13 changed files with 41 additions and 30 deletions

View File

@ -1,11 +1,12 @@
import { ErrorCard } from "@/components/auth/error-card";
import { siteConfig } from "@/config/site";
import { constructMetadata } from "@/lib/metadata";
import { AUTH_ROUTE_ERROR } from "@/routes";
export const metadata = constructMetadata({
title: "Auth Error",
description: "Auth Error",
canonicalUrl: `${siteConfig.url}/auth/error`,
canonicalUrl: `${siteConfig.url}${AUTH_ROUTE_ERROR}`,
});
const AuthErrorPage = () => {

View File

@ -1,11 +1,12 @@
import { ForgotPasswordForm } from "@/components/auth/forgot-password-form";
import { siteConfig } from "@/config/site";
import { constructMetadata } from "@/lib/metadata";
import { AUTH_ROUTE_FORGOT_PASSWORD } from "@/routes";
export const metadata = constructMetadata({
title: "Forgot Password",
description: "Forgot your password? Reset it.",
canonicalUrl: `${siteConfig.url}/auth/forgot-password`,
canonicalUrl: `${siteConfig.url}${AUTH_ROUTE_FORGOT_PASSWORD}`,
});
const ForgotPasswordPage = () => {

View File

@ -1,11 +1,12 @@
import { LoginForm } from "@/components/auth/login-form";
import { siteConfig } from "@/config/site";
import { constructMetadata } from "@/lib/metadata";
import { AUTH_ROUTE_LOGIN } from "@/routes";
export const metadata = constructMetadata({
title: "Login",
description: "Login to your account",
canonicalUrl: `${siteConfig.url}/auth/login`,
canonicalUrl: `${siteConfig.url}${AUTH_ROUTE_LOGIN}`,
});
const LoginPage = () => {

View File

@ -1,11 +1,12 @@
import { RegisterForm } from "@/components/auth/register-form";
import { siteConfig } from "@/config/site";
import { constructMetadata } from "@/lib/metadata";
import { AUTH_ROUTE_REGISTER } from "@/routes";
export const metadata = constructMetadata({
title: "Register",
description: "Create an account to get started",
canonicalUrl: `${siteConfig.url}/auth/register`,
canonicalUrl: `${siteConfig.url}${AUTH_ROUTE_REGISTER}`,
});
const RegisterPage = () => {

View File

@ -1,11 +1,12 @@
import { AuthCard } from "@/components/auth/auth-card";
import { AUTH_ROUTE_LOGIN } from "@/routes";
import { TriangleAlertIcon } from "lucide-react";
export const ErrorCard = () => {
return (
<AuthCard
headerLabel="Something went wrong!"
bottomButtonHref="/auth/login"
bottomButtonHref={`${AUTH_ROUTE_LOGIN}`}
bottomButtonLabel="Back to login"
className="border-none"
>

View File

@ -20,6 +20,7 @@ import { useForm } from "react-hook-form";
import type * as z from "zod";
import { Icons } from "@/components/icons/icons";
import { authClient } from "@/lib/auth-client";
import { AUTH_ROUTE_LOGIN, AUTH_ROUTE_RESET_PASSWORD } from "@/routes";
export const ForgotPasswordForm = () => {
const [error, setError] = useState<string | undefined>("");
@ -36,7 +37,7 @@ export const ForgotPasswordForm = () => {
const onSubmit = async (values: z.infer<typeof ForgotPasswordSchema>) => {
const { data, error } = await authClient.forgetPassword({
email: values.email,
redirectTo: "/auth/reset-password",
redirectTo: `${AUTH_ROUTE_RESET_PASSWORD}`,
}, {
onRequest: (ctx) => {
// console.log("forgotPassword, request:", ctx.url);
@ -63,7 +64,7 @@ export const ForgotPasswordForm = () => {
<AuthCard
headerLabel="Froget password?"
bottomButtonLabel="Back to login"
bottomButtonHref="/auth/login"
bottomButtonHref={`${AUTH_ROUTE_LOGIN}`}
className="border-none"
>
<Form {...form}>

View File

@ -9,7 +9,7 @@ import {
} from "@/components/auth/auth-dialog";
import { LoginForm } from "@/components/auth/login-form";
import { useMediaQuery } from "@/hooks/use-media-query";
// import { authRoutes } from "@/routes";
import { AUTH_ROUTE_LOGIN } from "@/routes";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
@ -31,7 +31,7 @@ export const LoginWrapper = ({
const { isTablet, isDesktop } = useMediaQuery();
const handleLogin = () => {
router.push("/auth/login");
router.push(`${AUTH_ROUTE_LOGIN}`);
};
// Close the modal on route change

View File

@ -17,7 +17,7 @@ import { Input } from "@/components/ui/input";
import { authClient } from "@/lib/auth-client";
import { LoginSchema } from "@/lib/schemas";
import { cn } from "@/lib/utils";
import { DEFAULT_LOGIN_REDIRECT } from "@/routes";
import { AUTH_ROUTE_FORGOT_PASSWORD, AUTH_ROUTE_REGISTER, DEFAULT_LOGIN_REDIRECT } from "@/routes";
import { zodResolver } from "@hookform/resolvers/zod";
import { useSearchParams } from "next/navigation";
import { useState } from "react";
@ -76,7 +76,7 @@ export const LoginForm = ({ className }: { className?: string }) => {
<AuthCard
headerLabel="Welcome back"
bottomButtonLabel="Don't have an account? Sign up"
bottomButtonHref="/auth/register"
bottomButtonHref={`${AUTH_ROUTE_REGISTER}`}
showSocialLoginButton
className={cn("", className)}
>
@ -115,7 +115,7 @@ export const LoginForm = ({ className }: { className?: string }) => {
className="px-0 font-normal text-muted-foreground"
>
<a
href="/auth/forgot-password"
href={`${AUTH_ROUTE_FORGOT_PASSWORD}`}
className="text-xs hover:underline hover:underline-offset-4 hover:text-primary"
>
Forgot password?
@ -135,7 +135,7 @@ export const LoginForm = ({ className }: { className?: string }) => {
)}
/>
</div>
<FormError message={error || urlError} />
<FormError message={error || urlError || undefined} />
<FormSuccess message={success} />
<Button
disabled={isPending}

View File

@ -16,7 +16,7 @@ import {
import { Input } from "@/components/ui/input";
import { authClient } from "@/lib/auth-client";
import { RegisterSchema } from "@/lib/schemas";
import { DEFAULT_LOGIN_REDIRECT } from "@/routes";
import { AUTH_ROUTE_LOGIN, DEFAULT_LOGIN_REDIRECT } from "@/routes";
import { zodResolver } from "@hookform/resolvers/zod";
import { useSearchParams } from "next/navigation";
import { useState } from "react";
@ -78,7 +78,7 @@ export const RegisterForm = () => {
<AuthCard
headerLabel="Create an account"
bottomButtonLabel="Already have an account? Sign in"
bottomButtonHref="/auth/login"
bottomButtonHref={`${AUTH_ROUTE_LOGIN}`}
showSocialLoginButton
className="border-none"
>

View File

@ -16,9 +16,10 @@ import {
import { Input } from "@/components/ui/input";
import { authClient } from "@/lib/auth-client";
import { ResetPasswordSchema } from "@/lib/schemas";
import { AUTH_ROUTE_LOGIN } from "@/routes";
import { zodResolver } from "@hookform/resolvers/zod";
import { useRouter, useSearchParams } from "next/navigation";
import { useState, useTransition } from "react";
import { useState } from "react";
import { useForm } from "react-hook-form";
import type * as z from "zod";
@ -61,7 +62,7 @@ export const ResetPasswordForm = () => {
onSuccess: (ctx) => {
// console.log("resetPassword, success:", ctx.data);
// setSuccess("Password reset successfully");
router.push("/auth/login");
router.push(`${AUTH_ROUTE_LOGIN}`);
},
onError: (ctx) => {
console.log("resetPassword, error:", ctx.error);
@ -74,7 +75,7 @@ export const ResetPasswordForm = () => {
<AuthCard
headerLabel="Reset password"
bottomButtonLabel="Back to login"
bottomButtonHref="/auth/login"
bottomButtonHref={`${AUTH_ROUTE_LOGIN}`}
className="border-none"
>
<Form {...form}>

View File

@ -7,7 +7,7 @@ import { useState } from "react";
import { FaBrandsGitHub } from "@/components/icons/github";
import { FaBrandsGoogle } from "@/components/icons/google";
import { authClient } from "@/lib/auth-client";
import { DEFAULT_LOGIN_REDIRECT } from "@/routes";
import { AUTH_ROUTE_ERROR, DEFAULT_LOGIN_REDIRECT } from "@/routes";
/**
* social login buttons
@ -32,7 +32,7 @@ export const SocialLoginButton = () => {
/**
* a url to redirect if an error occurs during the sign in process
*/
errorCallbackURL: "/auth/error",
errorCallbackURL: `${AUTH_ROUTE_ERROR}`,
/**
* a url to redirect if the user is newly registered
*/

View File

@ -15,7 +15,6 @@ import {
} from "@/components/ui/navigation-menu";
import { Sheet, SheetContent, SheetTrigger } from "@/components/ui/sheet";
import { siteConfig } from "@/config/site";
// import { useCurrentUser } from "@/hooks/use-current-user";
import { useScroll } from "@/hooks/use-scroll";
import { cn } from "@/lib/utils";
import type { DashboardConfig, MarketingConfig } from "@/types";

View File

@ -5,10 +5,9 @@
*/
export const publicRoutes = [
"/",
"/about(/.*)?",
"/terms(/.*)?",
"/privacy(/.*)?",
"/about(/.*)?",
"/changelog(/.*)?",
// blog
@ -17,10 +16,10 @@ export const publicRoutes = [
// docs
"/docs(/.*)?",
// unsubscribe
// unsubscribe newsletter
"/unsubscribe(/.*)?",
// stripe webhook must be public, otherwise can not receive stripe events
// stripe webhook
"/api/webhook",
// og images
@ -32,12 +31,18 @@ export const publicRoutes = [
* These routes will redirect logged in users to /dashboard
* @type {string[]}
*/
export const AUTH_ROUTE_LOGIN = "/auth/login";
export const AUTH_ROUTE_REGISTER = "/auth/register";
export const AUTH_ROUTE_ERROR = "/auth/error";
export const AUTH_ROUTE_FORGOT_PASSWORD = "/auth/forgot-password";
export const AUTH_ROUTE_RESET_PASSWORD = "/auth/reset-password";
export const authRoutes = [
"/auth/login",
"/auth/register",
"/auth/error",
"/auth/forgot-password",
"/auth/reset-password",
AUTH_ROUTE_LOGIN,
AUTH_ROUTE_REGISTER,
AUTH_ROUTE_ERROR,
AUTH_ROUTE_FORGOT_PASSWORD,
AUTH_ROUTE_RESET_PASSWORD,
];
/**