-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* added backend of login/signup * updated * removed node_modules
- Loading branch information
1 parent
ee6e723
commit 15e6071
Showing
7 changed files
with
1,281 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
Oops, something went wrong.