[GH-ISSUE #5632] Users country #27630

Closed
opened 2026-04-17 18:44:20 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @Yorizel on GitHub (Oct 28, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5632

How can i get the users country on the session?

Originally created by @Yorizel on GitHub (Oct 28, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5632 How can i get the users country on the session?
GiteaMirror added the locked label 2026-04-17 18:44:20 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 28, 2025):

Better Auth doesn't add the user's country to the session by default, but you have a few ways to include it:

  1. If your OAuth provider returns country info, you can add custom fields like country to your user object using the additionalFields config and map it in mapProfileToUser. Then, use the customSession plugin to expose it in the session response (example):
// In your auth config
additionalFields: {
  country: { type: "string" },
  countryCode: { type: "string" }
},
mapProfileToUser: async (profile) => ({
  // ...other fields
  country: profile.data.personal.country.name,
  countryCode: profile.data.personal.country.id,
})

// In your custom session plugin
import { customSession } from "better-auth/plugins/custom-session";
export const myCustomSession = customSession(async ({ user, session }, ctx) => ({
  ...session,
  country: user.country,
}));
  1. If you want to derive country from the user's IP, use a geolocation package like geoip-lite in your custom session function (example):
import geoIp from "geoip-lite";
customSession(async ({ user, session }) => {
  let country = undefined;
  if (session.ipAddress) {
    const res = geoIp.lookup(session.ipAddress);
    if (res) country = res.country;
  }
  return { ...session, country };
});
  1. If you're running on Cloudflare, the cloudflare plugin can automatically add geolocation fields (including country) to the session (details).

For all approaches, be mindful of privacy and only enrich sessions with location data if users have consented. If you want to persist country info in your database, use a database hook to update the user or session record on sign-in (feature discussion).

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3454097232 --> @dosubot[bot] commented on GitHub (Oct 28, 2025): <!-- Answer --> Better Auth doesn't add the user's country to the session by default, but you have a few ways to include it: 1. If your OAuth provider returns country info, you can add custom fields like `country` to your user object using the `additionalFields` config and map it in `mapProfileToUser`. Then, use the `customSession` plugin to expose it in the session response ([example](https://github.com/better-auth/better-auth/discussions/4387#discussioncomment-14296973)): ```typescript // In your auth config additionalFields: { country: { type: "string" }, countryCode: { type: "string" } }, mapProfileToUser: async (profile) => ({ // ...other fields country: profile.data.personal.country.name, countryCode: profile.data.personal.country.id, }) // In your custom session plugin import { customSession } from "better-auth/plugins/custom-session"; export const myCustomSession = customSession(async ({ user, session }, ctx) => ({ ...session, country: user.country, })); ``` 2. If you want to derive country from the user's IP, use a geolocation package like `geoip-lite` in your custom session function ([example](https://github.com/better-auth/better-auth/issues/821)): ```typescript import geoIp from "geoip-lite"; customSession(async ({ user, session }) => { let country = undefined; if (session.ipAddress) { const res = geoIp.lookup(session.ipAddress); if (res) country = res.country; } return { ...session, country }; }); ``` 3. If you're running on Cloudflare, the `cloudflare` plugin can automatically add geolocation fields (including `country`) to the session ([details](https://github.com/better-auth/better-auth/issues/5452)). For all approaches, be mindful of privacy and only enrich sessions with location data if users have consented. If you want to persist country info in your database, use a database hook to update the user or session record on sign-in ([feature discussion](https://github.com/better-auth/better-auth/issues/4743)). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/6a352037-6a31-4617-906d-4e30aa6627cf?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/6a352037-6a31-4617-906d-4e30aa6627cf?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/6a352037-6a31-4617-906d-4e30aa6627cf?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/6a352037-6a31-4617-906d-4e30aa6627cf?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/6a352037-6a31-4617-906d-4e30aa6627cf?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/6a352037-6a31-4617-906d-4e30aa6627cf?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/6a352037-6a31-4617-906d-4e30aa6627cf?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/5632)
Author
Owner

@himself65 commented on GitHub (Jan 3, 2026):

You can add additionalFields to the user schema.

<!-- gh-comment-id:3706911322 --> @himself65 commented on GitHub (Jan 3, 2026): You can add `additionalFields` to the user schema.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27630