2

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

sigma
  • 21
  • 1
  • Did you follow the steps documented here - https://cloud.google.com/appengine/docs/standard/go/services/access – NoCommandLine Mar 01 '22 at 06:16
  • Thank you for your comment. Yes. I did it according to the procedure. However, it still does not solve. – sigma Mar 01 '22 at 06:46

2 Answers2

1

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

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15
  • Thank you for politely teaching me. Thank you very very much. I will report if I can solve it. – sigma Mar 02 '22 at 04:46
0

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!

sigma
  • 21
  • 1