Skip to main content

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
{
_10
authenticate, // Authenticates the user via FCL-compatible wallets
_10
unauthenticate, // Logs out the current user and clears session data
_10
authorization, // Produces authorization details for transaction signing
_10
signUserMessage, // Signs arbitrary messages with the user's wallet
_10
subscribe, // Subscribes to authentication state changes
_10
snapshot, // Returns the current user object snapshot
_10
resolveArgument // Resolves the current user as a transaction argument
_10
}

Import

You can import the entire package and access the function:


_10
import * as fcl from "@onflow/fcl"
_10
_10
fcl.currentUser()

Or import directly the specific function:


_10
import { currentUser } from "@onflow/fcl"
_10
_10
currentUser()

Usage


_36
// Basic authentication flow
_36
import * as fcl from "@onflow/fcl"
_36
_36
// Configure FCL
_36
fcl.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
_36
const user = await fcl.currentUser.authenticate()
_36
console.log("User authenticated:", user.addr)
_36
_36
// Check authentication status
_36
const currentUser = await fcl.currentUser.snapshot()
_36
if (currentUser.loggedIn) {
_36
console.log("User is logged in:", currentUser.addr)
_36
}
_36
_36
// Subscribe to authentication state changes
_36
import * as fcl from "@onflow/fcl"
_36
_36
const 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
_36
window.addEventListener("beforeunload", () => unsubscribe())

Returns


_10
export interface CurrentUserService extends CurrentUserServiceApi {
_10
(): CurrentUserServiceApi
_10
}

A CurrentUserService object


Rate this page