登入
Types
type LoginRedirectWalletType = 'metamask' | 'qubic' | 'walletconnect';
type QubicSignInProvider = 'facebook' | 'google' | 'apple' | 'yahoo';
interface WalletUser {
method: ExtendedExternalProviderMethod;
address: string;
accessToken: string;
expiredAt: number;
provider: ExtendedExternalProvider | null; // followed provider api on https://docs.metamask.io/wallet/reference/provider-api/
qubicUser: QubicUser | null; // only Qubic Wallet User has this field
}
// when use loginWithRedirect the method will always be `redirect`
type ExtendedExternalProviderMethod = 'metamask' | 'qubic' | 'walletconnect' | 'custom' | 'redirect';
enum QubicUserProvider {
GOOGLE = 'GOOGLE',
FACEBOOK = 'FACEBOOK',
TWITTER = 'TWITTER',
APPLE = 'APPLE',
UNKNOWN = 'UNKNOWN',
}
export interface QubicUser {
provider: QubicUserProvider;
email: string;
}
使用 loginWithRedirect 登入
會將用戶引導至登入畫面,待登入完成再回到原網頁
// 1. will redirect page to choose all types of wallet
qubicConnect.loginWithRedirect()
// 2. sign in only with Qubic wallet
qubicConnect.loginWithRedirect({
walletType: 'qubic'
});
// 3. sign in only with qubic wallet, and can only use google
qubicConnect.loginWithRedirect({
walletType: 'qubic'
qubicSignInProvider: 'google'
})
// 4. sign in only with qubic metamask
// walletType: metamask has special flow
// if window.ethereum doesn't exist
// it will open https://metamask.app.link/dapp/$url
// in desktop, it will open metamask chrome extension page
// in mobile, if installed it will called metamask dapp browse
// in mobile, if not installed it will show google play or apple store page
qubicConnect.loginWithRedirect({
walletType: 'metamask',
});
使用 onAuthStateChanged 監聽登入狀態
// function onAuthStateChanged(callback: (result: WalletUser | null, error?: Error) => void): () => void;
qubicConnect.onAuthStateChanged((user, error) => {
if (error) {
console.log(error.message);
}
if (!user) {
console.log('user not logged in');
return;
}
console.log('user logged in');
console.log(user.address);
console.log(user.accessToken);
console.log(user.expiredAt);
});
Last updated