currentUser
The main current user service for managing user authentication and authorization in Flow applications. This service provides a complete interface for wallet connections, user sessions, transaction signing, and user data management. It handles the complexity of connecting to various FCL-compatible wallets, managing authentication state, and providing authorization functions for transaction signing.
The currentUser service is configured for web platforms and uses the browser's localStorage by default for session persistence. It integrates with Flow's discovery service to enable wallet selection and supports both authentication and re-authentication flows.
This service is reactive and provides subscription capabilities to monitor authentication state changes in real-time. All wallet interactions are handled through FCL's standardized protocols, ensuring compatibility with the Flow ecosystem.
Returns an object with the following methods:
_10{_10authenticate, // Authenticates the user via FCL-compatible wallets_10unauthenticate, // Logs out the current user and clears session data_10authorization, // Produces authorization details for transaction signing_10signUserMessage, // Signs arbitrary messages with the user's wallet_10subscribe, // Subscribes to authentication state changes_10snapshot, // Returns the current user object snapshot_10resolveArgument // Resolves the current user as a transaction argument_10}
Import
You can import the entire package and access the function:
_10import * as fcl from "@onflow/fcl"_10_10fcl.currentUser()
Or import directly the specific function:
_10import { currentUser } from "@onflow/fcl"_10_10currentUser()
Usage
_36// Basic authentication flow_36import * as fcl from "@onflow/fcl"_36_36// Configure FCL_36fcl.config({_36 "accessNode.api": "https://rest-testnet.onflow.org",_36 "discovery.wallet": "https://fcl-discovery.onflow.org/testnet/authn",_36 "flow.network": "testnet"_36})_36_36// Authenticate user_36const user = await fcl.currentUser.authenticate()_36console.log("User authenticated:", user.addr)_36_36// Check authentication status_36const currentUser = await fcl.currentUser.snapshot()_36if (currentUser.loggedIn) {_36 console.log("User is logged in:", currentUser.addr)_36}_36_36// Subscribe to authentication state changes_36import * as fcl from "@onflow/fcl"_36_36const unsubscribe = fcl.currentUser.subscribe((user) => {_36 if (user.loggedIn) {_36 console.log("User logged in:", user.addr)_36 document.getElementById("login-btn").style.display = "none"_36 document.getElementById("logout-btn").style.display = "block"_36 } else {_36 console.log("User logged out")_36 document.getElementById("login-btn").style.display = "block"_36 document.getElementById("logout-btn").style.display = "none"_36 }_36})_36// Clean up subscription when component unmounts_36window.addEventListener("beforeunload", () => unsubscribe())
Returns
_10export interface CurrentUserService extends CurrentUserServiceApi {_10 (): CurrentUserServiceApi_10}
A CurrentUserService object