Add dev code to test

This commit is contained in:
Modo 2023-10-15 11:59:51 +05:30
parent c60f635a89
commit 762dd9974c
6 changed files with 1197 additions and 5 deletions

38
devfolder/app.js Normal file
View File

@ -0,0 +1,38 @@
const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken'); // Import the jsonwebtoken library
const app = express();
const PORT = 5000;
const cors = require('cors');
const corsOptions = {
origin: 'http://127.0.0.1:5500', // Replace with your frontend's origin
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
const secretKey = 'your-secret-key'; // Replace with a strong secret key for encoding the JWT token
app.post('/api/auth', (req, res) => {
const { user_name, password } = req.body;
// Check the username and password (for demonstration purposes, accept any non-empty values)
if (user_name && password) {
// Authentication successful, create a JWT token
const token = jwt.sign({ user_name }, secretKey, { expiresIn: '1h' }); // Token expires in 1 hour
res.json({ token });
} else {
// Authentication failed
res.status(401).json({ error: 'Invalid username or password' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

View File

@ -44,5 +44,5 @@
<script src="index.js"></script> <script src="index.js"></script>
</body> </body>
<footer>HI</footer> <footer><p>© 2023 Password Manager</p></footer>
</html> </html>

View File

@ -43,7 +43,7 @@ async function getAuth() {
let _username = document.getElementById("username").value; let _username = document.getElementById("username").value;
let _password = document.getElementById("password").value; let _password = document.getElementById("password").value;
try { try {
const response = await fetch('http://127.0.0.1:5000/', { const response = await fetch('http://127.0.0.1:5000/api/auth', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -62,6 +62,8 @@ let _password = document.getElementById("password").value;
const token = data.token; const token = data.token;
console.log(`Got token: ${token}`) console.log(`Got token: ${token}`)
console.log(data)
handleAuthRes(data);
} catch (error) { } catch (error) {
console.log('Error', error); console.log('Error', error);
} }
@ -70,7 +72,7 @@ let _password = document.getElementById("password").value;
function handleAuthRes(data) { function handleAuthRes(data) {
if(data.token){ if(data.token){
localStorage.setItem('token', token.data); localStorage.setItem('token', data.token);
console.log("Logged In...") console.log("Logged In...")
} else { } else {
console.error('Login Failed:', data.error) console.error('Login Failed:', data.error)

View File

@ -29,8 +29,8 @@ body {
backdrop-filter: blur(20px); backdrop-filter: blur(20px);
justify-content: center; justify-content: center;
align-items: center; align-items: center;
width: 500px; width: 450px;
height: 450px; height: 400px;
flex-direction: column; flex-direction: column;
padding: 70px; padding: 70px;
padding-bottom: 90px; padding-bottom: 90px;
@ -155,4 +155,9 @@ footer{
background-color: rgb(12, 12, 12); background-color: rgb(12, 12, 12);
height: 50px; height: 50px;
width: 100vw; width: 100vw;
}
footer p{
color: white;
text-align: center;
} }

1127
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "unnamed-password-manager",
"version": "1.0.0",
"description": "Hi",
"main": "index.js",
"scripts": {
"start": "nodemon devfolder/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^3.0.1"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2"
}
}