-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.ts
91 lines (69 loc) · 2.75 KB
/
routes.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
// database
import { Database } from 'https://deno.land/x/denodb/mod.ts';
import db from './db.ts';
import { Router, Status } from "https://deno.land/x/oak/mod.ts";
// include login for validation
import { checkCredentials, createCookie, checkToken } from './controllers/login.ts';
const router = new Router();
// TODO: create catch-all GET/POST app.use() ?
// TODO: chain middleware for (1) login and (2) contents?
router.post('/', (ctx) => {
console.debug(`new general request: ${ctx.request}`)
ctx.response.body = '{"res": "choose available route"}'
})
/**
* POST /login
* {user: {string}, pw: {string}}
*
*/
router.post('/login', async (ctx) => {
// ctx.response.headers.set('Access-Control-Allow-Origin', '*');
// TODO: add Logger plugin
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
console.debug(`${time}: new login request from ${ctx.request.ip}`);
if (!ctx.request.hasBody) {
ctx.throw(Status.BadRequest, "Bad Request");
}
// ctx.response.headers.set('Access-Control-Allow-Origin', '*');
const body = ctx.request.body();
let credentials;
if (body.type === "json") {
credentials = await body.value;
} else {
ctx.throw(Status.BadRequest, "Bad Request");
}
if (await checkCredentials(db, credentials) == true) {
// create token
let token = await createCookie(credentials)
// build response
ctx.response.body = token;
ctx.cookies.set('token', token) // cookie valid for session: https://doc.deno.land/https/deno.land/x/oak/mod.ts#Cookies
ctx.response.body = Status.OK
ctx.response.body = `{"status": ${Status.OK}, "res": "access granted", "username": "${credentials.username}", "token": "${token}"}`
} else {
ctx.response.status = Status.Unauthorized
ctx.response.body = `{"status": ${Status.Unauthorized}, "res": "access denied"}`
}
});
router.get('/profile/:userid', async (ctx) => {
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
console.debug(`${time} show userid ${ctx.params.userid}`);
//console.log(`headers: ${ctx.request.headers}`)
let token = ctx.cookies.get("token") !== undefined ? ctx.cookies.get("token") : undefined;
if (token) {
const result = await checkToken(token);
if(result){
ctx.response.status = Status.OK
ctx.response.body = `{"status": ${Status.OK}, "res": "token validated"}`
}else{
ctx.response.status = Status.Unauthorized
ctx.response.body = `{"status": ${Status.Unauthorized}, "res": "invalid token"}`
}
}else{
ctx.response.status = Status.Unauthorized
ctx.response.body = `{"status": ${Status.Unauthorized}, "res": "invalid token"}`
}
});
export default router;