Authentication
Advanced Topics
Get Session Data

Get Data on The Current Session

Client-side

If you want to access the current user's data, e.g., to show the user's first name, you can either use the useSession hook or an async helper.

Hooks

useSession()
import { useSession } from "@roq/ui-react";
 
const { session, status } = useSession();
const firstName = session.user.firstName;
refetchSession()

This method is used to refetch the current token & session data (e.g., after a user updates their profile).

import { useRoqComponents } from "@roq/ui-react";
function Button() {
  const { refetchSession } = useRoqComponents();
  return <button onClick={refetchSession}>Refetch session</button>;
}

Async Helper

import { getSession } from "@roq/ui-react";
 
const user = await getSession();
const firstName = user.session.user.firstName;

Server-side

You can access the current user's data using this request handler:

import { getServerSession } from "@roq/nextjs";
 
async function handler(req: NextApiRequest, res: NextApiResponse) {
  const session = getServerSession(req);
  const user = session.user;
}