Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Yadhu567 authored Jun 18, 2024
1 parent 6e50258 commit d994d20
Show file tree
Hide file tree
Showing 4 changed files with 1,213 additions and 0 deletions.
109 changes: 109 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const cors = require('cors');

const app = express();
app.use(bodyParser.json());
app.use(cors())
const User = require('./models/User');

mongoose.connect('mongodb://localhost:27017/user', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));


app.listen(3000, () => {
console.log('Server running on port 3000');
});

app.post('/signup', async (req, res) => {
const { name, email, password } = req.body;

if (!name || !email || !password) {
return res.status(400).send('All fields are required');
}

const existingUser = await User.findOne({ email });

if (existingUser) {
return res.status(400).send('User already exists');
}

const hashedPassword = await bcrypt.hash(password, 10);

const user = new User({
name,
email,
password: hashedPassword
});

await user.save();

res.status(201).send('User created');
});

app.post('/signin', async (req, res) => {
const { email, password } = req.body;

if (!email || !password) {
return res.status(400).send('All fields are required');
}

const user = await User.findOne({ email });

if (!user) {
return res.status(400).send('Invalid email or password');
}

const isPasswordValid = await bcrypt.compare(password, user.password);

if (!isPasswordValid) {
return res.status(400).send('Invalid email or password');
}

const token = jwt.sign({ userId: user._id }, 'your_jwt_secret');

res.send({ token });
});

// Middleware to authenticate JWT
const authenticateJWT = (req, res, next) => {
const token = req.header('Authorization').replace('Bearer ', '');

if (!token) {
return res.status(401).send('Access Denied');
}

try {
const verified = jwt.verify(token, 'your_jwt_secret');
req.user = verified;
next();
} catch (err) {
res.status(400).send('Invalid Token');
}
};

app.get('/latest-prediction', authenticateJWT, async (req, res) => {
const user = await User.findById(req.user.userId);

if (!user || user.detections.length === 0) {
return res.status(404).send('No predictions found');
}

const latestPrediction = user.detections[user.detections.length - 1];

res.send(latestPrediction);
});

app.get('/all-predictions', authenticateJWT, async (req, res) => {
const user = await User.findById(req.user.userId);

if (!user) {
return res.status(404).send('No predictions found');
}

res.send(user.detections);
});
32 changes: 32 additions & 0 deletions backend/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// models/user.js
const mongoose = require('mongoose');

const DetectionSchema = new mongoose.Schema({
documentno: String,
area_name: String,
animal_name: String,
confidence: Number,
time: String,
animal_image: String,
sound_file: String,
no_detection: String
}, { _id: false });

const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
detections: [DetectionSchema]
});

module.exports = mongoose.model('User', UserSchema, 'users');
Loading

0 comments on commit d994d20

Please sign in to comment.