Last active
April 27, 2018 02:14
-
-
Save dictav/5e98c5da90f64753d1cf1e12386eba20 to your computer and use it in GitHub Desktop.
AppEngine Go User API Authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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