Skip to content

Commit

Permalink
Want To Add Backend Of Login/Signup Page #191 (#220)
Browse files Browse the repository at this point in the history
* added backend of login/signup

* updated

* removed node_modules
  • Loading branch information
nishant0708 authored Jun 12, 2024
1 parent ee6e723 commit 15e6071
Show file tree
Hide file tree
Showing 7 changed files with 1,281 additions and 45 deletions.
100 changes: 100 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
const PORT = process.env.PORT || 5000;
const JWT_SECRET = 'your_jwt_secret'; // Replace with your own secret

// Middleware
app.use(bodyParser.json());
app.use(cors());

// MongoDB connection
mongoose.connect('ADD_MONGO_DB_URI', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));

// User schema and model
const userSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true }
});

const User = mongoose.model('User', userSchema);

// Signup route
app.post('/api/signup', async (req, res) => {
const { username, email, password } = req.body;

try {
// Check if user already exists
const existingUser = await User.findOne({ email });
const existingUsername = await User.findOne({ username });
if (existingUser) {
return res.status(400).json({ message: 'User already exists' });
}
if (existingUsername) {
return res.status(400).json({ message: 'Username already exists' });
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);

// Create new user
const newUser = new User({
username,
email,
password: hashedPassword
});

await newUser.save();

// Generate JWT token
const token = jwt.sign({ userId: newUser._id }, JWT_SECRET, { expiresIn: '1h' });

// Respond with token
res.status(201).json({ token });
} catch (error) {
console.error('Signup error:', error);
res.status(500).json({ message: 'Server error' });
}
});

// Login route
app.post('/api/login', async (req, res) => {
const { email, password } = req.body;

try {
// Check if user exists
const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ message: 'Invalid email or password' });
}

// Check password
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid email or password' });
}

// Generate JWT token
const token = jwt.sign({ userId: user._id }, JWT_SECRET, { expiresIn: '1h' });

// Respond with token
res.status(200).json({ token });
} catch (error) {
console.error('Login error:', error);
res.status(500).json({ message: 'Server error' });
}
});

app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Loading

0 comments on commit 15e6071

Please sign in to comment.