Skip to content

Commit

Permalink
Merge pull request #49 from gobuffalo/fixing-could-not-find-desired-b…
Browse files Browse the repository at this point in the history
…lock

fixing could not find desired block (fixes #48)
  • Loading branch information
sio4 authored Feb 3, 2023
2 parents b571c17 + 5adbca7 commit 15d670d
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 99 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Buffalo-goth is a plugin for [buffalo cli](https://github.com/gobuffalo/cli)
that makes it easy to integrate [goth](https://github.com/markbates/goth)
into your Buffalo application.

In Buffalo `v0.9.4` the built in generator for [github.com/markbates/goth](https://github.com/markbates/goth) was removed in favor of this plugin.

## Installation

```console
Expand Down
2 changes: 0 additions & 2 deletions SHOULDERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Thank you to the following **GIANTS**:
* [github.com/kr/text](https://godoc.org/github.com/kr/text)
* [github.com/microcosm-cc/bluemonday](https://godoc.org/github.com/microcosm-cc/bluemonday)
* [github.com/pkg/diff](https://godoc.org/github.com/pkg/diff)
* [github.com/pkg/errors](https://godoc.org/github.com/pkg/errors)
* [github.com/pmezard/go-difflib](https://godoc.org/github.com/pmezard/go-difflib)
* [github.com/rogpeppe/go-internal](https://godoc.org/github.com/rogpeppe/go-internal)
* [github.com/russross/blackfriday/v2](https://godoc.org/github.com/russross/blackfriday/v2)
Expand All @@ -52,6 +51,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)
6 changes: 3 additions & 3 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cmd

import (
"context"
"fmt"
"os/exec"

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

Expand All @@ -33,13 +33,13 @@ var authCmd = &cobra.Command{
opts.Providers = args
gg, err := auth.New(opts)
if err != nil {
return errors.WithStack(err)
return err
}
gg.With(r)

g, err := gogen.Fmt(r.Root)
if err != nil {
return errors.WithStack(err)
return fmt.Errorf("formatting error: %w", err)
}
r.With(g)

Expand Down
6 changes: 3 additions & 3 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cmd

import (
"context"
"fmt"
"os/exec"

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

Expand All @@ -34,13 +34,13 @@ var generateCmd = &cobra.Command{
opts.Providers = args
g, err := goth.New(opts)
if err != nil {
return errors.WithStack(err)
return err
}
r.With(g)

g, err = gogen.Fmt(r.Root)
if err != nil {
return errors.WithStack(err)
return fmt.Errorf("formatting error: %w", err)
}
r.With(g)

Expand Down
4 changes: 2 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "current version of goth",
Short: "current version of buffalo-goth",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("goth", goth.Version)
fmt.Println("buffalo-goth", goth.Version)
return nil
},
}
Expand Down
10 changes: 5 additions & 5 deletions genny/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"bytes"
"embed"
"fmt"
"io/fs"
"os/exec"
"strings"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/genny/v2/gogen"
"github.com/gobuffalo/meta"
"github.com/pkg/errors"

"github.com/gobuffalo/buffalo-goth/genny/goth"
)
Expand All @@ -25,19 +25,19 @@ func New(opts *Options) (*genny.Group, error) {
Providers: opts.Providers,
})
if err != nil {
return gg, errors.WithStack(err)
return gg, err
}
gg.Add(g)

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

g = genny.New()

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

h := template.FuncMap{
Expand All @@ -59,7 +59,7 @@ func New(opts *Options) (*genny.Group, error) {

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

bb := &bytes.Buffer{}
Expand Down
4 changes: 4 additions & 0 deletions genny/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ var app *buffalo.App
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{})
// NOTE: this block should go before any resources
// that need to be protected by buffalo-goth!
auth := app.Group("/auth")
app.Use(SetCurrentUser)
app.Use(Authorize)
Expand All @@ -77,6 +80,7 @@ func App() *buffalo.App {
auth.DELETE("", AuthDestroy)
auth.Middleware.Skip(Authorize, bah, AuthCallback)
auth.GET("/{provider}/callback", AuthCallback)
}
return app
Expand Down
20 changes: 11 additions & 9 deletions genny/auth/templates/actions/auth.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package actions

import (
"fmt"
"net/http"
"os"

"{{.app.ModelsPkg}}"

"github.com/gobuffalo/buffalo"
"{{.app.ModelsPkg}}"
"github.com/markbates/going/defaults"
"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/x/defaults"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
{{range .providers -}}
"github.com/markbates/goth/providers/{{ downcase . }}"
{{end -}}
"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/nulls"
{{end -}}
"github.com/pkg/errors"
)

Expand All @@ -30,7 +32,7 @@ func init() {
func AuthCallback(c buffalo.Context) error {
gu, err := gothic.CompleteUserAuth(c.Response(), c.Request())
if err != nil {
return c.Error(401, err)
return c.Error(http.StatusUnauthorized, err)
}
tx := c.Value("tx").(*pop.Connection)
q := tx.Where("provider = ? and provider_id = ?", gu.Provider, gu.UserID)
Expand Down Expand Up @@ -58,13 +60,13 @@ func AuthCallback(c buffalo.Context) error {
}

c.Flash().Add("success", "You have been logged in")
return c.Redirect(302, "/")
return c.Redirect(http.StatusFound, "/")
}

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, "/")
}

func SetCurrentUser(next buffalo.Handler) buffalo.Handler {
Expand All @@ -85,7 +87,7 @@ func Authorize(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
if uid := c.Session().Get("current_user_id"); uid == nil {
c.Flash().Add("danger", "You must be authorized to see that page")
return c.Redirect(302, "/")
return c.Redirect(http.StatusFound, "/")
}
return next(c)
}
Expand Down
35 changes: 27 additions & 8 deletions genny/goth/goth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package goth

import (
"embed"
"fmt"
"io/fs"
"strings"
"text/template"

"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/genny/v2/gogen"
"github.com/pkg/errors"
)

//go:embed templates
Expand All @@ -18,15 +18,15 @@ func New(opts *Options) (*genny.Generator, error) {
g := genny.New()

if len(opts.Providers) == 0 {
return g, errors.New("you must specify at least one provider")
return g, fmt.Errorf("you must specify at least one provider")
}

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)
}

h := template.FuncMap{
Expand All @@ -44,22 +44,41 @@ func New(opts *Options) (*genny.Generator, error) {

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

f, err = gogen.AddImport(f, "github.com/markbates/goth/gothic")
if err != nil {
return errors.WithStack(err)
return fmt.Errorf("could not add import: %w", err)
}

expressions := []string{
"",
"// NOTE: this block should go before any resources",
"// that need to be protected by buffalo-goth!",
"auth := app.Group(\"/auth\")",
"auth.GET(\"/{provider}\", buffalo.WrapHandlerFunc(gothic.BeginAuthHandler))",
"auth.GET(\"/{provider}/callback\", AuthCallback)",
"",
}
f, err = gogen.AddInsideBlock(f, "if app == nil {", expressions...)

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(f)
})
Expand Down
13 changes: 9 additions & 4 deletions genny/goth/goth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ var app *buffalo.App
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{})
// NOTE: this block should go before any resources
// that need to be protected by buffalo-goth!
auth := app.Group("/auth")
auth.GET("/{provider}", buffalo.WrapHandlerFunc(gothic.BeginAuthHandler))
auth.GET("/{provider}/callback", AuthCallback)
}
return app
Expand All @@ -85,14 +89,15 @@ const authAfter = `package actions
import (
"fmt"
"net/http"
"os"
"github.com/gobuffalo/buffalo"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/twitter"
)
"github.com/markbates/goth/providers/twitter"
)
func init() {
gothic.Store = App().SessionStore
Expand All @@ -106,9 +111,9 @@ func init() {
func AuthCallback(c buffalo.Context) error {
user, err := gothic.CompleteUserAuth(c.Response(), c.Request())
if err != nil {
return c.Error(401, err)
return c.Error(http.StatusUnauthorized, err)
}
// Do something with the user, maybe register them/sign them in
return c.Render(200, r.JSON(user))
return c.Render(http.StatusOK, r.JSON(user))
}
`
7 changes: 4 additions & 3 deletions genny/goth/templates/actions/auth.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package actions

import (
"fmt"
"net/http"
"os"

"github.com/gobuffalo/buffalo"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
{{range .providers -}}
"github.com/markbates/goth/providers/{{ downcase . }}"
{{end -}}
{{end -}}
)

func init() {
Expand All @@ -29,8 +30,8 @@ func init() {
func AuthCallback(c buffalo.Context) error {
user, err := gothic.CompleteUserAuth(c.Response(), c.Request())
if err != nil {
return c.Error(401, err)
return c.Error(http.StatusUnauthorized, err)
}
// Do something with the user, maybe register them/sign them in
return c.Render(200, r.JSON(user))
return c.Render(http.StatusOK, r.JSON(user))
}
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module github.com/gobuffalo/buffalo-goth
go 1.16

require (
github.com/gobuffalo/genny/v2 v2.0.12
github.com/gobuffalo/meta v0.3.2
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0
github.com/gobuffalo/genny/v2 v2.1.0
github.com/gobuffalo/meta v0.3.3
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
)
Loading

0 comments on commit 15d670d

Please sign in to comment.