refactor: update routes, middleware, and configuration for improved routing and environment variables

- Update middleware to exclude API routes from locale redirection
- Modify public routes to include new pages and remove documentation routes
- Adjust auth routes order for better readability
- Replace `NEXT_PUBLIC_APP_URL` with `NEXT_PUBLIC_BASE_URL` across configuration files
- Clean up and standardize authentication and site configuration
This commit is contained in:
javayhu 2025-03-08 11:04:54 +08:00
parent 988a1a6b58
commit 074a6d9f1e
7 changed files with 27 additions and 23 deletions

View File

@ -70,7 +70,7 @@ export const RegisterForm = () => {
},
onError: (ctx) => {
// sign up fail, display the error message
console.error("register, error:", ctx.error.message);
console.error("register, error:", ctx.error);
setError(ctx.error.message);
},
});

View File

@ -15,7 +15,6 @@ export const footerConfig: FooterConfig = {
title: "Resources",
items: [
{ title: "Blog", href: Routes.Blog },
{ title: "Documentation", href: Routes.Docs },
{ title: "Changelog", href: Routes.Changelog },
{ title: "Roadmap", href: Routes.Roadmap },
],

View File

@ -1,6 +1,6 @@
import type { SiteConfig } from "@/types";
const SITE_URL = process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000";
const SITE_URL = process.env.NEXT_PUBLIC_BASE_URL ?? "http://localhost:3000";
export const siteConfig: SiteConfig = {
name: "MkSaaS",

View File

@ -2,7 +2,7 @@ import { createAuthClient } from "better-auth/react";
import { adminClient } from "better-auth/client/plugins";
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_APP_URL!,
baseURL: process.env.NEXT_PUBLIC_BASE_URL!,
plugins: [
// https://www.better-auth.com/docs/plugins/admin#add-the-client-plugin
adminClient(),

View File

@ -11,11 +11,11 @@ import { parse as parseCookies } from "cookie";
const from = process.env.RESEND_FROM || "delivered@resend.dev";
const getLocaleFromRequest = (request?: Request) => {
const cookies = parseCookies(request?.headers.get("cookie") ?? "");
return (
(cookies[LOCALE_COOKIE_NAME] as Locale) ??
routing.defaultLocale
);
const cookies = parseCookies(request?.headers.get("cookie") ?? "");
return (
(cookies[LOCALE_COOKIE_NAME] as Locale) ??
routing.defaultLocale
);
};
export const auth = betterAuth({
@ -61,13 +61,13 @@ export const auth = betterAuth({
},
locale,
});
},
},
},
emailVerification: {
// https://www.better-auth.com/docs/concepts/email#auto-signin-after-verification
autoSignInAfterVerification: true,
// https://www.better-auth.com/docs/authentication/email-password#require-email-verification
sendVerificationEmail: async ( { user, url, token }, request) => {
sendVerificationEmail: async ({ user, url, token }, request) => {
const locale = getLocaleFromRequest(request);
console.log("sendVerificationEmail, locale:", locale);
await send({
@ -95,11 +95,11 @@ export const auth = betterAuth({
},
account: {
// https://www.better-auth.com/docs/concepts/users-accounts#account-linking
accountLinking: {
accountLinking: {
enabled: true,
trustedProviders: ["google", "github"],
},
},
trustedProviders: ["google", "github"],
},
},
plugins: [
// https://www.better-auth.com/docs/plugins/admin
admin(),

View File

@ -21,6 +21,8 @@ export const config = {
// Enable redirects that add missing locales
// (e.g. `/pathnames` -> `/zh/pathnames`)
'/((?!_next|_vercel|.*\\..*).*)'
// Exclude API routes and other Next.js internal routes
// if not exclude api routes, auth routes will not work
'/((?!api|_next|_vercel|.*\\..*).*)'
]
};

View File

@ -80,16 +80,19 @@ export enum Routes {
*/
export const publicRoutes = [
"/",
"/terms(/.*)?",
"/privacy(/.*)?",
"/about(/.*)?",
"/changelog(/.*)?",
// blog
"/blog(/.*)?",
// docs
"/docs(/.*)?",
// pages
"/terms-of-service(/.*)?",
"/privacy-policy(/.*)?",
"/cookie-policy(/.*)?",
"/about(/.*)?",
"/contact(/.*)?",
"/waitlist(/.*)?",
"/changelog(/.*)?",
// unsubscribe newsletter
"/unsubscribe(/.*)?",
@ -105,9 +108,9 @@ export const publicRoutes = [
* The routes for the authentication pages
*/
export const authRoutes = [
Routes.AuthError,
Routes.Login,
Routes.Register,
Routes.AuthError,
Routes.ForgotPassword,
Routes.ResetPassword,
];