let users register

This commit is contained in:
Sam Dodrill 2015-01-05 14:22:26 -08:00
parent 6515955a14
commit 325feb0132
4 changed files with 83 additions and 10 deletions

View File

@ -1,28 +1,58 @@
lapis = require "lapis" lapis = require "lapis"
db = require "lapis.db" db = require "lapis.db"
csrf = require "lapis.csrf" csrf = require "lapis.csrf"
encoding = require "lapis.util.encoding"
import capture_errors from require "lapis.application"
import assert_valid from require "lapis.validate"
import respond_to from require "lapis.application" import respond_to from require "lapis.application"
import Model from require "lapis.db.model" import Model from require "lapis.db.model"
class User extends Model class Users extends Model
@primary_key: "login"
class App extends lapis.Application class App extends lapis.Application
"/": => "/": =>
render: "index" render: "index"
[form: "/form"]: respond_to { [register: "/register"]: capture_errors respond_to {
GET: => GET: =>
assert @ @csrf_token = csrf.generate_token @
csrf_token = csrf.generate_token @ render: true
@html =>
form method: "POST", action: "/form", ->
input type: "hidden", name: "csrf_token", value: csrf_token
input type: "submit"
POST: => POST: =>
csrf.assert_token @ csrf.assert_token @
"The form is valid!" assert_valid @params, {
{ "email", exists: true, min_length: 3 }
{ "password", exists: true, min_length: 3 }
{ "password_again", equals: @params.password }
{ "name", exists: true, min_length: 3}
}
@params.password = encoding.encode_base64 encoding.hmac_sha1("ninjas", @params.password)
@params.password_again = nil
@params.csrf_token = nil
@params.extension = "9001"
if Users\find email: @params.email
return "User with that email already exists"
user = Users\create @params
"Hi " .. user.name
}
[login: "/login"]: capture_errors respond_to {
GET: =>
@csrf_token = csrf.generate_token @
render: true
POST: =>
csrf.assert_token @
assert_valid @params, {
{ "email", exists: true, min_length: 3 }
{ "password", exists: true, min_length: 3 }
}
user = Users\find email: @params.email
} }

View File

@ -13,4 +13,7 @@ import add_column, create_table, types from require "lapis.db.schema"
[2]: => [2]: =>
add_column "users", "admin", types.boolean add_column "users", "admin", types.boolean
[3]: =>
add_column "users", "extension", types.text
} }

14
views/login.moon Normal file
View File

@ -0,0 +1,14 @@
import Widget from require "lapis.html"
class LoginForm extends Widget
content: =>
form method: "POST", action: "/login", ->
p ->
text "email address"
input type: "hidden", name: "csrf_token", value: @csrf_token
input type: "text", name: "email"
p ->
text "password"
input type: "password", name: "password"
br!
input type: "submit"

26
views/register.moon Normal file
View File

@ -0,0 +1,26 @@
import Widget from require "lapis.html"
class LoginForm extends Widget
content: =>
form method: "POST", action: "/register", ->
input type: "hidden", name: "csrf_token", value: @csrf_token
p ->
text "email address"
input type: "text", name: "email"
p ->
text "password"
input type: "password", name: "password"
p ->
text "password"
input type: "password", name: "password_again"
p ->
text "name"
input type: "text", name: "name"
br!
input type: "submit"