Skip to content

Instantly share code, notes, and snippets.

@jklydev
Last active September 23, 2016 21:47
Show Gist options
  • Save jklydev/267083e9fa776eb7d35c38fb8447e57c to your computer and use it in GitHub Desktop.
Save jklydev/267083e9fa776eb7d35c38fb8447e57c to your computer and use it in GitHub Desktop.
package main
import (
"os"
"fmt"
"net/http"
rc_api "github.com/JKiely/RC-API"
)
const htmlIndex = `<html><body>
<a href="/RCLogin">Log in with RC</a>
</body></html>
`
var config *rc_api.Config;
var rc rc_api.Auth;
func main() {
config = rc_api.MakeConfig(
"http://localhost:3000/recurse", //Your redirect url
os.Getenv("ClientID"),
os.Getenv("ClientSecret"),
)
http.HandleFunc("/", handleMain)
http.HandleFunc("/RCLogin", handleRCLogin)
http.HandleFunc("/me", handleMe)
http.HandleFunc("/recurse", handleRCRedirect)
fmt.Println(http.ListenAndServe(":3000", nil))
}
func handleMain(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, htmlIndex)
}
func handleRCLogin(w http.ResponseWriter, r *http.Request) {
url := config.GetUrl("StateString")
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func handleMe(w http.ResponseWriter, r *http.Request) {
me:= rc.Me()
str := me.Name
fmt.Fprintf(w, str)
}
func handleRCRedirect(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code")
rc = config.MakeAuth(code)
http.Redirect(w, r, "/me", http.StatusTemporaryRedirect)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment