made user login and Schema
This commit is contained in:
parent
8a805b96a6
commit
53f2eeab84
55
src/index.ts
Normal file
55
src/index.ts
Normal file
@ -0,0 +1,55 @@
|
||||
const express = require("express")
|
||||
const app = express()
|
||||
const path = require("path")
|
||||
const LoginModel = require("./mongodb")
|
||||
|
||||
|
||||
app.post("/signup", async (req, res)=>{
|
||||
const data = {
|
||||
email:req.body.email,
|
||||
username:req.body.name,
|
||||
password:req.body.pass
|
||||
}
|
||||
try {
|
||||
await LoginModel.insertMany([data])
|
||||
// Render success or error page
|
||||
res.send("Signup successful");
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
// Handle error and send appropriate response
|
||||
res.status(500).send("Error signing up");
|
||||
}
|
||||
})
|
||||
|
||||
app.post("/login", async (req, res) => {
|
||||
try {
|
||||
const check = await LoginModel.findOne({ username: req.body.name });
|
||||
|
||||
if (check && check.password === req.body.password) {
|
||||
// Render home page or send success response
|
||||
res.send("Login successful");
|
||||
} else {
|
||||
// Send "Wrong password" response
|
||||
res.send("Wrong password");
|
||||
}
|
||||
} catch (error) {
|
||||
try {
|
||||
const check = await LoginModel.findOne({ email: req.body.email });
|
||||
|
||||
if (check && check.password === req.body.password) {
|
||||
// Render home page or send success response
|
||||
res.send("Login successful");
|
||||
} else {
|
||||
// Send "Wrong password" response
|
||||
res.send("Wrong password");
|
||||
}
|
||||
} catch (error) {
|
||||
// Send "Wrong username or email" response
|
||||
res.send("Wrong username or email");
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(3000, ()=>{
|
||||
console.log("Port connected")
|
||||
})
|
31
src/mongodb.ts
Normal file
31
src/mongodb.ts
Normal file
@ -0,0 +1,31 @@
|
||||
const mongoose = require("mongoose")
|
||||
|
||||
console.log("hello world")
|
||||
|
||||
mongoose.connect("mongodb://localhost:27017/Finvis")
|
||||
.then(()=>{
|
||||
console.log("Mongodb connected");
|
||||
})
|
||||
.catch(()=>{
|
||||
console.log("Failed to connect")
|
||||
})
|
||||
|
||||
const LoginSchema = new mongoose.Schema({
|
||||
email:{
|
||||
type:String,
|
||||
required:true,
|
||||
unique:true
|
||||
},
|
||||
username:{
|
||||
type:String,
|
||||
required:true,
|
||||
unique:true
|
||||
},
|
||||
password:{
|
||||
type:String,
|
||||
required:true,
|
||||
}
|
||||
})
|
||||
|
||||
const collection = new mongoose.model("collection1", LoginSchema)
|
||||
module.exports=collection
|
Reference in New Issue
Block a user