Use an Auth provider to communicate with your backend. This provider handles login, logout, user identity, and session checks.
The Auth interface defines the contract:
interface Auth {
// Sign in the user
login: (params?: any) => Promise<LoginResult | void>
// Sign out the user
logout: (params?: any) => Promise<LogoutResult | void>
// Check if the user is authenticated
check: (params?: any) => Promise<CheckAuthResult>
// Handle API errors (e.g. 401 Unauthorized)
checkError: (error: any) => Promise<CheckAuthErrorResult>
// Get user identity (optional)
getIdentity?: (params?: any) => Promise<GetIdentityResult>
}
Use useLogin to handle the sign-in process. This composable manages the mutation state and side effects like redirection.
<script setup lang="ts">
import { useLogin } from '@ginjou/vue'
const { mutate: login, isPending } = useLogin()
function handleSubmit(credentials) {
login(credentials)
}
</script>
useLogin composes data composables and actions to provide a complete flow.
useMutation (TanStack Query) to manage the request state.auth.login from your provider.auth query to update the UI.Use useLogout to sign out the user.
<script setup lang="ts">
import { useLogout } from '@ginjou/vue'
const { mutate: logout } = useLogout()
</script>
useMutation.auth.logout.Use useAuthenticated to check if the current user is logged in. This is often used to protect routes or show/hide UI elements.
<script setup lang="ts">
import { useAuthenticated } from '@ginjou/vue'
const { data, isLoading } = useAuthenticated()
</script>
useQuery to fetch authentication status.auth.check.Use useGetIdentity to fetch the current user's profile information (e.g. name, avatar, rules).
<script setup lang="ts">
import { useGetIdentity } from '@ginjou/vue'
const { data: identity } = useGetIdentity()
</script>
useQuery to fetch identity data.auth.getIdentity.Use useCheckError to automatically handle API errors. This is usually triggered when an API returns an error (like 401 Unauthorized) to determine if the user should be logged out.
<script setup lang="ts">
import { useCheckError } from '@ginjou/vue'
const { mutate: checkError } = useCheckError()
// Example usage within a query error handler
function handleQueryError(error) {
checkError({ error })
}
</script>
useMutation.auth.checkError.logout automatically.