-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.nim
72 lines (58 loc) · 2.71 KB
/
admin.nim
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
# Wallets that bought NFT
# Wallet address: string
# When they bought: datetime string
# Which NFT they bought 0..48 : seq[int]
import prologue
import mynimlib/[nimtinydb, icecream, nimjwtauth]
import strutils
import strformat
import site_comps
import karax / [karaxdsl, vdom]
import consts
#####################
## Handlers #########
#####################
proc login*(ctx: Context) {.async.} =
case ctx.request.reqMethod:
of HttpGet:
let node = buildHtml(tdiv( class="bg-[#45474F99] p-4 rounded-lg max-w-xs mx-auto my-8")):
form( action="/login", `method`="post"):
tdiv( class="`for`m-group mb-4"):
input( `type`="password", id="password", name="password", placeholder="Enter password", class="w-full p-2 text-xl text-white bg-transparent border-b-2 border-white outline-none placeholder-white focus:border-[#D02F3A]")
button(`type`="submit", class="w-full text-xl text-white bg-[#D02F3A] px-4 py-2 rounded-lg transition duration-300 hover:scale-105"):
vdom.text "Submit"
resp base $node
of HttpPost:
let password = ctx.getFormParamsOption("password")
if password.isNone:
icr "No password"
resp redirect "/login"
if password.get == consts.password:
icr "Correct password"
#ctx.push_access_token("admin", consts.jwt_secret, %*{}, consts.admin_access_token)
resp jwtRedirect("/admin", "admin", consts.jwt_secret, %*{}, consts.admin_access_token)
#resp redirect "/admin"
else:
icr "Wrong password"
resp redirect "/login"
else:
discard
proc admin*(ctx: Context) {.async.} =
let current_user = ctx.get_current_user(
consts.admin_access_token,
consts.jwt_secret,
true,
30.days,
false
)
let is_admin = current_user.is_authenticated
if not is_admin:
resp redirect "/login"
return
let db = newTinyDB("/root/db.json")
resp base """<a class="text-white" href = "https://jsonformatter.org/json-pretty-print" target="_blank"> https://jsonformatter.org/json-pretty-print </a>""" & fmt"""<div class="text-white" > {$db.allRaw()} </div>"""
#####################
## Routes ###########
#####################
let admin_route* = pattern("/admin" , admin)
let login_route* = pattern("/login" , login, @[HttpGet, HttpPost])