API
Composables
API reference for UNuxt composables
Reference for the composables available in UNuxt.
useUserSession
Access the current user session and authentication state.
const { user, session, loggedIn, fetch, clear } = useUserSession()
Returns
| Property | Type | Description |
|---|---|---|
user | Ref<User | null> | Current user object |
session | Ref<Session | null> | Current session data |
loggedIn | ComputedRef<boolean> | Whether user is authenticated |
fetch | () => Promise<void> | Refresh session from server |
clear | () => Promise<void> | Clear session and logout |
Example
<script setup lang="ts">
const { user, loggedIn, clear } = useUserSession()
async function logout() {
await clear()
navigateTo('/auth/login')
}
</script>
useOrganization
Manage organization state and permissions.
const {
organizations,
activeOrganization,
setActiveOrganization,
hasPermission
} = useOrganization()
Returns
| Property | Type | Description |
|---|---|---|
organizations | Ref<Organization[]> | User's organizations |
activeOrganization | Ref<Organization | null> | Currently active organization |
setActiveOrganization | (id: string) => Promise<void> | Switch active organization |
hasPermission | (permission: string) => boolean | Check user permission |
Example
<script setup lang="ts">
const { activeOrganization, hasPermission } = useOrganization()
const canManageMembers = computed(() => hasPermission('member:invite'))
</script>
useTheme
Control application theming and color mode.
const { primaryColor, colorMode, setTheme } = useTheme()
Returns
| Property | Type | Description |
|---|---|---|
primaryColor | Ref<string> | Current primary color |
colorMode | Ref<'light' | 'dark'> | Current color mode |
setTheme | (color: string, mode: string) => void | Update theme |
Example
<script setup lang="ts">
const { primaryColor, colorMode, setTheme } = useTheme()
function applyTheme(color: string) {
setTheme(color, colorMode.value)
}
</script>
use2FA
Manage two-factor authentication.
const { enable2FA, verify2FA, disable2FA } = use2FA()
Returns
| Property | Type | Description |
|---|---|---|
enable2FA | () => Promise<{ qrCode: string }> | Enable 2FA, returns QR code |
verify2FA | (code: string) => Promise<void> | Verify 2FA code |
disable2FA | (code: string) => Promise<void> | Disable 2FA |
Example
<script setup lang="ts">
const { enable2FA, verify2FA } = use2FA()
const qrCode = ref('')
const verificationCode = ref('')
async function setup2FA() {
const { qrCode: code } = await enable2FA()
qrCode.value = code
}
async function confirm2FA() {
await verify2FA(verificationCode.value)
}
</script>