Skip to main content
TinyCloud

UserAuthorization

The UserAuthorization module handles authentication and authorization for the TinyCloud Web SDK.

import { UserAuthorization } from '@tinycloudlabs/web-sdk';

Description

UserAuthorization provides methods for connecting to Ethereum wallets, signing in with Ethereum (SIWE), and managing user sessions. It is a core component used by the TinyCloudWeb class.

Constructor

constructor(config: UserAuthorizationConfig)

Creates a new instance of the UserAuthorization class.

Parameters:

  • config - Configuration options for user authorization
    • siweConfig - Configuration for Sign-In with Ethereum (SIWE)
    • walletConnectProjectId - Optional WalletConnect project ID

Properties

NameTypeDescription
sessionTCWClientSession | undefinedThe current user session if signed in.
web3Providerethers.providers.Web3Provider | undefinedThe connected Ethereum provider.

Methods

connect

async connect(provider?: any): Promise<UserAuthorizationConnected>

Connects to a user's Ethereum wallet.

Parameters:

  • provider - Optional Web3 provider to use. If not provided, it will detect and use the available provider.

Returns: A Promise that resolves to a UserAuthorizationConnected object.

Example:

const auth = new UserAuthorization({
siweConfig: {
domain: 'example.com'
}
});

const connected = await auth.connect();
console.log('Connected to wallet:', connected.address);

signIn

async signIn(): Promise<TCWClientSession>

Signs the user in using Sign-In with Ethereum (SIWE).

Returns: A Promise that resolves to a TCWClientSession object containing the session information.

Example:

await auth.connect();
const session = await auth.signIn();
console.log('User signed in:', session.address);

signOut

async signOut(): Promise<void>

Signs the user out and clears the current session.

Example:

await auth.signOut();
console.log('User signed out');

isConnected

isConnected(): boolean

Checks if a user is connected to a wallet.

Returns: true if connected, false otherwise.

Example:

const connected = auth.isConnected();
console.log('Connected:', connected);

isSignedIn

isSignedIn(): boolean

Checks if a user is currently signed in.

Returns: true if signed in, false otherwise.

Example:

const signedIn = auth.isSignedIn();
console.log('Signed in:', signedIn);

getSigner

getSigner(): ethers.Signer

Gets the Ethereum signer for the connected wallet.

Returns: An ethers.js Signer object.

Example:

const signer = auth.getSigner();
const address = await signer.getAddress();
console.log('Wallet address:', address);

address

address(): string

Gets the Ethereum address of the connected wallet.

Returns: The Ethereum address as a string.

Example:

const userAddress = auth.address();
console.log('Wallet address:', userAddress);

chainId

chainId(): number

Gets the chain ID of the connected network.

Returns: The chain ID as a number.

Example:

const chain = auth.chainId();
console.log('Connected to chain ID:', chain);

signMessage

async signMessage(message: string): Promise<string>

Signs a message with the connected wallet.

Parameters:

  • message - The message to sign.

Returns: A Promise that resolves to the signature as a string.

Example:

const message = 'Hello, world!';
const signature = await auth.signMessage(message);
console.log('Signature:', signature);