Basic authentication

This post was flagged by the community and is temporarily hidden.

Hi,

I think your problem is that the middleware is associated with the group, but your route isn’t. Try replacing

e.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, “Authenticated”)
})

with

g.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, “Authenticated”)
})```

Thanks for advice. After update, I get prompt for basic auth, but then upon entering user / password get

message “Not Found”

updated code below

package main

import (
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"net/http"
)

func main() {
	// Echo instance
	e := echo.New()

	g := e.Group("/admin")
	g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
		if username == "joe" && password == "secret" {
			return true, nil
		}
		return false, nil
	}))
	// Route => handler
	e.GET("/", func(c echo.Context) error {
		return c.HTML(http.StatusOK, "root")
	})
	g.GET("/admin", func(c echo.Context) error {
		return c.HTML(http.StatusOK, "admin")
	})

	// Start server
	e.Logger.Fatal(e.Start(":1323"))
}

Hi,
You haven’t specified what URL you are trying to get to, but what I expect is happening is that you are trying to go to /admin, when what it looks like you’ve created is a route to /admin/admin. That would explain why you’re authentication is processing but you still get a 404 response

Thanks for the help @kyle.cacciatore . Based on your advise got it to work. Basically whatever is used for

g := e.Group("/<url>")

Becomes the starting point for all additional to the get function for that group.