-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (118 loc) · 3.61 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
const Koa = require("koa");
const Router = require("koa-router");
const BodyParser = require("koa-bodyparser");
const ObjectID = require("mongodb").ObjectID;
const app = new Koa();
require("./includes/mongo")(app);
const jwt = require("./includes/jwt");
const router = new Router();
const routerNotes = new Router();
const routerNotSecure = new Router();
app.use(BodyParser());
router.use(jwt.errorHandler()).use(jwt.jwt());
var checkOwnerOrRole = function(ctx) {
console.log(ctx.state);
if(ctx.state.user.username == ctx.state.username.login) {
return true;
}
if(ctx.state.user.role != 'admin') {
ctx.status = 401;
ctx.body = {
"error": "Not authorized"
};
return false
}
return true;
}
router.get("/", function (ctx) {
ctx.body = {message: "Hello World!"}
});
router.get("/users", async (ctx) => {
console.log(ctx.state);
ctx.body = await ctx.app.users.find().toArray();
});
router.post("/users", async (ctx) => {
ctx.body = await ctx.app.users.insert(ctx.request.body);
});
router.get("/users/:username", (ctx) => {
console.log(ctx.state);
//ctx.body = await ctx.app.users.findOne({"login":ctx.params.username});
ctx.body = ctx.state.username;
});
router.put("/users/:username", async (ctx) => {
let documentQuery = {"login": ctx.params.username};
let valuesToUpdate = ctx.request.body;
ctx.body = await ctx.app.users.updateOne(documentQuery, valuesToUpdate);
});
router.delete("/users/:username", async (ctx) => {
let documentQuery = {"login": ctx.params.username};
ctx.body = await ctx.app.users.deleteOne(documentQuery);
});
router.get("/users/:username/notes", async (ctx) => {
if(checkOwnerOrRole(ctx)) {
ctx.body = await ctx.app.notes.find({
login: ctx.state.username.login
}).toArray();
}
});
router.post("/users/:username/notes", async (ctx) => {
if(checkOwnerOrRole(ctx)) {
ctx.request.body.login = ctx.state.username.login;
ctx.body = await ctx.app.notes.insert(ctx.request.body);
}
});
router.get("/users/:username/notes/:id", async (ctx) => {
if(checkOwnerOrRole(ctx)) {
ctx.body = await ctx.app.notes.findOne({
"_id": ObjectID(ctx.params.id),
login: ctx.state.username.login});
}
});
router.put("/users/:username/notes/:id", async (ctx) => {
if(checkOwnerOrRole(ctx)) {
let documentQuery = {
login: ctx.state.username.login,
"_id": ObjectID(ctx.params.id)
};
let valuesToUpdate = ctx.request.body;
ctx.body = await ctx.app.notes.updateOne(documentQuery, valuesToUpdate);
}
});
router.delete("/users/:username/notes/:id", async (ctx) => {
if(checkOwnerOrRole(ctx)) {
let documentQuery = {
login: ctx.state.username.login,
"_id": ObjectID(ctx.params.id)
};
ctx.body = await ctx.app.users.deleteOne(documentQuery);
}
});
router.param('username', async (id, ctx, next) => {
ctx.state.username = await ctx.app.users.findOne({"login":ctx.params.username});
if (!ctx.state.username) return ctx.status = 404;
return next();
});
routerNotSecure.post("/login", async (ctx) => {
let username = ctx.request.body.username;
let password = ctx.request.body.password;
let user = await ctx.app.users.findOne({
login: ctx.request.body.username,
password: ctx.request.body.password
});
console.log(user);
if (user) {
ctx.body = {
token: jwt.issue({
username: user.login,
role: user.role
})
}
} else {
ctx.status = 401;
ctx.body = {error: "Invalid login"}
console.log('failed');
}
});
app.use(router.routes()).use(router.allowedMethods());
app.use(routerNotSecure.routes()).use(routerNotSecure.allowedMethods());
app.listen(3000);