Search code examples
node.jsgoogle-app-enginegoogle-cloud-platformgoogle-signin

How to access logged in user data in GAE?


I have a site dashboard on GAE standard node.js environment that uses login: admin option in handlers: element in my app.yaml file. This option makes google login window appear before accessing the site. Is it possible to get email and profile of user logged in?


Solution

  • Updates on Feb 2

    • Tested on my local server

    • I added login:required in app.yaml; it's a Python3 App but I did not use users api; I start the app with dev_appserver.py to make sure it uses the app.yaml file in the development environment; When I loaded the home page, it prompted me to sign in with [email protected]

    • I dumped the header output and it included the following

       X-Appengine-User-Email: [email protected]
       X-Appengine-User-Id: XXXXX
    

    Original Answer

    1. You can use the 'users' Api and call the function GetCurrentUser()

    Example, if your code was in python, you'd have something like

        from google.appengine.api import users
    
        # Get information about the currently logged in user
        users.GetCurrentUser()
    
        # Extra - if you wanted to confirm this is an admin user
        users.IsCurrentUserAdmin()
    
    1. Since you're not using Python, see if the environment variable for USER_EMAIL exists. The source code for users api has the snippet below (so see if it works for you)
        email = os.environ.get('USER_EMAIL', email)
        _user_id = os.environ.get('USER_ID', _user_id)