forked from steren/cloud-run-node-18
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
130 lines (110 loc) · 3.47 KB
/
server.js
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
122
123
124
125
126
127
128
129
130
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const path = require('path');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const User = require('./models/User');
const withAuth = require('./middleware');
const app = express();
const secret = 'mysecretsshhh';
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*'); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const mongo_uri = 'mongodb://localhost/react-auth';
mongoose.connect(mongo_uri, { useNewUrlParser: true, useUnifiedTopology: true }, function(err) {
if (err) {
throw err;
} else {
console.log(`Successfully connected to ${mongo_uri}`);
}
});
app.use(express.static(path.join(__dirname, 'public')));
// app.get('/', function (req, res) {
// res.sendFile(path.join(__dirname, 'public', 'index.html'));
// });
app.get('/api/home', function(req, res) {
res.send('Welcome!');
});
app.get('/api/secret', withAuth, function(req, res) {
res.send('The password is potato');
});
app.post('/api/register', function(req, res) {
const { email, password } = req.body;
const user = new User({ email, password });
user.save(function(err) {
if (err) {
console.log(err);
res.status(500).send("Error registering new user please try again.");
} else {
res.status(200).send("Welcome to the club!");
}
});
});
app.post('/api/authenticate', function(req, res) {
const { email, password } = req.body;
User.findOne({ email }, function(err, user) {
if (err) {
console.error(err);
res.status(500)
.json({
error: 'Internal error please try again'
});
} else if (!user) {
res.status(401)
.json({
error: 'Incorrect email or password'
});
} else {
user.isCorrectPassword(password, function(err, same) {
if (err) {
res.status(500)
.json({
error: 'Internal error please try again'
});
} else if (!same) {
res.status(401)
.json({
error: 'Incorrect email or password'
});
} else {
// Issue token
const payload = { email };
const token = jwt.sign(payload, secret, {
expiresIn: '1h'
});
res.cookie('token', token, { httpOnly: true }).sendStatus(200);
}
});
}
});
});
app.post('/api/login', function(req, res, next) {
const { email, password } = req.body;
const payload = { email };
const token = jwt.sign(payload, secret, {
expiresIn: '1h'
});
res.cookie('token', token, { httpOnly: true }).sendStatus(200);
});
// app.listen(process.env.PORT || 8080);
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log('Node.js demo is listening on port', port);
});
app.get('/', async (req, res) => {
// const french = new Intl.DateTimeFormat('fr', { weekday: 'long' });
// // see https://www.weather.gov/documentation/services-web-api
// const response = await fetch('https://api.weather.gov/gridpoints/MTR/85,105/forecast');
// const weatherData = await response.json();
res.send(
`
Congrats, you're running Node.js ${process.version}.<br><br>.<br>
`
);
});