-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
263 lines (228 loc) · 5.97 KB
/
main.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import express from 'express';
import morgan from 'morgan';
import { engine } from 'express-handlebars';
import cookieParser from 'cookie-parser';
import { PrismaClient } from '@prisma/client';
import * as Sentry from '@sentry/node';
import { captureException } from '@sentry/node';
import * as Tracing from '@sentry/tracing';
import { comparePassword, hashPassword } from './lib/hashPassword';
import { getJwt, getUserFromJwt } from './lib/getJwt';
import { getUrlId } from './lib/getUrlId';
import { config } from 'dotenv';
config();
console.log('🏖 Starting server...');
const app = express();
// Constants
const TOKEN_NAME = 'citypoppin-token';
// Sentry
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV || 'production',
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({ app }),
],
tracesSampleRate: 0.5,
});
// Middleware
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static('public'));
app.use(cookieParser());
if (process.env.NODE_ENV === 'development') {
// small logs for development
app.use(morgan('dev'));
} else {
// large logs for production
app.use(morgan('combined'));
}
// Handlebars engine
app.engine(
'.hbs',
engine({
extname: '.hbs',
helpers: {
// Helper to check if a value is equal to another
eq: (a: string, b: string) => a === b,
},
})
);
app.set('view engine', '.hbs');
app.set('views', './views');
// Routes
app.get('/', async (req, res) => {
try {
if (req.cookies[TOKEN_NAME]) {
const user = await getUserFromJwt(req.cookies[TOKEN_NAME]);
res.redirect(307, '/links')
} else {
// If no user just render the login page
res.render('landing', { error: req.query.error });
}
} catch (e) {
// Always render landing in case something breaks
res.render('landing', { error: req.query.error });
}
});
app.get('/health', (req, res) => {
res.send('ok');
});
app.get('/signup', (req, res) => {
res.render('signup', { error: req.query.error });
});
app.post('/signup', async (req, res) => {
const prisma = new PrismaClient();
const { email, password, invitationCode } = req.body;
const hashedPassword = await hashPassword(password);
try {
const invite = await prisma.invitationCode.findUnique({
where: {
code: invitationCode,
},
});
// check if invitation code is valid
if (!invite || invite?.isUsed) {
res.redirect('/signup?error=invalid');
return;
}
// Create user
const user = await prisma.user.create({
data: {
email,
password: hashedPassword,
},
});
// Update invitation code
await prisma.invitationCode.update({
where: {
code: invitationCode,
},
data: {
isUsed: true,
},
});
res.cookie(TOKEN_NAME, getJwt(user), {
httpOnly: true,
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30), // 30 days
});
res.redirect('/links');
} catch (e) {
console.error(e);
captureException(e);
res.redirect('/signup?error=invalid');
}
});
app.post('/login', async (req, res) => {
const prisma = new PrismaClient();
const { email, password } = req.body;
const user = await prisma.user.findUnique({
where: {
email,
},
});
// Check if user exists and password is correct
const passwordMatch = await comparePassword(password, user?.password || '');
if (!passwordMatch || !user) {
res.redirect('/?error=login');
return;
}
// User is authenticated
const jwt = getJwt(user);
res.cookie(TOKEN_NAME, jwt, {
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30), // 30 days
httpOnly: true,
});
res.redirect('/links');
});
app.post('/links', async (req, res) => {
const prisma = new PrismaClient();
try {
if (req.body.linkTo === '') {
return res.redirect('/links?error=linkTo');
}
const user = await getUserFromJwt(req.cookies[TOKEN_NAME]);
console.log(user);
const link = await prisma.shortLink.create({
data: {
slug: req.body.slug || getUrlId(),
linkTo: req.body.linkTo,
owner: {
connect: {
id: user.id,
},
},
},
});
console.log('🔗 Created link ' + link.slug + ' for user ' + user.email);
res.redirect('/links?success=' + link.slug);
} catch (e) {
console.error(e);
captureException(e);
if ((e as Error).message.includes('Unique constraint failed')) {
return res.redirect('/links?error=slugInvalid');
}
// TODO: handle other errors
res.sendStatus(400);
}
});
app.get('/links', async (req, res) => {
const prisma = new PrismaClient();
try {
const cookie = req.cookies[TOKEN_NAME];
if (!cookie) {
res.redirect('/');
} else {
const user = getUserFromJwt(cookie);
const links = await prisma.shortLink.findMany({
where: {
ownerId: user.id,
},
});
res.render('links', {
links,
error: req.query.error,
success: req.query.success,
});
}
} catch (e) {
console.error(e);
captureException(e);
res.sendStatus(400);
}
});
app.get('/:slug', async (req, res) => {
const prisma = new PrismaClient();
try {
const link = await prisma.shortLink.update({
where: {
slug: req.params.slug,
},
select: {
linkTo: true,
},
data: {
clicks: {
increment: 1,
},
},
});
if (!link) {
res.sendStatus(404);
} else {
res.redirect(link.linkTo);
}
} catch (e) {
console.error(e);
captureException(e);
res.sendStatus(400);
}
});
// Error handlers (must be after all routes)
app.use(Sentry.Handlers.errorHandler());
const port = process.env.PORT || 5000;
app.listen(Number(port), '0.0.0.0', () => {
console.log('☁️ App listening on port ' + port);
});