Update authentication redirects to use environment variable for site URL, enhancing flexibility for different environments. Add NEXT_PUBLIC_SITE_URL to .env for production URL configuration.

This commit is contained in:
2025-04-23 22:38:56 +08:00
parent b94a91914a
commit 92db5ad783
5 changed files with 115 additions and 4 deletions

View File

@@ -101,11 +101,14 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const signInWithGoogle = async () => {
setIsLoading(true);
try {
// 获取网站 URL如果环境变量不存在则使用当前来源
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || window.location.origin;
// 尝试通过Supabase登录Google
const { error } = await supabase.auth.signInWithOAuth({
provider: 'google',
options: {
redirectTo: `${window.location.origin}/auth/callback`,
redirectTo: `${siteUrl}/auth/callback`,
},
});
@@ -127,11 +130,14 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const signInWithGitHub = async () => {
setIsLoading(true);
try {
// 获取网站 URL如果环境变量不存在则使用当前来源
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || window.location.origin;
// 尝试通过Supabase登录GitHub
const { error } = await supabase.auth.signInWithOAuth({
provider: 'github',
options: {
redirectTo: `${window.location.origin}/auth/callback`,
redirectTo: `${siteUrl}/auth/callback`,
},
});
@@ -153,12 +159,15 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const signUp = async (email: string, password: string) => {
setIsLoading(true);
try {
// 获取网站 URL如果环境变量不存在则使用当前来源
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || window.location.origin;
// 尝试通过Supabase注册
const { error } = await supabase.auth.signUp({
email,
password,
options: {
emailRedirectTo: `${window.location.origin}/auth/callback`,
emailRedirectTo: `${siteUrl}/auth/callback`,
}
});