-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.sl
38 lines (34 loc) · 1.17 KB
/
register.sl
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
<%
require("includes/init.sl");
Response.redirect("index.sl") if @current_user;
if Request.method == "POST" {
errors = [];
unless %r{^[a-z0-9]{3,30}$}.match(Request["username"]) {
errors.push("username must be alphanumeric and between 3 and 30 characters");
}
unless Request["password"] == Request["confirm"] {
errors.push("password confirmation must match password");
}
unless Request["password"].length >= 6 {
errors.push("password must be at least 6 characters");
}
if DB.query_single("SELECT * FROM users WHERE username = '" + DB.escape(Request["username"]) + "'") {
errors.push("username is already taken");
}
if errors.any {
@error = errors.join(", ");
} else {
salt = (rand() * 100000000).to_s;
DB.insert("users", {
"username" => Request["username"],
"password" => hash_password(Request["password"], salt),
"salt" => salt
});
@session["user_id"] = DB.insert_id;
@session.save;
Response.redirect("index.sl");
}
}
require("templates/header.sl");
require("templates/register.sl");
require("templates/footer.sl");