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.The checkError function examines API errors and determines whether they indicate an authentication failure. Here are common error handling patterns:
Pattern 1: Auto-Logout on 401 Unauthorized
// In your Auth provider
const auth = {
checkError: async (error) => {
// If 401, session is invalid
if (error.status === 401) {
// Trigger logout
window.location.href = '/login'
return { logout: true }
}
// Otherwise, let the error propagate
return { logout: false }
},
}
Pattern 2: Retry with Refresh Token
const auth = {
checkError: async (error) => {
if (error.status === 401) {
try {
// Attempt to refresh the token
await refreshToken()
return { logout: false } // Retry the request
}
catch {
// Refresh failed - session is truly invalid
return { logout: true }
}
}
return { logout: false }
},
}
Pattern 3: Differentiate Between Error Types
const auth = {
checkError: async (error) => {
if (error.status === 401) {
// Session expired or invalid credentials
return { logout: true }
}
if (error.status === 403) {
// User lacks permissions - don't logout
return { logout: false }
}
if (error.status === 500) {
// Server error - don't logout
return { logout: false }
}
return { logout: false }
},
}