forked from jonathanschimpf/Table-Turner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
121 lines (89 loc) · 3.01 KB
/
server.js
File metadata and controls
121 lines (89 loc) · 3.01 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const PORT = process.env.PORT || 3001;
const cors = require("cors");
const passport = require("passport");
// const passportLocal = require("passport-local").Strategy;
const cookieParser = require("cookie-parser");
const bcrypt = require("bcryptjs");
const session = require("express-session");
const bodyParser = require("body-parser");
const User = require("./models/user");
const app = express();
// const app = expressPassport();
const routes = require("./routes");
// -------------- End Of Imports ------------------- //
// --- Middleware --- //
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Add routes, both API and view
app.use(routes);
// --- Connect to Mongoose --- //
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/Project-Three", { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true });
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cors({
origin: "http://localhost:3000", // <-- location of the react app we're connecting to
credentials: true
}))
app.use(session({
secret: "secretcode",
resave: true,
saveUnintialized: true
}));
app.use(cookieParser("secretcode"))
app.use(passport.initialize());
app.use(passport.session());
require('./passportConfig')(passport);
// --- End of Middleware --- //
// --- Routes for login --- //
// !! Can be Moved to routes folder when finished !! //
app.post ("/api/login", (req, res, next) => {
passport.authenticate("local", (err, user, info) => {
if (err) throw err;
if (!user) res.send("No User Exists");
else {
req.logIn(user, (err) => {
if (err) throw err;
res.send('Successfully Authenticated User');
console.log(req.user);
});
}
})(req, res, next)
})
app.post ("/api/register", (req, res) => {
User.findOne({username: req.body.username}, async (err, doc) => {
if (err) throw err;
if (doc) res.send("User Already Exists");
if(!doc){
const hashedPassword = await bcrypt.hash(req.body.password, 10)
const newUser = new User({
username: req.body.username,
password: hashedPassword,
title: req.body.title
});
await newUser.save();
res.send("User Created");
}
});
});
app.get ("/user", (req, res) => {
res.send(req.user) // <--- this is where the entire user is stored .. can be used elsewhere in app
})
app.get("/api/logout", function(req, res) {
req.logout();
res.redirect("/");
});
// Serve up static assets (usually on heroku) -- commented out till we run npm build -- //
// Send every request to the React app
// Define any API routes before this runs
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.listen(PORT, function () {
console.log(`🌎 ==> API server now on port ${PORT}!`);
});