aboutsummaryrefslogtreecommitdiffhomepage
path: root/site/login.lisp
blob: ef4ba264a614448d6fc33542c4c404fa1241d0cf (plain)
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
66
67
68
69
70
71
72
73
(in-package #:vampire)

(wknd:defendpoint login.html
  :get :route "login"
  :returns "text/html"
  :handle (login-page))

(wknd:defendpoint login-user
  :post :route "login"
  :parameters
  (name string)
  (password string)
  :properties
  (user user)
  :authenticate (authenticate-login-user name password)
  :handle (wknd:endpoint-redirect 'home.html))

(defun authenticate-login-user (name password)
  (do>
    found-user :when= (user-with-name name)
    :when (equal (user-pwhash found-user)
		 (hash-string password (user-pwsalt found-user)))
    session := (db:with-transaction () (make-instance 'session :user found-user))
    (wknd:set-cookie +session-cookie+ :value (key session))))

(wknd:defendpoint new-account.html
  :get :route "new-account"
  :returns "text/html"
  :handle (new-account-page))

(wknd:defendpoint create-new-account
  :post :to "new-account"
  :parameters
  (username string)
  (password string)
  (password2 string)
  (invite-code string)
  :properties
  (invite invite)
  :authenticate (and
		 (equal password password2)
		 (setf invite (object-with-key invite-code)))
  :authorize (invite-validp invite)
  :handle (progn
	    (db:with-transaction ()
	      (when (uses-remaining invite)
		(decf (uses-remaining invite))
		(when (zerop (uses-remaining invite))
		  (db:delete-object invite)))
	      (let ((user (make-instance 'user :name username)))
		(setf (user-pwhash user) (hash-string password (user-pwsalt user)))))
	    (wknd:endpoint-redirect 'login.html)))

(defun login-page ()
  (page (:title "V A M P I R E ~ LOGIN")
    (:div (:h1 "I vant to suck your blood")
          (:form :method "POST" :action "/login"
                 (:input :placeholder "Name" :name "name")
                 (:br)
                 (:input :placeholder "Password" :type "password" :name "password")
                 (:br)
                 (:button :type "submit" "Click to Login")))))

(defun new-account-page ()
  (page (:title "V A M P I R E ~ JOIN")
    (:form :method "POST" :action "/new-account"
           (:input :placeholder "Invite Code" :name "invite-code")(:br)
           (:input :placeholder "Username" :name "username")(:br)
           (:input :placeholder "Password" :name "password" :type "password")(:br)
           (:input :placeholder "Repeat Password" :name "password2" :type "password")(:br)
           (:button :type "submit" "Become Undead"))))