Skip to content

Commit

Permalink
Added SignIn and SignUp Backend and Improved UI
Browse files Browse the repository at this point in the history
  • Loading branch information
SamarthUrane committed Jun 2, 2024
1 parent c67605a commit a1d9045
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 211 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGO_URL = 'mongodb://localhost:27017/75per'
PORT = 4000
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.env
48 changes: 34 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"bootstrap": "^5.3.3",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-session": "^1.18.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.4.0",
"mongoose": "^8.4.1",
"passport": "^0.7.0",
"passport-google-oauth20": "^2.0.0",
"path": "^0.12.7",
Expand Down
66 changes: 66 additions & 0 deletions server.js
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}`));
120 changes: 0 additions & 120 deletions src/components/login/LoginSignup.css

This file was deleted.

Loading

0 comments on commit a1d9045

Please sign in to comment.