Skip to main content

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:


_10
import * as fcl from "@onflow/fcl-core"
_10
_10
fcl.getCurrentUser(config)

Or import directly the specific function:


_10
import { getCurrentUser } from "@onflow/fcl-core"
_10
_10
getCurrentUser(config)

Usage


_61
// Basic setup and authentication
_61
import * as fcl from "@onflow/fcl"
_61
_61
// Configure FCL
_61
fcl.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
_61
const currentUser = fcl.getCurrentUser({
_61
platform: "web"
_61
})
_61
_61
// Authenticate user
_61
const user = await currentUser.authenticate()
_61
console.log("Authenticated user:", user.addr)
_61
_61
// Subscribe to authentication state changes
_61
const currentUser = fcl.getCurrentUser({ platform: "web" })
_61
_61
const 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
_61
window.addEventListener("beforeunload", () => unsubscribe())
_61
_61
// Sign transactions with user authorization
_61
const currentUser = fcl.getCurrentUser({ platform: "web" })
_61
_61
const 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
_61
const currentUser = fcl.getCurrentUser({ platform: "web" })
_61
_61
const message = Buffer.from("Hello, Flow!").toString("hex")
_61
const signatures = await currentUser.signUserMessage(message)
_61
_61
console.log("Message signatures:", signatures)

Parameters

config

  • Type:

_10
export interface CurrentUserConfig {
_10
platform: string
_10
discovery?: object | undefined
_10
getStorageProvider?: () => Promise<StorageProvider>
_10
}

  • Description: Configuration object for the current user service

Returns


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

Current user service object with authentication and authorization methods


Rate this page