Skip to content

Commit c6399ff

Browse files
Merge pull request #573 from SUMIQVERSE/fix-authentication
Fix authentication
2 parents 809a967 + ae2fceb commit c6399ff

4 files changed

Lines changed: 27 additions & 15 deletions

File tree

backend/server.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ const logger = require('./logger');
1414

1515
const app = express();
1616

17+
// Enable trust proxy
18+
app.set('trust proxy', 1);
19+
1720
// CORS configuration
18-
const allowedOrigins = ['http://localhost:5173', 'https://github-spy.etlify.app'];
21+
const allowedOrigins = ['http://localhost:5173', 'https://github-spy.netlify.app']; // there was a typo error in the url, it is fixed now.
1922
app.use(cors({
2023
origin: function (origin, callback) {
2124
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
@@ -29,10 +32,16 @@ app.use(cors({
2932

3033
// Middleware
3134
app.use(bodyParser.json());
32-
if (process.env.NODE_ENV === 'production') {
33-
app.set('trust proxy', 1);
34-
}
35-
app.use(session(createSessionConfig()));
35+
app.use(session({
36+
secret: process.env.SESSION_SECRET,
37+
resave: false,
38+
saveUninitialized: false,
39+
cookie: {
40+
secure: process.env.NODE_ENV === 'production', // Only send cookies over HTTPS in production
41+
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax', //Cross-domain cookies = 'none'
42+
maxAge: 24 * 60 * 60 * 1000
43+
}
44+
}));
3645
app.use(passport.initialize());
3746
app.use(passport.session());
3847

backend/validators/authValidator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const signupSchema = z.object({
1818
.min(8, "Password must be at least 8 characters long")
1919
.max(100, "Password must be at most 100 characters long")
2020
.regex(
21-
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9\s])[^\s]{8,}$/,
21+
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/,
2222
'Password must contain uppercase, lowercase, number, and special character'
2323
),
2424
});

src/pages/Login/Login.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ThemeContext } from "../../context/ThemeContext";
55
import type { ThemeContextType } from "../../context/ThemeContext";
66
import { AuthContext } from "../../context/AuthContext";
77

8-
const backendUrl = import.meta.env.VITE_BACKEND_URL;
8+
const backendUrl = import.meta.env.VITE_BACKEND_URL || ""; // Fallback to an empty string if VITE_BACKEND_URL is undefined to ensure relative routing
99

1010
interface LoginFormData {
1111
email: string;
@@ -33,7 +33,7 @@ const Login: React.FC = () => {
3333

3434
try {
3535
const response = await axios.post(`${backendUrl}/api/auth/login`, formData, {
36-
withCredentials: true,
36+
withCredentials: true
3737
});
3838
setMessage(response.data.message);
3939

src/pages/Signup/Signup.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { User, Mail, Lock, Eye, EyeOff } from "lucide-react";
66
import { ThemeContext } from "../../context/ThemeContext";
77
import type { ThemeContextType } from "../../context/ThemeContext";
88

9-
const backendUrl = import.meta.env.VITE_BACKEND_URL;
9+
const backendUrl = import.meta.env.VITE_BACKEND_URL || ""; // Fallback to an empty string if VITE_BACKEND_URL is undefined to ensure relative routing
1010

1111
interface SignUpFormData {
1212
username: string;
@@ -83,18 +83,21 @@ const SignUp: React.FC = () => {
8383
}
8484
setIsLoading(true);
8585
try {
86-
const response = await axios.post(`${backendUrl}/api/auth/signup`,
87-
formData,
88-
{ withCredentials: true }
89-
);
86+
const response = await axios.post(`${backendUrl}/api/auth/signup`, formData, {
87+
withCredentials: true
88+
});
9089
setMessage(response.data.message); // Show success message from backend
9190

9291
// Navigate to login page after successful signup
9392
if (response.data.message === 'User created successfully') {
9493
navigate("/login");
9594
}
96-
} catch (error: any) {
97-
setMessage(error.response?.data?.message || "Something went wrong. Please try again.");
95+
} catch (error) {
96+
if (axios.isAxiosError(error)) {
97+
setMessage(error.response?.data?.message || "Something went wrong. Please try again.");
98+
} else {
99+
setMessage("An unexpected error occurred. Please try again.");
100+
}
98101
} finally {
99102
setIsLoading(false);
100103
}

0 commit comments

Comments
 (0)