Skip to content

Commit

Permalink
Merge branch 'SUGAM-ARORA:main' into new-project-display
Browse files Browse the repository at this point in the history
  • Loading branch information
DharshiBalasubramaniyam authored Jun 27, 2024
2 parents 354a3a1 + 19b5246 commit 3220576
Show file tree
Hide file tree
Showing 23 changed files with 2,369 additions and 420 deletions.
38 changes: 38 additions & 0 deletions BACKEND/controllers/authcontrollers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const asyncHandler = require("express-async-handler");
const User = require('../models/user');
const jwt = require('jsonwebtoken');

const signup = asyncHandler(async (req, res) => {
const { username, email, password } = req.body;
try {
const user = new User({ username, email, password });
await user.save();
res.status(201).send('User created successfully');
} catch (error) {
res.status(400).send(error);
}
});

const loginuser = asyncHandler(async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user) return res.status(401).send('Invalid email or password');

const isMatch = await user.comparePassword(password);
if (!isMatch) return res.status(401).send('Invalid email or password');

const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' });
res.cookie('token', token, { httpOnly: true });
res.send('Logged in successfully');
} catch (error) {
res.status(400).send(error);
}
});

const logoutuser = asyncHandler(async (req, res) => {
res.clearCookie('token');
res.send('Logged out successfully');
});

module.exports = {signup,loginuser,logoutuser};
51 changes: 11 additions & 40 deletions BACKEND/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
const express = require('express');
const router = express.Router();
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');

router.post('/signup', async (req, res) => {
const { username, email, password } = req.body;
try {
const user = new User({ username, email, password });
await user.save();
res.status(201).send('User created successfully');
} catch (error) {
res.status(400).send(error);
}
});

router.post('/login', async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user) return res.status(401).send('Invalid email or password');

const isMatch = await user.comparePassword(password);
if (!isMatch) return res.status(401).send('Invalid email or password');

const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' });
res.cookie('token', token, { httpOnly: true });
res.send('Logged in successfully');
} catch (error) {
res.status(400).send(error);
}
});

router.post('/logout', (req, res) => {
res.clearCookie('token');
res.send('Logged out successfully');
});

module.exports = router;
const express=require("express");
const {
signup,
loginuser,
logoutuser
}=require("../controllers/authcontrollers.js");
const router=express.Router();
router.get("/signup",signup);
router.get("/login",loginuser);
router.get("/logout",logoutuser);
module.exports=router;
15 changes: 13 additions & 2 deletions form/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<div class="container">
Expand Down Expand Up @@ -33,6 +33,17 @@ <h2>Register</h2>
<button type="submit" class="button">Submit</button>
</form>
</div>
<script src="script.js"></script>
<div id="google_element"></div>
<script type="text/javascript" src="http://translate.google.com/translate_a/element.js?cb=loadGoogleTranslate"></script>

<script type="text/javascript">
function loadGoogleTranslate(){
new google.translate.TranslateElement(
"google_element");
}


</script>
<script src="./script.js"></script>
</body>
</html>
Loading

0 comments on commit 3220576

Please sign in to comment.