| @@ -49,6 +49,9 @@ func (s *Server) requireUser(h func(http.ResponseWriter, *http.Request, store.Us |
| 49 | return func(w http.ResponseWriter, r *http.Request) { |
49 | return func(w http.ResponseWriter, r *http.Request) { |
| 50 | u := s.viewer(r) |
50 | u := s.viewer(r) |
| 51 | if u.ID == 0 { |
51 | if u.ID == 0 { |
| |
52 | if r.Method == http.MethodGet { |
| |
53 | s.setNext(w, r.URL.RequestURI()) |
| |
54 | } |
| 52 | http.Redirect(w, r, "/login", http.StatusSeeOther) |
55 | http.Redirect(w, r, "/login", http.StatusSeeOther) |
| 53 | return |
56 | return |
| 54 | } |
57 | } |
| @@ -76,15 +79,16 @@ func (s *Server) checkOrigin(h http.HandlerFunc) http.HandlerFunc { |
| 76 | // the page can tell a brand-new visitor how to get an account. EmailLogin |
79 | // the page can tell a brand-new visitor how to get an account. EmailLogin |
| 77 | // says whether this instance can mail a link; Sent switches the page to the |
80 | // says whether this instance can mail a link; Sent switches the page to the |
| 78 | // confirmation that follows a request. |
81 | // confirmation that follows a request. |
| 79 | func (s *Server) renderLogin(w http.ResponseWriter, errMsg string, sent bool) { |
82 | func (s *Server) renderLogin(w http.ResponseWriter, errMsg string, sent bool, next string) { |
| 80 | s.render(w, "login.html", struct { |
83 | s.render(w, "login.html", struct { |
| 81 | basePage |
84 | basePage |
| 82 | Mode string // closed | invite | open |
85 | Mode string // closed | invite | open |
| 83 | Error string |
86 | Error string |
| 84 | EmailLogin bool |
87 | EmailLogin bool |
| 85 | Sent bool |
88 | Sent bool |
| |
89 | Next string |
| 86 | }{basePage{Site: s.siteName(), Host: s.cfg.SiteHost()}, |
90 | }{basePage{Site: s.siteName(), Host: s.cfg.SiteHost()}, |
| 87 | s.cfg.Registration.Mode, errMsg, s.emailLoginEnabled(), sent}) |
91 | s.cfg.Registration.Mode, errMsg, s.emailLoginEnabled(), sent, next}) |
| 88 | } |
92 | } |
| 89 | |
93 | |
| 90 | // emailLoginEnabled reports whether a link can be mailed at all. There is no |
94 | // emailLoginEnabled reports whether a link can be mailed at all. There is no |
| @@ -111,18 +115,18 @@ func (s *Server) loginSubmit(w http.ResponseWriter, r *http.Request) { |
| 111 | if err := control.RequestLoginLink(s.cfg, s.st, r.FormValue("identifier")); err != nil { |
115 | if err := control.RequestLoginLink(s.cfg, s.st, r.FormValue("identifier")); err != nil { |
| 112 | log.Printf("login link: %v", err) |
116 | log.Printf("login link: %v", err) |
| 113 | } |
117 | } |
| 114 | s.renderLogin(w, "", true) |
118 | s.renderLogin(w, "", true, "") |
| 115 | } |
119 | } |
| 116 | |
120 | |
| 117 | func (s *Server) login(w http.ResponseWriter, r *http.Request) { |
121 | func (s *Server) login(w http.ResponseWriter, r *http.Request) { |
| 118 | token := r.URL.Query().Get("token") |
122 | token := r.URL.Query().Get("token") |
| 119 | if token == "" { |
123 | if token == "" { |
| 120 | s.renderLogin(w, "", false) |
124 | s.renderLogin(w, "", false, s.peekNext(r)) |
| 121 | return |
125 | return |
| 122 | } |
126 | } |
| 123 | userID, err := s.st.ConsumeLoginToken(store.HashToken(token)) |
127 | userID, err := s.st.ConsumeLoginToken(store.HashToken(token)) |
| 124 | if err != nil { |
128 | if err != nil { |
| 125 | s.renderLogin(w, badLoginToken, false) |
129 | s.renderLogin(w, badLoginToken, false, "") |
| 126 | return |
130 | return |
| 127 | } |
131 | } |
| 128 | // A token minted before the account was suspended is still consumable, |
132 | // A token minted before the account was suspended is still consumable, |
| @@ -130,7 +134,7 @@ func (s *Server) login(w http.ResponseWriter, r *http.Request) { |
| 130 | // read. Checking here covers every mint path. The message is the one a |
134 | // read. Checking here covers every mint path. The message is the one a |
| 131 | // bad token gets: a distinct one would confirm the account exists. |
135 | // bad token gets: a distinct one would confirm the account exists. |
| 132 | if u, err := s.st.UserByID(userID); err != nil || u.Disabled { |
136 | if u, err := s.st.UserByID(userID); err != nil || u.Disabled { |
| 133 | s.renderLogin(w, badLoginToken, false) |
137 | s.renderLogin(w, badLoginToken, false, "") |
| 134 | return |
138 | return |
| 135 | } |
139 | } |
| 136 | sessTok, sessHash, err := store.NewToken() |
140 | sessTok, sessHash, err := store.NewToken() |
| @@ -143,7 +147,11 @@ func (s *Server) login(w http.ResponseWriter, r *http.Request) { |
| 143 | return |
147 | return |
| 144 | } |
148 | } |
| 145 | http.SetCookie(w, s.sessionCookieFor(sessTok)) |
149 | http.SetCookie(w, s.sessionCookieFor(sessTok)) |
| 146 | http.Redirect(w, r, "/", http.StatusSeeOther) |
150 | dest := s.takeNext(w, r) |
| |
151 | if dest == "" { |
| |
152 | dest = "/" |
| |
153 | } |
| |
154 | http.Redirect(w, r, dest, http.StatusSeeOther) |
| 147 | } |
155 | } |
| 148 | |
156 | |
| 149 | // sessionCookieFor is the cookie a new session ships in. Secure follows TLS |
157 | // sessionCookieFor is the cookie a new session ships in. Secure follows TLS |