How to use captchae in my login form in GO?

I want to use captchae in my login form in my login page. I am using “github.com/afocus/captcha”. and the following code for this purpose:

func captchae(w http.ResponseWriter, req *http.Request) {
pd := captcha.New{
// Must set a font, Other settings have default values
cap.SetFont(“comic.ttf”)
img str := cap.Create(4, captcha.NUM)
}
err := tpl.ExecuteTemplate(w, “admin.gohtml”, pd)
if err != nil {
log.Println(err)
http.Error(w, “Internal server error”, http.StatusInternalServerError)
return
}
}
But it is throwing some error. Can you please suggest me what is the correct way of using captchae in my login page.

Thanks
Deepak

What is the error?

Sorry for being late in reply. The error coming is as following:-

go build main.go

command-line-arguments

.\main.go:84: syntax error: unexpected semicolon or newline, expecting comma or
}

My function for Captcha is as following:-

func captchae(w http.ResponseWriter, req *http.Request) {

pd := captcha.New{
 //Must set a font, Other settings have default values
cap.SetFont("comic.ttf")
img str := cap.Create(4, captcha.NUM)
}
err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}

}

I think this is what you intended:

func captchae(w http.ResponseWriter, req *http.Request) {
	pd := captcha.New()
	//Must set a font, Other settings have default values
	pd.SetFont("comic.ttf")
	img, str := pd.Create(4, captcha.NUM)

	err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
	if err != nil {
		log.Println(err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}
}
  • captcha.New is a function, not a struct literal as what you wrote seems to indicate. It takes no arguments
  • since you called your captca pd, I renamed the references to cap
  • cap.Create returns two values. They require a comma to separate them

Thanks a lot for the reply. After making the changes that you have suggested like this:-

func captchae(w http.ResponseWriter, req *http.Request) {
pd := captcha.New()
 //Must set a font, Other settings have default values
pd.SetFont("comic.ttf")
img, str := pd.Create(4, captcha.NUM)

err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}  }

It is giving this error now:-

go build main.go

command-line-arguments

.\main.go:85: img declared and not used
.\main.go:85: str declared and not used

because you just declared the variables but not using them anywhere

You might want to look at the example code: https://github.com/afocus/captcha/blob/master/examples/main.go

Sadly that example isn’t a complete solution because it doesn’t do everything. My quick thoughts on what a captcha flow requires:

  1. generate the text and image; server remembers the text, sends image to browser
  2. browser displays image
  3. user fills in what the text is; POSTs to server
  4. server compares what the user thinks the text is with the remembered text
  5. goto 1 if incorrect, proceed if correct

Both your code and the example code don’t do all of this.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.