Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ WORKDIR /app
# Copy package.json
COPY package.json .

# Install production dependencies using Yarn
RUN npm install --production
# Install dependencies (dev deps needed for the build step)
RUN npm install
# Copy the rest of the application files
COPY . .

# Build the frontend using Yarn
# Build the frontend
RUN npm run build

# Stage 2: Serve the application with Nginx
Expand Down
14 changes: 10 additions & 4 deletions backend/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ router.post("/signup", async (req, res) => {
const { username, email, password } = req.body;

try {
const existingUser = await User.findOne( {email} );
const existingUser = await User.findOne({
$or: [{ email }, { username }],
});

if (existingUser)
return res.status(400).json( {message: 'User already exists'} );
return res.status(400).json({ message: 'User already exists' });

const newUser = new User( {username, email, password} );
const newUser = new User({ username, email, password });
await newUser.save();
res.status(201).json( {message: 'User created successfully'} );
res.status(201).json({ message: 'User created successfully' });
} catch (err) {
if (err && err.code === 11000) {
return res.status(400).json({ message: 'User already exists' });
}

res.status(500).json({ message: 'Error creating user', error: err.message });
}
});
Expand Down
10 changes: 9 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ require('./config/passportConfig');
const app = express();

// CORS configuration
app.use(cors('*'));
const allowedOrigins = (process.env.FRONTEND_URL || 'http://localhost:5173')
.split(',')
.map((origin) => origin.trim())
.filter(Boolean);

app.use(cors({
origin: allowedOrigins,
credentials: true,
}));

// Middleware
app.use(bodyParser.json());
Expand Down
9 changes: 9 additions & 0 deletions spec/auth.routes.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('Auth Routes', () => {
expect(res.body.message).toBe('User already exists');
});

it('should not sign up a user with existing username', async () => {
await new User({ username: 'testuser', email: 'test@example.com', password: 'password123' }).save();
const res = await request(app)
.post('/auth/signup')
.send({ username: 'testuser', email: 'test2@example.com', password: 'password456' });
expect(res.status).toBe(400);
expect(res.body.message).toBe('User already exists');
});

it('should login a user with correct credentials', async () => {
await request(app)
.post('/auth/signup')
Expand Down
6 changes: 5 additions & 1 deletion src/pages/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const Login: React.FC = () => {
setIsLoading(true);

try {
const response = await axios.post(`${backendUrl}/api/auth/login`, formData);
const response = await axios.post(
`${backendUrl}/api/auth/login`,
formData,
{ withCredentials: true }
);
setMessage(response.data.message);

if (response.data.message === 'Login successful') {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/Signup/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const navigate = useNavigate();
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
try {
const response = await axios.post(`${backendUrl}/api/auth/signup`,
formData // Include cookies for session
const response = await axios.post(
`${backendUrl}/api/auth/signup`,
formData,
{ withCredentials: true }
);
setMessage(response.data.message); // Show success message from backend

Expand Down
Loading