-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.lua
More file actions
65 lines (50 loc) · 1.49 KB
/
app.lua
File metadata and controls
65 lines (50 loc) · 1.49 KB
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
local lapis = require("lapis")
local respond_to = require("lapis.application").respond_to
local db = require("lapis.db")
local app = lapis.Application()
app:enable("etlua")
app.layout = require("views.layout")
app:get("home" ,"/", function(self)
if not self.session.user then
return {redirect_to = self:url_for("login")}
end
print(self.session.user)
self.users = db.query("select * from users")
return { render = "home" }
end)
app:get("logout", "/logout", function(self)
self.session.user = nil
return {redirect_to = self:url_for("login")}
end)
app:match("login", "/login", respond_to({
GET = function(self)
return { render = "login" , layout = false}
end,
POST = function(self)
local user = db.query("select * from users where email = ? and password = ?", self.params.email, self.params.password)
if user[1] then
self.session.user = user
return {redirect_to = self:url_for("home")}
end
self.error = "Invalid email or password"
return {layout = false, render = "login"}
end
}))
app:match("register", "/register", respond_to({
GET = function(self)
return { render = "register" , layout = false}
end,
POST = function(self)
local res = db.insert("users", {
username = self.params.username,
email = self.params.email,
password = self.params.password
})
if res then
return {redirect_to = self:url_for("login")}
else
return {layout = false, render = "register"}
end
end
}))
return app