-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (45 loc) · 1.64 KB
/
Copy pathserver.js
File metadata and controls
59 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import express from "express"
import dotenv from "dotenv"
import User from "./models/user.js"
import cors from "cors"
import mongoose from "mongoose"
dotenv.config()
const app = express()
app.use(express.json())
app.use(cors())
const PORT = process.env.PORT || 3000
mongoose.connect(process.env.MONGODB_URI)
.then(() => console.log(`MongoDB SUCCESFULLY CONNECTED`))
.catch((err) => err.message)
// signup endpoint
app.post("/api/signup", async(req,res) => {
const {name, email, password} = req.body
if(!name || !email || !password){
res.status(401).json({success: false, message: "Name, email, password required"})
}
const existingUser = await User.findOne({ email })
if(existingUser){
return res.status(409).json({success: false, message: "User already exists"})
}
const newUser = new User({ name, email, password })
await newUser.save()
res.status(201).json({success: true, message: "User created successfully"})
})
//login endpoint
app.post("/api/login", async(req,res) => {
const {email, password} = req.body
if(!email || !password){
return res.status(401).json({success: false, message: "Email and password required"})
}
const existingUser = await User.findOne({ email: email })
if(!existingUser){
return res.status(404).json({success: false, message: "User not found"})
}
if(existingUser.password !== password){
return res.status(401).json({success: false, message: "Invalid password"})
}
res.status(200).json({success: true, message: "Login successful"})
})
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
})