Documentation
supastarter for Nuxtsupastarter for NuxtAuthentication

User and session

Learn how to access the user and session in your supastarter application.

Accessing the user and session

You can access the user and session in your application from the useSession composable.

<script setup lang="ts">
const { user, session } = useSession();
</script>

Both can be null if the user is not authenticated, but if you use the composable inside an /app/... route, they should always be defined.

The user object contains the information of the authenticated user and the session object contains the session data.

type Session = {
    id: string;
    userId: string;
    createdAt: Date;
    updatedAt: Date;
    expiresAt: Date;
    token: string;
    ipAddress?: string | null | undefined;
    userAgent?: string | null | undefined;
    impersonatedBy?: string | null | undefined;
}

type User = {
    id: string;
    createdAt: Date;
    updatedAt: Date;
    email: string;
    emailVerified: boolean;
    name: string;
    image?: string | null | undefined;
    role: "admin" | "user";
    onboardingComplete: boolean;
}

Reload session

If for some reason you need to reload the session, for example when you changed some property of the user like its name or role, you can use the reloadSession function.

<script setup lang="ts">
const { reloadSession } = useSession();

await reloadSession();
</script>