no1b4me commited on
Commit
c129e9b
·
verified ·
1 Parent(s): e9b9b55

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +44 -0
app.js CHANGED
@@ -7,6 +7,8 @@ const path = require('path');
7
  const fetch = require('node-fetch');
8
  const cors = require('cors');
9
  const logger = require('./logger');
 
 
10
 
11
  const app = express();
12
  const port = process.env.PORT || 3001;
@@ -65,6 +67,48 @@ app.use(session({
65
  }
66
  }));
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  // Hugging Face API Example Function
69
  async function fetchHuggingFaceData(apiUrl) {
70
  try {
 
7
  const fetch = require('node-fetch');
8
  const cors = require('cors');
9
  const logger = require('./logger');
10
+ const bcrypt = require('bcrypt');
11
+ const User = require('./models/User'); // Assuming User model is defined for your MongoDB users
12
 
13
  const app = express();
14
  const port = process.env.PORT || 3001;
 
67
  }
68
  }));
69
 
70
+ // Example login route
71
+ app.post('/api/login', async (req, res) => {
72
+ const { email, password } = req.body;
73
+ try {
74
+ logger.info('Attempting login for email:', email);
75
+
76
+ const user = await User.findOne({ email });
77
+ if (!user) {
78
+ logger.info('User not found');
79
+ return res.status(401).send('User not found');
80
+ }
81
+
82
+ const isMatch = await bcrypt.compare(password, user.password);
83
+ if (!isMatch) {
84
+ logger.info('Invalid credentials');
85
+ return res.status(401).send('Invalid credentials');
86
+ }
87
+
88
+ // Session handling
89
+ req.session.user = user; // Save user to session
90
+ res.status(200).send('Login successful');
91
+ } catch (error) {
92
+ logger.error('Login error:', error);
93
+ res.status(500).send('Internal server error');
94
+ }
95
+ });
96
+
97
+ // Register route for user creation with bcrypt hashing (assuming you're registering users)
98
+ app.post('/api/register', async (req, res) => {
99
+ const { email, password } = req.body;
100
+ try {
101
+ const hashedPassword = await bcrypt.hash(password, 10);
102
+ const newUser = new User({ email, password: hashedPassword });
103
+
104
+ await newUser.save();
105
+ res.status(201).send('User registered successfully');
106
+ } catch (error) {
107
+ logger.error('Registration error:', error);
108
+ res.status(500).send('Internal server error');
109
+ }
110
+ });
111
+
112
  // Hugging Face API Example Function
113
  async function fetchHuggingFaceData(apiUrl) {
114
  try {