Fetch User Details

Learn how to read data from a MongoDB database using Go.

Reading data from a database

Users can browse through the app and perform different activities when they log in successfully. When they go to the “Profile” tab, they should be able to see details about themselves, for example, the name and username they provided during registration.

  1. When users log in successfully and navigate to their “Profile” tab, they want to see details about their account. To do this, we must fetch these details from our database.

  2. To fetch the user's details, we create an endpoint that takes the user's ID as a parameter and then searches for the associated account.

  3. If the account exists, the user details are returned; if not, an error is thrown.

Create authentication helpers

First, create a file called authHelper.go. This file contains the logic our system needs to verify the user type trying to access a specific resource. This is useful because our system is multi-tenant, in a way. We crosscheck the user_type field to see if that particular user_type has been authorized.

Get hands-on with 1200+ tech skills courses.