Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Package hcaptcha is a middleware that provides hCaptcha integration for Flamego

License

Notifications You must be signed in to change notification settings

flamego/hcaptcha

Repository files navigation

hcaptcha

GitHub Workflow Status GoDoc

Package hcaptcha is a middleware that provides hCaptcha rendering integration for Flamego.

Installation

go get github.com/flamego/hcaptcha

Getting started

<!-- templates/home.tmpl -->
<html>
<head>
 <script src="https://hcaptcha.com/1/api.js"></script>
</head>
<body>
 <form method="POST">
 <div class="h-captcha" data-sitekey="{{.SiteKey}}"></div>
 <input type="submit" name="button" value="Submit">
 </form>
</body>
</html>
package main
import (
	"fmt"
	"net/http"
	"github.com/flamego/flamego"
	"github.com/flamego/hcaptcha"
	"github.com/flamego/template"
)
func main() {
	f := flamego.Classic()
	f.Use(template.Templater())
	f.Use(hcaptcha.Captcha(
		hcaptcha.Options{
			Secret: "<SECRET>",
		},
	))
	f.Get("/", func(t template.Template, data template.Data) {
		data["SiteKey"] = "<SITE KEY>"
		t.HTML(http.StatusOK, "home")
	})
	f.Post("/", func(w http.ResponseWriter, r *http.Request, h hcaptcha.HCaptcha) {
		token := r.PostFormValue("h-captcha-response")
		resp, err := h.Verify(token)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(err.Error()))
			return
		} else if !resp.Success {
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(fmt.Sprintf("Verification failed, error codes %v", resp.ErrorCodes)))
			return
		}
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte("Verified!"))
	})
	f.Run()
}

Getting help

License

This project is under the MIT License. See the LICENSE file for the full license text.

About

Package hcaptcha is a middleware that provides hCaptcha integration for Flamego

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

Languages

AltStyle によって変換されたページ (->オリジナル) /