Skip to content
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

fixing could not find desired block (fixes #90) #91

Merged
merged 4 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions SHOULDERS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# buffao-auth Stands on the Shoulders of Giants
# buffalo-auth Stands on the Shoulders of Giants

buffao-auth does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants, this project would not be possible. Please make sure to check them out and thank them for all of their hard work.
buffalo-auth does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants, this project would not be possible. Please make sure to check them out and thank them for all of their hard work.

Thank you to the following **GIANTS**:

Expand Down Expand Up @@ -53,6 +53,5 @@ Thank you to the following **GIANTS**:
* [golang.org/x/tools](https://godoc.org/golang.org/x/tools)
* [golang.org/x/xerrors](https://godoc.org/golang.org/x/xerrors)
* [gopkg.in/check.v1](https://godoc.org/gopkg.in/check.v1)
* [gopkg.in/errgo.v2](https://godoc.org/gopkg.in/errgo.v2)
* [gopkg.in/yaml.v2](https://godoc.org/gopkg.in/yaml.v2)
* [gopkg.in/yaml.v3](https://godoc.org/gopkg.in/yaml.v3)
11 changes: 8 additions & 3 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"context"
"os/exec"

"github.com/gobuffalo/buffalo-auth/genny/auth"
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/genny/v2/gogen"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -23,13 +23,18 @@ var authCmd = &cobra.Command{
}

if err := r.WithNew(auth.New(args)); err != nil {
return errors.WithStack(err)
return err
}

if err := r.WithNew(gogen.Fmt(r.Root)); err != nil {
return errors.WithStack(err)
return err
}

gomodtidy := exec.Command("go", "mod", "tidy")
g := genny.New()
g.Command(gomodtidy)
r.With(g)

return r.Run()
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var Version = "local development version"
var Version = "v1.5.0"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Expand Down
42 changes: 29 additions & 13 deletions genny/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/gobuffalo/genny/v2/plushgen"
"github.com/gobuffalo/meta"
"github.com/gobuffalo/plush/v4"
"github.com/pkg/errors"
)

//go:embed templates
Expand Down Expand Up @@ -52,15 +51,15 @@ func New(args []string) (*genny.Generator, error) {
var err error
fields, err = attrs.ParseArgs(extraAttrs(args)...)
if err != nil {
return g, errors.WithStack(err)
return g, fmt.Errorf("could not parse arguments: %w", err)
}

sub, err := fs.Sub(templates, "templates")
if err != nil {
return g, errors.WithStack(err)
return g, fmt.Errorf("failed to get subtree of templates: %w", err)
}
if err := g.FS(sub); err != nil {
return g, errors.WithStack(err)
return g, fmt.Errorf("failed to add subtree: %w", err)
}

ctx := plush.NewContext()
Expand All @@ -73,16 +72,17 @@ func New(args []string) (*genny.Generator, error) {
g.Transformer(genny.NewTransformer(".fizz", migrationsTransformer(time.Now())))

g.RunFn(func(r *genny.Runner) error {

path := filepath.Join("actions", "app.go")
gf, err := r.FindFile(path)

f, err := r.FindFile(path)
if err != nil {
return err
return fmt.Errorf("setup auth: %w", err)
}

gf, err = gogen.AddInsideBlock(
gf,
`if app == nil {`,
expressions := []string{
``,
`// NOTE: this block should go before any resources`,
`// that need to be protected by buffalo-goth!`,
`//AuthMiddlewares`,
`app.Use(SetCurrentUser)`,
`app.Use(Authorize)`,
Expand All @@ -101,12 +101,28 @@ func New(args []string) (*genny.Generator, error) {
`users.POST("/", UsersCreate)`,
`users.Middleware.Remove(Authorize)`,
``,
)
}

f, err = gogen.AddInsideBlock(f, "appOnce.Do(func() {", expressions...)
if err != nil {
return errors.WithStack(err)
if strings.Contains(err.Error(), "could not find desired block") {
// TODO: remove this block some day soon
// add this block for compatibility with the apps built with
// the old version of Buffalo CLI (v0.18.8 or older)
f, err = gogen.AddInsideBlock(f, "if app == nil {", expressions...)
if err != nil {
if err != nil {
return fmt.Errorf("could not add a code block: %w", err)
} else {
r.Logger.Warnf("This app was built with CLI v0.18.8 or older. See https://gobuffalo.io/documentation/known-issues/#cli-v0.18.8")
}
}
} else {
return fmt.Errorf("could not add a code block: %w", err)
}
}

return r.File(gf)
return r.File(f)
})

return g, nil
Expand Down
11 changes: 6 additions & 5 deletions genny/auth/templates/actions/auth.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"database/sql"
"net/http"
"strings"

"github.com/gobuffalo/buffalo"
Expand All @@ -15,13 +16,13 @@ import (

// AuthLanding shows a landing page to login
func AuthLanding(c buffalo.Context) error {
return c.Render(200, r.HTML("auth/landing.plush.html"))
return c.Render(http.StatusOK, r.HTML("auth/landing.plush.html"))
}

// AuthNew loads the signin page
func AuthNew(c buffalo.Context) error {
c.Set("user", models.User{})
return c.Render(200, r.HTML("auth/new.plush.html"))
return c.Render(http.StatusOK, r.HTML("auth/new.plush.html"))
}

// AuthCreate attempts to log the user in with an existing account.
Expand All @@ -48,7 +49,7 @@ func AuthCreate(c buffalo.Context) error {
}

if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
// couldn't find an user with the supplied email address.
return bad()
}
Expand All @@ -68,12 +69,12 @@ func AuthCreate(c buffalo.Context) error {
redirectURL = redir
}

return c.Redirect(302, redirectURL)
return c.Redirect(http.StatusFound, redirectURL)
}

// AuthDestroy clears the session and logs a user out
func AuthDestroy(c buffalo.Context) error {
c.Session().Clear()
c.Flash().Add("success", "You have been logged out!")
return c.Redirect(302, "/")
return c.Redirect(http.StatusFound, "/")
}
9 changes: 5 additions & 4 deletions genny/auth/templates/actions/auth_test.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ func (as *ActionSuite) createUser() (*models.User, error) {
}

verrs, err := u.Create(as.DB)
as.False(verrs.HasAny())
as.NoError(err)
as.False(verrs.HasAny(), "validation error: %v", verrs)

return u, err
}

func (as *ActionSuite) Test_Auth_Signin() {
res := as.HTML("/auth/").Get()
as.Equal(200, res.Code)
as.Equal(http.StatusOK, res.Code)
as.Contains(res.Body.String(), `<a href="/auth/new/">Sign In</a>`)
}

func (as *ActionSuite) Test_Auth_New() {
res := as.HTML("/auth/new").Get()
as.Equal(200, res.Code)
as.Equal(http.StatusOK, res.Code)
as.Contains(res.Body.String(), "Sign In")
}

Expand Down Expand Up @@ -82,7 +83,7 @@ func (as *ActionSuite) Test_Auth_Redirect() {

res := as.HTML("/auth").Post(u)

as.Equal(302, res.Code)
as.Equal(http.StatusFound, res.Code)
as.Equal(res.Location(), tcase.resultLocation)
})
}
Expand Down
12 changes: 8 additions & 4 deletions genny/auth/templates/actions/home_test.go.plush
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package actions

import "<%= app.PackagePkg %>/models"
import (
"net/http"

"<%= app.PackagePkg %>/models"
)

func (as *ActionSuite) Test_HomeHandler() {
res := as.HTML("/").Get()
as.Equal(302, res.Code)
as.Equal(http.StatusFound, res.Code)
as.Equal(res.Location(), "/auth/new")
}

Expand All @@ -20,11 +24,11 @@ func (as *ActionSuite) Test_HomeHandler_LoggedIn() {
as.Session.Set("current_user_id", u.ID)

res := as.HTML("/auth").Get()
as.Equal(200, res.Code)
as.Equal(http.StatusOK, res.Code)
as.Contains(res.Body.String(), "Sign Out")

as.Session.Clear()
res = as.HTML("/auth").Get()
as.Equal(200, res.Code)
as.Equal(http.StatusOK, res.Code)
as.Contains(res.Body.String(), "Sign In")
}
12 changes: 7 additions & 5 deletions genny/auth/templates/actions/users.go.plush
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions

import (
"net/http"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop/v6"
"github.com/pkg/errors"
Expand All @@ -12,7 +14,7 @@ import (
func UsersNew(c buffalo.Context) error {
u := models.User{}
c.Set("user", u)
return c.Render(200, r.HTML("users/new.plush.html"))
return c.Render(http.StatusOK, r.HTML("users/new.plush.html"))
}

// UsersCreate registers a new user with the application.
Expand All @@ -31,13 +33,13 @@ func UsersCreate(c buffalo.Context) error {
if verrs.HasAny() {
c.Set("user", u)
c.Set("errors", verrs)
return c.Render(200, r.HTML("users/new.plush.html"))
return c.Render(http.StatusOK, r.HTML("users/new.plush.html"))
}

c.Session().Set("current_user_id", u.ID)
c.Flash().Add("success", "Welcome to Buffalo!")
c.Flash().Add("success", "Welcome to <%= app.Name %>!")

return c.Redirect(302, "/")
return c.Redirect(http.StatusFound, "/")
}

// SetCurrentUser attempts to find a user based on the current_user_id
Expand Down Expand Up @@ -69,7 +71,7 @@ func Authorize(next buffalo.Handler) buffalo.Handler {
}

c.Flash().Add("danger", "You must be authorized to see that page")
return c.Redirect(302, "/auth/new")
return c.Redirect(http.StatusFound, "/auth/new")
}
return next(c)
}
Expand Down
8 changes: 5 additions & 3 deletions genny/auth/templates/actions/users_test.go.plush
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package actions

import (
"<%= app.PackagePkg %>/models"
"net/http"

"<%= app.PackagePkg %>/models"
)

func (as *ActionSuite) Test_Users_New() {
res := as.HTML("/users/new").Get()
as.Equal(200, res.Code)
as.Equal(http.StatusOK, res.Code)
}

func (as *ActionSuite) Test_Users_Create() {
Expand All @@ -21,7 +23,7 @@ func (as *ActionSuite) Test_Users_Create() {
}

res := as.HTML("/users").Post(u)
as.Equal(302, res.Code)
as.Equal(http.StatusFound, res.Code)

count, err = as.DB.Count("users")
as.NoError(err)
Expand Down
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module github.com/gobuffalo/buffalo-auth
go 1.16

require (
github.com/gobuffalo/attrs v1.0.2
github.com/gobuffalo/genny/v2 v2.0.12
github.com/gobuffalo/meta v0.3.2
github.com/gobuffalo/plush/v4 v4.1.13
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.5.0
github.com/gobuffalo/attrs v1.0.3
github.com/gobuffalo/genny/v2 v2.1.0
github.com/gobuffalo/meta v0.3.3
github.com/gobuffalo/plush/v4 v4.1.18
github.com/spf13/cobra v1.6.1
)
Loading