Homepage

[DEPRECATED] Accessing Authenticated User Data

Last edit: Jun 10, 2024

Warning

This article series promotes UserProfiles and Forms, which are deprecated. We decided to reduce the learning curve by promoting explicit implementation via Liquid, Pages and GraphQL, instead of built-in features, which add magic into the mix increasing the learning curve and making debugging harder. Please refer to our Get Started to read up-to date articles, including User Authentication

This guide will help you access information of authenticated users.

Requirements

To follow the steps in this tutorial, you should be familiar with the required directory structure for your codebase, and understand the concepts of pages and users, and GraphQL. To have information stored of authenticated users that you can access, make sure you have followed the previous tutorials in the Users section.

Steps

Accessing authenticated user data is a two-step process:

Step 1: Create GraphQL query

app/graphql/current_user.graphql
query current_user {
  current_user {
    id
    slug
    first_name
    email
    developer_profile: profile(profile_type: "developer") {
      id
    }
    client_profile: profile(profile_type: "client") {
      id
    }
  }
}

Step 2: Fetch information from GraphQL file

On any given page (including layout itself, but be careful with adding queries to the layout), you can add this liquid tag:


{% graphql g = 'current_user' %}

This tag fetches information defined in the GraphQL file for the currently logged in user and stores it in a variable named g. The returned data is a standard hash, so you can even display it via {{ g }}.

Next steps

Congratulations! You know how to access information of authenticated users. Now you can learn about sending an Email Notification to a newly signed up user:

Questions?

We are always happy to help with any questions you may have.

contact us