use new auth.SetErrorHandler option
This commit is contained in:
parent
f71020d5d9
commit
31cbf00c93
6
go.sum
6
go.sum
|
@ -162,6 +162,10 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
|
|||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
|
||||
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
|
||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||
github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
|
||||
|
@ -442,6 +446,8 @@ go.cryptoscope.co/secretstream v1.2.2/go.mod h1:7nRGZ7fTqSgQAnv2Y4m8xQsS3MFxvB7I
|
|||
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
|
||||
go.mindeco.de v1.7.0 h1:EWrMf1guWXB5P4IzMNJvPbO9Ud9h4G/NMzsl5v1smPY=
|
||||
go.mindeco.de v1.7.0/go.mod h1:ePOcyktbpqzhMPRBDv2gUaDd3h8QtT+DUU1DK+VbQZE=
|
||||
go.mindeco.de/ssb-refs v0.1.1-0.20210108133850-cf1f44fea870 h1:TCI3AefMAaOYECvppn30+CfEB0Fn8IES1SKvvacc3/c=
|
||||
go.mindeco.de/ssb-refs v0.1.1-0.20210108133850-cf1f44fea870/go.mod h1:OnBnV02ux4lLsZ39LID6yYLqSDp+dqTHb/3miYPkQFs=
|
||||
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
||||
|
|
|
@ -56,9 +56,7 @@ func New(
|
|||
// TODO: add plural and template data variants
|
||||
// TODO: move these to the i18n helper pkg
|
||||
render.InjectTemplateFunc("i18n", func(r *http.Request) interface{} {
|
||||
lang := r.FormValue("lang")
|
||||
accept := r.Header.Get("Accept-Language")
|
||||
loc := locHelper.NewLocalizer(lang, accept)
|
||||
loc := localizerFromRequest(locHelper, r)
|
||||
return loc.LocalizeSimple
|
||||
}),
|
||||
render.InjectTemplateFunc("is_logged_in", func(r *http.Request) interface{} {
|
||||
|
@ -102,7 +100,24 @@ func New(
|
|||
},
|
||||
}
|
||||
|
||||
// TODO: use r.Error?
|
||||
authErrH := func(rw http.ResponseWriter, req *http.Request, err error, code int) {
|
||||
msg := err.Error()
|
||||
|
||||
// localize some specific error messages
|
||||
if err == auth.ErrBadLogin {
|
||||
msg = localizerFromRequest(locHelper, req).LocalizeSimple("AuthErrorBadLogin")
|
||||
}
|
||||
|
||||
r.HTML("/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
return errorTemplateData{
|
||||
Err: msg,
|
||||
// TODO: localize?
|
||||
Status: http.StatusText(code),
|
||||
StatusCode: code,
|
||||
}, nil
|
||||
}).ServeHTTP(rw, req)
|
||||
}
|
||||
|
||||
notAuthorizedH := r.HTML("/error.tmpl", func(rw http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
statusCode := http.StatusUnauthorized
|
||||
rw.WriteHeader(statusCode)
|
||||
|
@ -115,6 +130,7 @@ func New(
|
|||
|
||||
a, err = auth.NewHandler(fs,
|
||||
auth.SetStore(store),
|
||||
auth.SetErrorHandler(authErrH),
|
||||
auth.SetNotAuthorizedHandler(notAuthorizedH),
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -168,3 +184,9 @@ func concatTemplates(lst ...[]string) []string {
|
|||
}
|
||||
return catted
|
||||
}
|
||||
|
||||
func localizerFromRequest(helper *i18n.Helper, r *http.Request) *i18n.Localizer {
|
||||
lang := r.FormValue("lang")
|
||||
accept := r.Header.Get("Accept-Language")
|
||||
return helper.NewLocalizer(lang, accept)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue