-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content-Type application/json error: "failed to redirect user during authboss.Middleware redirect: template for page redirect not found" #29
Comments
Changing the sample like so: diff --git a/blog.go b/blog.go
index bae3bda..b125d93 100644
--- a/blog.go
+++ b/blog.go
@@ -256,7 +256,7 @@ func main() {
// Authed routes
mux.Group(func(mux chi.Router) {
- mux.Use(authboss.Middleware(ab, true, false, false), lock.Middleware(ab), confirm.Middleware(ab))
+ mux.Use(authboss.Middleware2(ab, authboss.RequireNone, authboss.RespondUnauthorized), lock.Middleware(ab), confirm.Middleware(ab))
mux.MethodFunc("GET", "/blogs/new", newblog)
mux.MethodFunc("GET", "/blogs/{id}/edit", edit)
mux.MethodFunc("POST", "/blogs/{id}/edit", update) With the following request: $ curl -v -X GET -d '{}' http://localhost:3000/blogs/new Gives the following response (and no error):
Do not use |
Now I think I understand. But a question: API endpoints (with
Can the following code be fine, in your opinion? r.Group(func(r chi.Router) {
r.Use(authboss.Middleware2(config.Ab, authboss.RequireNone, authboss.RespondRedirect))
r.Get("/routeRenderedByServerNotAPI", myServerRouteHandler)
})
r.Group(func(r chi.Router) {
r.Use(authboss.Middleware2(config.Ab, authboss.RequireNone, authboss.RespondUnauthorized))
r.Get("/routeForAPIContentTypeJSON", myAPIRouteHandler)
}) What if someone calls |
For Authboss to have API endpoints it needs to be in API mode. They can do redirects, but they do them differently. But when it's in API mode it doesn't look for a template, therefore doesn't error when templates are missing. |
Ok, @aarondl, I understand. What I do not understand now is if authboss can be used in two different ways on the same server or not. As you can see from the previous examples, I have both html endpoints (authboss pages also) (ex: I need authboss to protect both and I need different "respond" operations if the json endpoint or html page is called. How do you solve it? Should I have two different In func redirect(w http.ResponseWriter, r *http.Request, path string) {
if *flagAPI {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Location", path)
w.WriteHeader(http.StatusFound)
fmt.Fprintf(w, `{"path": %q}`, path)
return
}
http.Redirect(w, r, path, http.StatusFound)
} Why does authboss not detect automatically if the call has Can we introduce this behavior? |
It cannot be used in two different ways on the same server. It was meant to be one or the other. It's a significant amount of work to change everything to accept both types of requests since there's so much abstraction going on in Authboss. When you first set Authboss up you do a lot of configuration and instantiating types that will fulfill it's interfaces and quite a bit of that is binary - either API or Form based. It'd be a challenge. It's not something I want to do because it doesn't help my use cases and I don't really see it as an important thing, but I wouldn't stop someone from contributing that. |
Ok I understand. Just a final little question: If I use the below code am I totally wrong? ...
r.Use(Ab.LoadClientStateMiddleware, remember.Middleware(Ab))
// HTML endpoints with **authboss.RespondRedirect**?
r.Group(func(r chi.Router) {
r.Use(authboss.Middleware2(Ab, authboss.RequireNone, authboss.RespondRedirect))
....
}
// JSON API endpoints with **authboss.RespondUnauthorized**?
r.Group(func(r chi.Router) {
r.Use(authboss.Middleware2(Ab, authboss.RequireNone, authboss.RespondUnauthorized))
....
}
// authboss endpoints
r.Group(func(r chi.Router) {
r.Use(auth.DataInjector, authboss.ModuleListMiddleware(Ab))
r.Mount(AUTH_URL, http.StripPrefix(AUTH_URL, Ab.Config.Core.Router))
}) |
That could work sure, so long as you're not mixing API and non-API anywhere. :) |
Issue opened for the creation of a wiki page that summarizes the doubts and problems for newbies (volatiletech/authboss#210).
Let's take
authboss-sample
(https://github.com/volatiletech/authboss-sample).If I use it via browser it works. If I visit
localhost:3000/blogs/new
it redirects on/login
page which is what I want.But if I use Postman (or a javascript client) and - example - call the
POST localhost:3000/blogs/new
or maybe theGET localhost:3000/blogs/new
it panics:Why this behaviour?
If I remove the
Content-Type: application/json
from the request it redirects again but is not what I need.I need a 401 http status code so I can handle the error in my javascript client.
I don't need the
API
mode. Just need to use authboss in browser with templates and all, which is amazing (thanks again!).Same problem using
Middleware2
.Is it wrong what I need?
Same on authboss: volatiletech/authboss#208
The text was updated successfully, but these errors were encountered: