Skip to content

Commit

Permalink
web-authentication-documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
learnwithfair committed May 2, 2024
0 parents commit 81c03d4
Show file tree
Hide file tree
Showing 83 changed files with 28,384 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
70 changes: 70 additions & 0 deletions Level 1_ databse-matching authentication/app.js
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 Level 1_ databse-matching authentication/models/user.model.js
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);
Loading

0 comments on commit 81c03d4

Please sign in to comment.