gogo-server

How can I Authenticate any login for a http request done by client?


I have an HTTP server in Go in which when client is trying to login I have to authenticate credentials and in return i want to send success or failure. Later if any request come, I need to authenticate it using tokenID generated and on success i have to send a file.

I have tried the above using cookies. But cookies values are visible when opening cookies tab. So i need to send encrypt cookie. Please tell me a way to do so IF its possible.


Solution

  • ERROR:= "http: superfluous response.WriteHeader call". This error is coming as you cannot send two response for one request.

    The best way to achieve what you are trying to do is by using cookies. Send the data in the form of cookies and Bingo. Your work will be done without errors/warnings.

    expiration := time.Now().Add(time.Second * time.Duration(1000))
    cookie := http.Cookie{Name: "Token", Value: "username", Expires: expiration}
    http.SetCookie(w, &cookie)
    usercookie := http.Cookie{Name: "usercookie", Value: "username", Expires: expiration}
    http.SetCookie(w, &usercookie)
    http.ServeFile(w, r, r.URL.Path[1:])
    

    This Code will create a cookie and later you can access it. This is the right way of achieving what you want.