Move login screen to new forms framework
This commit is contained in:
@@ -29,6 +29,9 @@ type Form struct {
|
||||
|
||||
// Used for letting the browser know which user is logged in
|
||||
Username string
|
||||
|
||||
// Actions to perform when the form is rendered
|
||||
Extra ExtraActions
|
||||
}
|
||||
|
||||
// Field is a single input field in a form
|
||||
@@ -54,11 +57,22 @@ type Field struct {
|
||||
|
||||
Type FieldType
|
||||
|
||||
// Only used when Type = `captcha`. When using reCaptcha the field name has
|
||||
// to be `recaptcha_response`
|
||||
// Only used when Type == FieldTypeCaptcha
|
||||
CaptchaSiteKey string
|
||||
}
|
||||
|
||||
// ExtraActions contains extra actions to performs when rendering the form
|
||||
type ExtraActions struct {
|
||||
// Redirects the browser to a different URL with a HTTP 303: See Other
|
||||
// status. This is useful for redirecting the user to a different page if
|
||||
// the form submission was successful
|
||||
RedirectTo string
|
||||
|
||||
// A cookie to install in the browser when the form is rendered. Useful for
|
||||
// setting / destroying user sessions or configurations
|
||||
SetCookie *http.Cookie
|
||||
}
|
||||
|
||||
// FieldType defines the type a form field has and how it should be rendered
|
||||
type FieldType string
|
||||
|
||||
@@ -82,15 +96,17 @@ func (f *Form) ReadInput(r *http.Request) (success bool) {
|
||||
}
|
||||
f.Submitted = true
|
||||
|
||||
var val string
|
||||
|
||||
for i, field := range f.Fields {
|
||||
val = r.FormValue(field.Name)
|
||||
field.EnteredValue = val
|
||||
field.EnteredValue = r.FormValue(field.Name)
|
||||
|
||||
if field.DefaultValue == "" {
|
||||
field.DefaultValue = val
|
||||
field.DefaultValue = field.EnteredValue
|
||||
}
|
||||
|
||||
if field.Type == FieldTypeCaptcha && field.EnteredValue == "" {
|
||||
field.EnteredValue = r.FormValue("g-recaptcha-response")
|
||||
}
|
||||
|
||||
f.Fields[i] = field // Update the new values in the array
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@ package webcontroller
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/pixelapi"
|
||||
"fornaxian.com/pixeldrain-web/webcontroller/forms"
|
||||
@@ -99,7 +100,7 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f form
|
||||
"website with fake accounts",
|
||||
Separator: true,
|
||||
Type: forms.FieldTypeCaptcha,
|
||||
CaptchaSiteKey: wc.captchaSiteKey,
|
||||
CaptchaSiteKey: wc.captchaKey(),
|
||||
},
|
||||
},
|
||||
BackLink: "/",
|
||||
@@ -114,7 +115,7 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f form
|
||||
"password in both password fields"}
|
||||
return f
|
||||
}
|
||||
|
||||
log.Debug("capt: %s", f.FieldVal("recaptcha_response"))
|
||||
resp, err := td.PixelAPI.UserRegister(
|
||||
f.FieldVal("username"),
|
||||
f.FieldVal("e-mail"),
|
||||
@@ -145,6 +146,57 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f form
|
||||
return f
|
||||
}
|
||||
|
||||
func (wc *WebController) loginForm(td *TemplateData, r *http.Request) (f forms.Form) {
|
||||
td.Title = "Login"
|
||||
f = forms.Form{
|
||||
Name: "login",
|
||||
Title: "Log in to your pixeldrain account",
|
||||
Fields: []forms.Field{
|
||||
{
|
||||
Name: "username",
|
||||
Label: "Username / e-mail",
|
||||
Type: forms.FieldTypeUsername,
|
||||
}, {
|
||||
Name: "password",
|
||||
Label: "Password",
|
||||
Type: forms.FieldTypeCurrentPassword,
|
||||
},
|
||||
},
|
||||
BackLink: "/",
|
||||
SubmitLabel: "Login",
|
||||
PostFormHTML: template.HTML(
|
||||
`<br/>If you don't have a pixeldrain account yet, you can ` +
|
||||
`<a href="/register">register here</a>. No e-mail address is ` +
|
||||
`required.<br/>`,
|
||||
),
|
||||
}
|
||||
|
||||
if f.ReadInput(r) {
|
||||
loginResp, err := td.PixelAPI.UserLogin(f.FieldVal("username"), f.FieldVal("password"), false)
|
||||
if err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok {
|
||||
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
|
||||
} else {
|
||||
log.Error("%s", err)
|
||||
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
||||
}
|
||||
} else {
|
||||
log.Debug("key %s", loginResp.APIKey)
|
||||
// Request was a success
|
||||
f.SubmitSuccess = true
|
||||
f.SubmitMessages = []template.HTML{"Success!"}
|
||||
f.Extra.SetCookie = &http.Cookie{
|
||||
Name: "pd_auth_key",
|
||||
Value: loginResp.APIKey,
|
||||
Path: "/",
|
||||
Expires: time.Now().AddDate(50, 0, 0),
|
||||
}
|
||||
f.Extra.RedirectTo = "/user"
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (wc *WebController) passwordForm(td *TemplateData, r *http.Request) (f forms.Form) {
|
||||
td.Title = "Change Password"
|
||||
f = forms.Form{
|
||||
|
@@ -64,7 +64,9 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
|
||||
r.GET(p+"/register_old" /* */, wc.serveRegister)
|
||||
r.GET(p+"/register" /* */, wc.serveForm(wc.registerForm, false))
|
||||
r.POST(p+"/register" /* */, wc.serveForm(wc.registerForm, false))
|
||||
r.GET(p+"/login" /* */, wc.serveTemplate("login", false))
|
||||
r.GET(p+"/login" /* */, wc.serveForm(wc.loginForm, false))
|
||||
r.POST(p+"/login" /* */, wc.serveForm(wc.loginForm, false))
|
||||
// r.GET(p+"/login" /* */, wc.serveTemplate("login", false))
|
||||
r.GET(p+"/logout" /* */, wc.serveTemplate("logout", true))
|
||||
r.POST(p+"/logout" /* */, wc.serveLogout)
|
||||
r.GET(p+"/user" /* */, wc.serveTemplate("user_home", true))
|
||||
@@ -133,8 +135,18 @@ func (wc *WebController) serveForm(
|
||||
|
||||
td.Form.Username = td.Username
|
||||
|
||||
// Execute the extra actions if any
|
||||
if td.Form.Extra.SetCookie != nil {
|
||||
http.SetCookie(w, td.Form.Extra.SetCookie)
|
||||
}
|
||||
if td.Form.Extra.RedirectTo != "" {
|
||||
http.Redirect(w, r, td.Form.Extra.RedirectTo, http.StatusSeeOther)
|
||||
log.Debug("redirect: %s", td.Form.Extra.RedirectTo)
|
||||
return // Don't need to render a form if the user is redirected
|
||||
}
|
||||
|
||||
// Remove the recaptcha field if captcha is disabled
|
||||
if wc.captchaSiteKey == "none" {
|
||||
if wc.captchaKey() == "none" {
|
||||
for i, field := range td.Form.Fields {
|
||||
if field.Type == forms.FieldTypeCaptcha {
|
||||
td.Form.Fields = append(
|
||||
|
Reference in New Issue
Block a user