Skip to content

Instantly share code, notes, and snippets.

@dictav
Last active April 27, 2018 02:14
Show Gist options
  • Save dictav/5e98c5da90f64753d1cf1e12386eba20 to your computer and use it in GitHub Desktop.
Save dictav/5e98c5da90f64753d1cf1e12386eba20 to your computer and use it in GitHub Desktop.
AppEngine Go User API Authentication
package app
import (
"fmt"
"net/http"
"strings"
"google.golang.org/appengine"
"google.golang.org/appengine/user"
)
func init() {
http.HandleFunc("/", welcome)
}
func welcome(w http.ResponseWriter, r *http.Request) {
println("-----")
for k, values := range r.Header {
println(k, strings.Join(values, ","))
}
println("-----")
w.Header().Set("Content-type", "text/html; charset=utf-8")
ctx := appengine.NewContext(r)
u := user.Current(ctx)
if u == nil {
url, _ := user.LoginURL(ctx, "/")
fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
return
}
url, _ := user.LogoutURL(ctx, "/")
fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
}
service: default
runtime: go
api_version: go1.8
handlers:
- url: /.*
script: _go_app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment