Spaces:
Paused
Paused
Update app.js
Browse files
app.js
CHANGED
@@ -67,6 +67,7 @@ app.use(session({
|
|
67 |
}
|
68 |
}));
|
69 |
|
|
|
70 |
app.post('/api/login', async (req, res) => {
|
71 |
const { email, password } = req.body;
|
72 |
try {
|
@@ -86,20 +87,20 @@ app.post('/api/login', async (req, res) => {
|
|
86 |
|
87 |
// Session handling
|
88 |
req.session.user = user; // Save user to session
|
89 |
-
res.status(200).send('Login successful');
|
90 |
} catch (error) {
|
91 |
logger.error('Login error:', error);
|
92 |
-
|
93 |
-
//
|
94 |
if (error.message && error.message.startsWith('Unexpected token')) {
|
95 |
-
res.status(500).send(error
|
96 |
} else {
|
97 |
-
res.status(500).send('Internal server error');
|
98 |
}
|
99 |
}
|
100 |
});
|
101 |
|
102 |
-
// Register route for user creation with bcrypt hashing
|
103 |
app.post('/api/register', async (req, res) => {
|
104 |
const { email, password } = req.body;
|
105 |
try {
|
@@ -107,14 +108,14 @@ app.post('/api/register', async (req, res) => {
|
|
107 |
const newUser = new User({ email, password: hashedPassword });
|
108 |
|
109 |
await newUser.save();
|
110 |
-
res.status(201).send('User registered successfully');
|
111 |
} catch (error) {
|
112 |
logger.error('Registration error:', error);
|
113 |
res.status(500).send('Internal server error');
|
114 |
}
|
115 |
});
|
116 |
|
117 |
-
// Hugging Face API Example Function
|
118 |
async function fetchHuggingFaceData(apiUrl) {
|
119 |
try {
|
120 |
const response = await fetch(apiUrl);
|
|
|
67 |
}
|
68 |
}));
|
69 |
|
70 |
+
// POST login route
|
71 |
app.post('/api/login', async (req, res) => {
|
72 |
const { email, password } = req.body;
|
73 |
try {
|
|
|
87 |
|
88 |
// Session handling
|
89 |
req.session.user = user; // Save user to session
|
90 |
+
res.status(200).send({ message: 'Login successful' });
|
91 |
} catch (error) {
|
92 |
logger.error('Login error:', error);
|
93 |
+
|
94 |
+
// Send generic error message if unexpected token error occurs
|
95 |
if (error.message && error.message.startsWith('Unexpected token')) {
|
96 |
+
return res.status(500).send('Internal server error: invalid response format');
|
97 |
} else {
|
98 |
+
return res.status(500).send('Internal server error');
|
99 |
}
|
100 |
}
|
101 |
});
|
102 |
|
103 |
+
// Register route for user creation with bcrypt hashing
|
104 |
app.post('/api/register', async (req, res) => {
|
105 |
const { email, password } = req.body;
|
106 |
try {
|
|
|
108 |
const newUser = new User({ email, password: hashedPassword });
|
109 |
|
110 |
await newUser.save();
|
111 |
+
res.status(201).send({ message: 'User registered successfully' });
|
112 |
} catch (error) {
|
113 |
logger.error('Registration error:', error);
|
114 |
res.status(500).send('Internal server error');
|
115 |
}
|
116 |
});
|
117 |
|
118 |
+
// Hugging Face API Example Function (to demonstrate API interaction)
|
119 |
async function fetchHuggingFaceData(apiUrl) {
|
120 |
try {
|
121 |
const response = await fetch(apiUrl);
|