# 登入

## Types

```typescript
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 登入

會將用戶引導至登入畫面，待登入完成再回到原網頁

```typescript
// 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 監聽登入狀態

```typescript
// 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);
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qubic.gitbook.io/docs/guides/qubic-connect-sdk/authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
