mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
Add a better example for the recaptcha middleware as requested at: https://github.com/kataras/iris/issues/799
Former-commit-id: 85c3286a9d6be5cf47631e7608f70f3790934e64
This commit is contained in:
@@ -1,44 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
|
||||
"github.com/kataras/iris/middleware/recaptcha"
|
||||
)
|
||||
|
||||
// publicDataSiteKey and secretKey and should be obtained by https://www.google.com/recaptcha.
|
||||
// keys should be obtained by https://www.google.com/recaptcha
|
||||
const (
|
||||
publicDataSiteKey = ""
|
||||
secretKey = ""
|
||||
recaptchaPublic = ""
|
||||
recaptchaSecret = ""
|
||||
)
|
||||
|
||||
func showRecaptchaForm(ctx iris.Context, path string) {
|
||||
ctx.HTML(recaptcha.GetFormHTML(recaptchaPublic, path))
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
r := recaptcha.New(secretKey)
|
||||
// On both Get and Post on this example, so you can easly
|
||||
// use a single route to show a form and the main subject if recaptcha's validation result succeed.
|
||||
app.HandleMany("GET POST", "/", func(ctx iris.Context) {
|
||||
if ctx.Method() == iris.MethodGet {
|
||||
showRecaptchaForm(ctx, "/")
|
||||
return
|
||||
}
|
||||
|
||||
app.Get("/comment", showRecaptchaForm)
|
||||
result := recaptcha.SiteFerify(ctx, recaptchaSecret)
|
||||
if !result.Success {
|
||||
/* redirect here if u want or do nothing */
|
||||
ctx.HTML("<b> failed please try again </b>")
|
||||
return
|
||||
}
|
||||
|
||||
// pass the middleware before the main handler or use the `recaptcha.SiteVerify`.
|
||||
app.Post("/comment", r, postComment)
|
||||
ctx.Writef("succeed.")
|
||||
})
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
var htmlForm = `<form action="/comment" method="POST">
|
||||
<script src="https://www.google.com/recaptcha/api.js"></script>
|
||||
<div class="g-recaptcha" data-sitekey="%s"></div>
|
||||
<input type="submit" name="button" value="Verify">
|
||||
</form>`
|
||||
|
||||
func showRecaptchaForm(ctx iris.Context) {
|
||||
contents := fmt.Sprintf(htmlForm, publicDataSiteKey)
|
||||
ctx.HTML(contents)
|
||||
}
|
||||
|
||||
func postComment(ctx iris.Context) {
|
||||
// [...]
|
||||
ctx.JSON(iris.Map{"success": true})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user