Skip to content

Commit ff77b9f

Browse files
committed
update README.md
1 parent 8c70cae commit ff77b9f

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

README.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<p align="center"><img src="https://arcaptcha.ir/logo.png" height="150px"></p>
1+
<p align="center"><img src="https://arcaptcha.ir/_nuxt/023053fecdcdf20e40bdc993c754d487.svg" height="100px"></p>
22

33
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/arcaptcha/arcaptcha-go)
44
[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/arcaptcha/arcaptcha-go/master/LICENSE)
@@ -7,34 +7,78 @@
77

88
Arcaptcha
99
=====================
10-
Arcaptcha library implementation in Golang to validate captcha.
10+
Arcaptcha library implementation in Golang to verify captcha.
1111

1212
## [Guide](https://arcaptcha.ir/guide)
13+
1314
### Installation
1415

1516
```
1617
go get -u github.com/arcaptcha/arcaptcha-go
1718
```
1819

1920
### Usage
21+
2022
Register on [Arcaptcha](https://arcaptcha.ir/), create website and get your own SiteKey and SecretKey
23+
24+
```go
25+
website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
26+
//'arcaptcha-token' is created for each captcha
27+
//After you put captcha widget in your website, you can get 'arcaptcha-token' from form
28+
result, err := website.Verify("arcaptcha-token")
29+
if err != nil {
30+
// error in sending or receiving API request
31+
// handle error
32+
}
33+
if !result.Success {
34+
// captcha not verified
35+
// can see result.ErrorCodes to find what's wrong
36+
// throw specific error
37+
}
38+
// it's ok
39+
```
40+
41+
#### Use in middleware:
42+
2143
```go
2244
package main
2345

2446
import (
25-
"log"
47+
"net/http"
2648

2749
"github.com/arcaptcha/arcaptcha-go"
2850
)
2951

52+
var website *arcaptcha.Website
53+
3054
func main() {
31-
website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
32-
//ChallengeID is created for each captcha
33-
//After you put captcha widget in your website, you can get challengeID
34-
if err := website.ValidateCaptcha("challengeID"); err != nil {
35-
log.Fatal(err)
36-
}
37-
//Its OK
38-
log.Printf("Captcha %v is valid and challenge succeeded", "challengeID")
55+
website = arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
56+
57+
myHandler := http.HandlerFunc(handler)
58+
http.Handle("/", verifyCaptcha(myHandler))
59+
http.ListenAndServe(":8000", nil)
60+
}
61+
62+
func handler(w http.ResponseWriter, r *http.Request) {
63+
// handle request
64+
}
65+
66+
func verifyCaptcha(next http.Handler) http.Handler {
67+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
68+
token := r.FormValue("arcaptcha-token")
69+
result, err := website.Verify(token)
70+
if err != nil {
71+
// error in sending or receiving API request
72+
// handle error
73+
}
74+
if !result.Success {
75+
// captcha not verified
76+
// can see result.ErrorCodes to find what's wrong
77+
// throw specific error
78+
return
79+
}
80+
// it's ok
81+
next.ServeHTTP(w, r)
82+
})
3983
}
4084
```

examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
## Arcaptcha Verification Example
1+
# Arcaptcha Verification Example
22

33
this example is an HTTP server that serves a html form with a captcha and test the verification.
44

5-
### Install
5+
## [Guide](https://arcaptcha.ir/guide)
6+
### Installation
67
```shell
78
go get -u github.com/arcaptcha/arcaptcha-go
89
cd $GOPATH/src/github.com/arcaptcha/arcaptcha-go

examples/example.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ func handleDemo(w http.ResponseWriter, r *http.Request) {
4444
_, submitted := r.Form["submitted"]
4545
if submitted {
4646
// can verify captcha
47-
tokenForm, _ := r.Form["arcaptcha-token"]
48-
token := tokenForm[0]
47+
token := r.FormValue("arcaptcha-token")
4948
result, err := website.Verify(token)
5049
if err != nil {
5150
log.Fatal(err)

0 commit comments

Comments
 (0)