-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SignIn and SignUp Backend and Improved UI
- Loading branch information
1 parent
c67605a
commit a1d9045
Showing
10 changed files
with
333 additions
and
211 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,2 @@ | ||
MONGO_URL = 'mongodb://localhost:27017/75per' | ||
PORT = 4000 |
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 |
---|---|---|
|
@@ -21,3 +21,5 @@ | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
.env |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,66 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const cors = require('cors'); | ||
const jwt = require('jsonwebtoken'); | ||
const mongoose = require('mongoose'); | ||
const dotenv=require("dotenv"); | ||
dotenv.config() | ||
|
||
const app = express(); | ||
const PORT = process.env.PORT || 4000; | ||
|
||
// Middleware | ||
app.use(bodyParser.json()); | ||
app.use(cors()); | ||
|
||
// Connect to MongoDB | ||
mongoose.connect(process.env.MONGO_URL, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
const db = mongoose.connection; | ||
db.on('error', console.error.bind(console, 'connection error:')); | ||
db.once('open', () => { | ||
console.log('Connected to MongoDB'); | ||
}); | ||
|
||
// Define User Schema and Model | ||
const userSchema = new mongoose.Schema({ | ||
username: String, | ||
email: { type: String, unique: true }, | ||
password: String, | ||
}); | ||
|
||
const User = mongoose.model('User', userSchema); | ||
|
||
// Routes | ||
app.post('/login', async (req, res) => { | ||
const { email, password } = req.body; | ||
const user = await User.findOne({email:email,password:password}); | ||
if (!user) { | ||
return res.status(401).json({ errors: 'Invalid credentials' }); | ||
} | ||
|
||
// Assuming your user object contains a username field | ||
const { username } = user; | ||
console.log(username) | ||
const token = jwt.sign({ email: user.email, username: user.username }, 'secret', { expiresIn: '1h' }); | ||
res.json({ success: true, token, username }); // Return username along with other data | ||
}); | ||
|
||
app.post('/signup', async (req, res) => { | ||
const { username, email, password } = req.body; | ||
try { | ||
if (await User.findOne({ email })) { | ||
return res.status(400).json({ errors: 'Email already exists' }); | ||
} | ||
const newUser = new User({ username, email, password }); | ||
await newUser.save(); | ||
const token = jwt.sign({ email: newUser.email }, 'secret', { expiresIn: '1h' }); | ||
res.json({ success: true, token }); | ||
} catch (err) { | ||
res.status(500).json({ errors: 'Internal server error' }); | ||
} | ||
}); | ||
|
||
app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.