-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
37 lines (31 loc) · 870 Bytes
/
helpers.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
const Poem = require("./models/poem");
const Annotation = require("./models/annotation");
const mongoose = require("mongoose");
const compareObjectIds = (id1, id2) => {
return JSON.stringify(id1) === JSON.stringify(id2);
}
const isAuth = (req, res, next) => {
if (!req.session.user_id) {
res.redirect("/sign_in");
return;
}
next();
};
const hasAccess = (itemId, userId) => {
return compareObjectIds(itemId, userId);
};
const youtube_parser = (url) => {
let regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
let match = url.match(regExp);
return (match&&match[7].length===11)? match[7] : false;
};
const randomObjectId = () => {
return new mongoose.Types.ObjectId();
}
module.exports = {
compareObjectIds,
isAuth,
youtube_parser,
hasAccess,
randomObjectId
}