-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 81c03d4
Showing
83 changed files
with
28,384 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// match from database authentication | ||
require("dotenv").config(); | ||
const express = require("express"); | ||
const cors = require("cors"); | ||
const mongoose = require("mongoose"); | ||
const User = require("./models/user.model"); | ||
|
||
const app = express(); | ||
const PORT = process.env.PORT || 5000; | ||
const dbURL = process.env.MONGO_URL; | ||
|
||
mongoose | ||
.connect(dbURL) | ||
.then(() => { | ||
console.log("mongodb atlas is connected"); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
process.exit(1); | ||
}); | ||
|
||
app.use(cors()); | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
|
||
app.get("/", (req, res) => { | ||
res.sendFile(__dirname + "/./views/index.html"); | ||
}); | ||
|
||
app.post("/register", async (req, res) => { | ||
try { | ||
const newUser = new User(req.body); | ||
await newUser.save(); | ||
res.status(201).json(newUser); | ||
} catch (error) { | ||
res.status(500).json(error.message); | ||
} | ||
}); | ||
|
||
app.post("/login", async (req, res) => { | ||
try { | ||
const { email, password } = req.body; | ||
const user = await User.findOne({ email: email }); | ||
if (user && user.password === password) { | ||
res.status(200).json({ status: "valid user" }); | ||
} else { | ||
res.status(404).json({ status: "Not valid user" }); | ||
} | ||
} catch (error) { | ||
res.status(500).json(error.message); | ||
} | ||
}); | ||
|
||
// route not found error | ||
app.use((req, res, next) => { | ||
res.status(404).json({ | ||
message: "route not found", | ||
}); | ||
}); | ||
|
||
//handling server error | ||
app.use((err, req, res, next) => { | ||
res.status(500).json({ | ||
message: "something broke", | ||
}); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`server is running at http://localhost:${PORT}`); | ||
}); |
18 changes: 18 additions & 0 deletions
18
Level 1_ databse-matching authentication/models/user.model.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
require: true, | ||
}, | ||
password: { | ||
type: String, | ||
require: true, | ||
}, | ||
createdOn: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}); | ||
|
||
module.exports = mongoose.model("user", userSchema); |
Oops, something went wrong.