getCurrentUser
Creates and configures the Current User service for managing user authentication and authorization in Flow applications. This is the core service for handling user sessions, wallet connections, transaction signing, and user data management. The service provides both callable function interface and object methods for maximum flexibility.
Import
You can import the entire package and access the function:
_10import * as fcl from "@onflow/fcl-core"_10_10fcl.getCurrentUser(config)
Or import directly the specific function:
_10import { getCurrentUser } from "@onflow/fcl-core"_10_10getCurrentUser(config)
Usage
_61// Basic setup and authentication_61import * as fcl from "@onflow/fcl"_61_61// Configure FCL_61fcl.config({_61 "accessNode.api": "https://rest-testnet.onflow.org",_61 "discovery.wallet": "https://fcl-discovery.onflow.org/testnet/authn"_61})_61_61// Create current user service_61const currentUser = fcl.getCurrentUser({_61 platform: "web"_61})_61_61// Authenticate user_61const user = await currentUser.authenticate()_61console.log("Authenticated user:", user.addr)_61_61// Subscribe to authentication state changes_61const currentUser = fcl.getCurrentUser({ platform: "web" })_61_61const unsubscribe = currentUser.subscribe((user) => {_61 if (user.loggedIn) {_61 console.log("User logged in:", user.addr)_61 document.getElementById("login-btn").style.display = "none"_61 document.getElementById("logout-btn").style.display = "block"_61 } else {_61 console.log("User logged out")_61 document.getElementById("login-btn").style.display = "block"_61 document.getElementById("logout-btn").style.display = "none"_61 }_61})_61_61// Clean up subscription_61window.addEventListener("beforeunload", () => unsubscribe())_61_61// Sign transactions with user authorization_61const currentUser = fcl.getCurrentUser({ platform: "web" })_61_61const txId = await fcl.mutate({_61 cadence: `_61 transaction(amount: UFix64, to: Address) {_61 prepare(signer: AuthAccount) {_61 // Transfer tokens logic here_61 }_61 }_61 `,_61 args: (arg, t) => [_61 arg("10.0", t.UFix64),_61 arg("0x01", t.Address)_61 ],_61 authz: currentUser.authorization_61})_61_61// Sign custom messages_61const currentUser = fcl.getCurrentUser({ platform: "web" })_61_61const message = Buffer.from("Hello, Flow!").toString("hex")_61const signatures = await currentUser.signUserMessage(message)_61_61console.log("Message signatures:", signatures)
Parameters
config
- Type:
_10export interface CurrentUserConfig {_10 platform: string_10 discovery?: object | undefined_10 getStorageProvider?: () => Promise<StorageProvider>_10}
- Description: Configuration object for the current user service
Returns
_10export interface CurrentUserService extends CurrentUserServiceApi {_10 (): CurrentUserServiceApi_10}
Current user service object with authentication and authorization methods