Home > OS >  go Unable to get login user information
go Unable to get login user information

Time:03-10

Hello. I'm using the go language in Google App Engine. I'm having trouble getting the logged-in user's information. Similarly, the login URL and logout URL cannot be obtained. All nil will be returned. user.IsAdmin (c) returns false. please help me.



admin.go

func Entry(w http.ResponseWriter, r *http.Request) {
    ...
    c := appengine.NewContext(r)

    inUrl, err := user.LoginURL(c, "/admin/top/")
    ...
}

func AdminTop(w http.ResponseWriter, r *http.Request) {
    ...
    c := appengine.NewContext(r)
    booisadmin := user.IsAdmin(c)
    u := user.Current(c)
    outUrl, err := user.LogoutURL(c, "/")
    ...
}




app.yaml

runtime: go116

app_engine_apis: true

handlers:
  - url: /assets/css
    mime_type: text/css
    static_dir: assets/css

  - url: /assets/html
    mime_type: text/html
    static_dir: assets/html

  - url: /assets/img
    static_dir: assets/img

  - url: /admin/.*
    login: require
    script: _go_app

  - url: /.*
    script: _go_app

CodePudding user response:

When you use login: required in app.yaml, you can get the logged in users information via the following headers -

  • X-Appengine-User-Id
  • X-Appengine-User-Nickname
  • X-Appengine-User-Email

I confirmed the above works in Go (ran it on my local machine)

I believe the same headers should work when you use the Users API but you can always dump all the headers to figure out the values that you need.

Regarding using the User API to get login/logout urls, I also got blank values when I tried it on my local machine but I'm a novice when it comes to Go. You might want to try and see if the calls work when you deploy to Production

CodePudding user response:

Thank you. I was able to solve it. It was because I was using Gorillamux. I solved it with the following code.

import (
    ...
    "github.com/gorilla/mux"
    "net/http"
    ...
)

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", indexHandler)
    r.HandleFunc("/admin/", admin.Entry)
    http.Handle("/", r)
}

The last http.Handle ("/", r) was missing.

I wrote the details here. https://uubaago.blogspot.com/

Thank you very much NoCommandLine!

  • Related