-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
89 lines (76 loc) · 2.02 KB
/
app.js
File metadata and controls
89 lines (76 loc) · 2.02 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import express from "express";
import * as dotenv from "dotenv";
import bodyParser from "body-parser";
import User from "./models/User.js";
import mongoose from "mongoose";
import md5 from "./utils/md5.js";
import auth from "./middleware/auth.js ";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
const secretKey = process.env.JTW_SECRET;
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log(err));
app.use(bodyParser.json());
app.post("/register", async (req, res) => {
const { name, email, password } = req.body;
const emailStatus = await User.findOne({ email });
if (emailStatus) {
res.status(400).json({
message: "Zaten bu mail adresinde bir kullanıcı kayıtlı",
status: false,
});
return false;
}
try {
const user = User({
name,
email,
password: password,
});
const savedUser = await user.save();
res.status(201).json(savedUser);
} catch (err) {
console.log(err);
res.status(400).json({
message: "Server Error",
});
}
});
app.get("/authenticate", auth, async (req, res) => {
res.json({ user: req.user });
});
app.post("/login", async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user) {
res.status(401).json({ message: "Authentication failed!" });
return false;
} else {
const md5Pass = md5(password);
console.log(user.password);
if (user.password != md5Pass) {
res.status(401).json({ message: "Authentication failed!" });
return false;
}
}
const token = await user.generateAuthToken();
res.status(200).json({
user,
token,
});
return true;
} catch (err) {
console.log(err);
res.status(500).json({ message: "Server error" });
}
});
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});