I have no idea why the first function works perfectly and the second function never logs out and I am always logged in. The structure defined corresponds to a templates variable.
MOST strange thing is that in the two different handlers the call to LogoutURL generates two different links (not only the return URL the structure of those two are different one[index page's one which works] starts with myapp-notworking-000.appspot.com and then _ah/logout URL and the admin pages one starts with https://google.com/accounts and all). What am I missing here? Why is GAE so complicated??
import (
"appengine"
"appengine/user"
"fmt"
"html/template"
"net/http"
)
var templates = template.Must(template.ParseGlob("static/tmpl/*"))
type AdminPage struct {
Admin_email string
LogoutUrl string
}
func init() {
http.HandleFunc("/", Index)
http.HandleFunc("/admin", Admin)
}
func Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "text/html; charset=utf-8")
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, _ := user.LoginURL(c, "/")
fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
return
}
url, _ := user.LogoutURL(c, "/")
fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
}
func Admin(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
an := AdminPage{"", ""}
var templ = ""
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
c.Debugf("User present %v", u)
an.Admin_email = u.Email
templ = "adminIndex"
url, err := user.LoginURL(c, "/admin")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
an.LogoutUrl = url
err = templates.ExecuteTemplate(w, templ, an)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
app.yaml file -----
application: myapp-notworking-000
version: beta
runtime: go
api_version: go1
handlers:
- url: /static
static_dir: static
application_readable: true
- url: /css
static_dir: static/css
application_readable: true
- url: /js
static_dir: static/js
application_readable: true
- url: /images
static_dir: static/images
application_readable: true
- url: /fonts
static_dir: static/fonts
application_readable: true
- url: /.*
script: _go_app