rewrote documentation for functions and expressions
This commit is contained in:
@@ -5,6 +5,9 @@ Request = import("request.noja").Request;
|
|||||||
respond = import("response.noja").new;
|
respond = import("response.noja").new;
|
||||||
urlencoded = import("urlencoded.noja");
|
urlencoded = import("urlencoded.noja");
|
||||||
|
|
||||||
|
user_table = {};
|
||||||
|
session_table = {};
|
||||||
|
|
||||||
fun getCookie(req: Request, name: String) {
|
fun getCookie(req: Request, name: String) {
|
||||||
src = req.headers["Cookie"];
|
src = req.headers["Cookie"];
|
||||||
if src == none:
|
if src == none:
|
||||||
@@ -19,8 +22,9 @@ fun getSessionID(req: Request) {
|
|||||||
session_id, error = getCookie(req, "SESSID");
|
session_id, error = getCookie(req, "SESSID");
|
||||||
if error != none:
|
if error != none:
|
||||||
return none, error;
|
return none, error;
|
||||||
session_id = string.toInt(session_id);
|
if session_id == none:
|
||||||
return session_id;
|
return none;
|
||||||
|
return string.toInt(session_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getUsername(req: Request) {
|
fun getUsername(req: Request) {
|
||||||
@@ -31,18 +35,12 @@ fun getUsername(req: Request) {
|
|||||||
return username, session_id;
|
return username, session_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
fun redirect(endpoint) {
|
|
||||||
fun callback()
|
|
||||||
return respond(307, none, {'Location': endpoint});
|
|
||||||
return callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
user_table = {};
|
|
||||||
session_table = {};
|
|
||||||
|
|
||||||
router = newRouter();
|
router = newRouter();
|
||||||
|
|
||||||
router->plug("/", {GET: redirect("/login")});
|
router->plug("/", {
|
||||||
|
fun GET()
|
||||||
|
return respond(307, none, {"Location": "/login"});
|
||||||
|
});
|
||||||
|
|
||||||
router->plug("/home", {
|
router->plug("/home", {
|
||||||
|
|
||||||
@@ -79,23 +77,23 @@ router->plug("/login", {
|
|||||||
return respond(400, "You're already logged in");
|
return respond(400, "You're already logged in");
|
||||||
|
|
||||||
fun areValid(params) {
|
fun areValid(params) {
|
||||||
expect = {user: String, pass: String};
|
expect = {username: String, password: String};
|
||||||
return istypeof(expect, params)
|
return istypeof(expect, params)
|
||||||
and count(params.user) > 0
|
and count(params.username) > 0
|
||||||
and count(params.pass) > 0;
|
and count(params.password) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
params = urlencoded.parse(req.body);
|
params = urlencoded.parse(req.body);
|
||||||
if not areValid(params):
|
if not areValid(params):
|
||||||
return respond(400, "Invalid parameters");
|
return respond(400, "Invalid parameters");
|
||||||
username = params.user;
|
username = params.username;
|
||||||
password = params.pass;
|
password = params.password;
|
||||||
|
|
||||||
info = user_table[username];
|
info = user_table[username];
|
||||||
|
|
||||||
if info == none:
|
if info == none:
|
||||||
return respond(200, "No such user");
|
return respond(200, "No such user");
|
||||||
if info.pass != password:
|
if info != password:
|
||||||
return respond(200, "Wrong password");
|
return respond(200, "Wrong password");
|
||||||
|
|
||||||
# Log-in succeded
|
# Log-in succeded
|
||||||
@@ -125,26 +123,26 @@ router->plug("/signup", {
|
|||||||
return respond(400, "You're already logged in");
|
return respond(400, "You're already logged in");
|
||||||
|
|
||||||
fun areValid(params) {
|
fun areValid(params) {
|
||||||
expect = {user: String, pass: String};
|
expect = {username: String, password: String};
|
||||||
return istypeof(expect, params)
|
return istypeof(expect, params)
|
||||||
and count(params.user) > 0
|
and count(params.username) > 0
|
||||||
and count(params.pass) > 0;
|
and count(params.password) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
params = urlencoded.parse(req.body);
|
params = urlencoded.parse(req.body);
|
||||||
if not areValid(params):
|
if not areValid(params):
|
||||||
return respond(400, "Invalid parameters");
|
return respond(400, "Invalid parameters");
|
||||||
username = params.user;
|
username = params.username;
|
||||||
password = params.pass;
|
password = params.password;
|
||||||
|
|
||||||
if user_table[password] != none:
|
if user_table[password] != none:
|
||||||
return respond(200, "User already exists");
|
return respond(200, "User already exists");
|
||||||
|
|
||||||
# Signed up!
|
# Signed up!
|
||||||
user_table[username] = {pass: password};
|
user_table[username] = password;
|
||||||
session_id = random.generate(0, 1000);
|
session_id = random.generate(0, 1000);
|
||||||
session_table[session_id] = username;
|
session_table[session_id] = username;
|
||||||
|
|
||||||
# Respond
|
# Respond
|
||||||
message = string.cat("Welcome, ", username, "!");
|
message = string.cat("Welcome, ", username, "!");
|
||||||
headers = {
|
headers = {
|
||||||
@@ -165,9 +163,9 @@ router->plug("/logout", {
|
|||||||
return respond(401, "You're not logged in");
|
return respond(401, "You're not logged in");
|
||||||
|
|
||||||
session_table[session_id] = none;
|
session_table[session_id] = none;
|
||||||
|
|
||||||
return respond(303, "Goodbye!", {"Location": "/login"});
|
return respond(303, "Goodbye!", {"Location": "/login"});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
error(serve("127.0.0.1", 8080, router));
|
error(serve("127.0.0.1", 8080, router));
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<form action="/login" method="POST">
|
<form action="/login" method="POST">
|
||||||
<input type="text" name="user" placeholder="[Username]" />
|
<input type="text" name="username" placeholder="[Username]" />
|
||||||
<input type="password" name="pass" placeholder="[Password]" />
|
<input type="password" name="password" placeholder="[Password]" />
|
||||||
<input type="submit" value="Log-in" />
|
<input type="submit" value="Log-in" />
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<form action="/signup" method="POST">
|
<form action="/signup" method="POST">
|
||||||
<input type="text" name="user" placeholder="[Username]" />
|
<input type="text" name="username" placeholder="[Username]" />
|
||||||
<input type="password" name="pass" placeholder="[Password]" />
|
<input type="password" name="password" placeholder="[Password]" />
|
||||||
<input type="submit" value="Sign-up!" />
|
<input type="submit" value="Sign-up!" />
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -49,9 +49,9 @@ fun new() {
|
|||||||
router.table[name] = methods;
|
router.table[name] = methods;
|
||||||
|
|
||||||
|
|
||||||
fun getRouteAllowedMethods(route) {
|
fun getRouteAllowedMethods(router: Router, route) {
|
||||||
|
|
||||||
method_table = route_table[route];
|
method_table = router.table[route];
|
||||||
if method_table == none:
|
if method_table == none:
|
||||||
return none;
|
return none;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user