# Fetching users by customId

**URL:** <https://forum.heroiclabs.com/t/fetching-users-by-customid/2891>\
**Category:** Runtime Framework\
**Created:** [August 3, 2022, 9:51am UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891 "2022-08-03T09:51:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anselm](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@anselm](https://forum.heroiclabs.com/u/anselm)\
**Post date:** [August 3, 2022, 9:51am UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891/1 "2022-08-03T09:51:54Z")

</div>

Hi,

we implemented a custom authentication flow using our existing backend.  
Using a `BeforeAuthenticateCustom` hook, we set the customId of the user to the user-id from our existing backend.

Now we would like to implement an rpc method that we can call from our existing backend. Is there any way we can get nakama accounts by their customId?

I only found methods to get an account by userId (accountGetId), facebookId (usersGetId) or username (usersGetUsername).

Is this currently just not possible without keeping track of userId and nakama-userId on our side, or am I missing something here?

Thanks!

---

<div class="post-metadata">

**Author:** ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)\
**Post date:** [August 5, 2022, 3:10pm UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891/2 "2022-08-05T15:10:20Z")

</div>

Hey @anselm, we definitely recommend authenticating to Nakama using your custom ID from your backend. Then you will want to retain the session object to make future calls to Nakama. If there is something that really prevents you from doing that, the lesser option is to store the custom id as a username for your Nakama user, and then query off that username. Lastly, we do not recommend writing a custom query to retrieve an account by custom\_id because it’s intentionally not indexed in the database.

---

<div class="post-metadata">

**Author:** ![anselm](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@anselm](https://forum.heroiclabs.com/u/anselm)\
**Post date:** [August 8, 2022, 6:38am UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891/3 "2022-08-08T06:38:44Z")

</div>

Hi @lugehorsam

> Then you will want to retain the session object to make future calls to Nakama

That’s what we do on the client-side, right?

What I am trying to do, is provide some RPC methods, that we can call server to server (using an [http\_key](https://heroiclabs.com/docs/nakama/server-framework/runtime-examples/#server-to-server)).

> Lastly, we do not recommend writing a custom query to retrieve an account by custom\_id because it’s intentionally not indexed in the database.

I was assuming that the custom\_id must be indexed because `AuthenticateCustom` must be able to efficiently check for the existing account. Also, it must be unique, right?

> the lesser option is to store the custom id as a username for your Nakama user, and then query off that username

If the username is guaranteed to be unique, that is a valid wordaround for us 🙂

---

<div class="post-metadata">

**Author:** ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)\
**Post date:** [August 8, 2022, 1:16pm UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891/4 "2022-08-08T13:16:08Z")

</div>

@anselm I don’t know the full details of your service but it is valid to store the session tokens in your service.

Yes apologies, `custom_id` is indexed and must be unique, but we still do not recommend writing custom SQL for this use case.

---

<div class="post-metadata">

**Author:** ![formatCvt](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/formatcvt/32/1127_2.png) [@formatCvt](https://forum.heroiclabs.com/u/formatCvt)\
**Post date:** [August 29, 2022, 2:54pm UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891/5 "2022-08-29T14:54:34Z")

</div>

> [@lugehorsam](#):
>
> Yes apologies, `custom_id` is indexed and must be unique, but we still do not recommend writing custom SQL for this use case.

but there is no other way to get user by custom id? Or i’m missing something? In our case we need to block user by their custom id and i see nothing to do this except custom sql

---

<div class="post-metadata">

**Author:** ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)\
**Post date:** [August 29, 2022, 6:28pm UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891/6 "2022-08-29T18:28:10Z")

</div>

Hey @formatCvt could you start a new forum post giving your specific use case and why you need custom ID instead of user id?

---

<div class="post-metadata">

**Author:** ![thegoldenmule](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/thegoldenmule/32/2118_2.png) [@thegoldenmule](https://forum.heroiclabs.com/u/thegoldenmule)\
**Post date:** [March 28, 2024, 4:09pm UTC](https://forum.heroiclabs.com/t/fetching-users-by-customid/2891/7 "2024-03-28T16:09:24Z")

</div>

Probably the most frustrating thing about Nakama is that the developers constantly think they know all use cases of the software, and instead of trying to actually help with your issues, run interference.

For anyone interested, here is the Go code I used to fetch users by `custom_id`. Since `custom_id` is indexed and unique, it is perfectly safe to do.

```auto
func getUserId(ctx context.Context, db *sql.DB, customId string) (string, error) {
	var userID string

	query := `
SELECT u.id
FROM users u
WHERE u.custom_id = $1`

	if err := db.QueryRowContext(ctx, query, customId).Scan(&userID); err != nil {
		if err == sql.ErrNoRows {
			return "", nil
		}

		return "", err
	}

	return userID, nil
}

```
