59 lines
1.5 KiB
Plaintext
59 lines
1.5 KiB
Plaintext
lapis = require "lapis"
|
|
db = require "lapis.db"
|
|
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 Model from require "lapis.db.model"
|
|
|
|
class Users extends Model
|
|
|
|
class App extends lapis.Application
|
|
"/": =>
|
|
render: "index"
|
|
|
|
[register: "/register"]: 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 }
|
|
{ "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
|
|
}
|