The User SDK allows you to interact with the current user. Here we list the methods available in the User SDK.
The example below work within a Server Components: by using the required client, you can easily adapt the code examples below to both Server Actions and Route Handlers.
The User SDK is namespaced under sdk.user
- so all the methods below are available under sdk.user
.
Get the current user
To get the current user, use the sdk.user.getCurrent()
method:
import { LoaderFunctionArgs } from '@remix-run/node';
import getSdk from '~/lib/sdk';
import getSupabaseServerClient from '~/core/supabase/server-client';
export async function loader(args: LoaderFunctionArgs) {
const client = getSupabaseServerClient(args.request);
const sdk = getSdk(client);
const user = await sdk.user.getCurrent();
// ...
}
This method wraps the Supabase auth.getUser()
function.
Get the current user's session
To get the current session, use the sdk.user.getCurrentSession()
method:
import { LoaderFunctionArgs } from '@remix-run/node';
import getSdk from '~/lib/sdk';
import getSupabaseServerClient from '~/core/supabase/server-client';
export async function loader(args: LoaderFunctionArgs) {
const client = getSupabaseServerClient(args.request);
const sdk = getSdk(client);
const session = await sdk.user.getCurrentSession();
// ...
}
This method wraps the Supabase auth.getSession()
function.
Get the current user's ID
To get the current user's ID, use the sdk.user.getCurrentId()
method:
import { LoaderFunctionArgs } from '@remix-run/node';
import getSdk from '~/lib/sdk';
import getSupabaseServerClient from '~/core/supabase/server-client';
export async function loader(args: LoaderFunctionArgs) {
const client = getSupabaseServerClient(args.request);
const sdk = getSdk(client);
const userId = await sdk.user.getCurrentId();
// ...
}
Get the current user's Database profile
We store the user's record data in the database table users
. To get the current user's profile data, use the sdk.user.getData()
method:
import { LoaderFunctionArgs } from '@remix-run/node';
import getSdk from '~/lib/sdk';
import getSupabaseServerClient from '~/core/supabase/server-client';
export async function loader(args: LoaderFunctionArgs) {
const client = getSupabaseServerClient(args.request);
const sdk = getSdk(client);
const data = await sdk.user.getData();
// ...
}
The data
object will contain the user's profile data.
interface UserData {
displayName: string | null;
photoUrl: string | null;
// your custom fields...
}
It's very likely that you'll be adding more fields, so this interface may change.