Skip to content

Instantly share code, notes, and snippets.

@motyar

motyar/login.go Secret

Created June 14, 2016 10:32
Show Gist options
  • Save motyar/dd1a441ed7214439afcb388b6c26b567 to your computer and use it in GitHub Desktop.
Save motyar/dd1a441ed7214439afcb388b6c26b567 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"html/template"
"net/http"
"os"
"github.com/gorilla/pat"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/twitter"
"sort"
)
func init() {
gothic.Store = sessions.NewFilesystemStore(os.TempDir(), []byte("goth-example"))
}
func main() {
goth.UseProviders(
twitter.New("bu7D17UKHEjK7UTfNReTcG4gK", "K4uJb0lcUsBikNN9L4M53vMCET7vLogA2TnTf1j2y5VmxsTlUp", "http://162.243.216.116:3000/auth/twitter/callback"),
)
m := make(map[string]string)
m["twitter"] = "Twitter"
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
providerIndex := &ProviderIndex{Providers: keys, ProvidersMap: m}
p := pat.New()
p.Get("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) {
user, err := gothic.CompleteUserAuth(res, req)
if err != nil {
fmt.Fprintln(res, err)
return
}
t, _ := template.New("foo").Parse(userTemplate)
t.Execute(res, user)
})
p.Get("/auth/{provider}", gothic.BeginAuthHandler)
p.Get("/", func(res http.ResponseWriter, req *http.Request) {
t, _ := template.New("foo").Parse(indexTemplate)
t.Execute(res, providerIndex)
})
http.ListenAndServe(":3000", p)
}
type ProviderIndex struct {
Providers []string
ProvidersMap map[string]string
}
var indexTemplate = `{{range $key,$value:=.Providers}}
<p><a href="/auth/{{$value}}">Log in with {{index $.ProvidersMap $value}}</a></p>
{{end}}`
var userTemplate = `
<p>Name: {{.Name}}</p>
<p>Email: {{.Email}}</p>
<p>NickName: {{.NickName}}</p>
<p>Location: {{.Location}}</p>
<p>AvatarURL: {{.AvatarURL}} <img src="{{.AvatarURL}}"></p>
<p>Description: {{.Description}}</p>
<p>UserID: {{.UserID}}</p>
<p>AccessToken: {{.AccessToken}}</p>
<p>ExpiresAt: {{.ExpiresAt}}</p>
<p>RefreshToken: {{.RefreshToken}}</p>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment