# Methods

## loginWithOauthProvider

```typescript
await wepinLogin.loginWithOauthProvider(params);
```

An inappbrowser will open and proceed to log in to OAuth provider. Returns OAuth login info upon successful login.

#### **Parameters**

* **params** `<object>`
  * **provider** `<'google'|'naver'|'discord'|'apple'>`

    OAuth Login Provider
  * **clientId** `<string>`

    client id of OAuth Login Provider

#### **Returns**

* **Promise\<LoginOauthResult>**
  * **provider** `<'google'|'naver'|'discord'|'apple'>`

    The name of the OAuth provider used
  * **token** `<string>`

    The accessToken (if provider is "naver" or "discord") or idToken (if provider is "google" or "apple")
  * **type** `<'id_token'|'access_token'>`

    The type of OAuth token (e.g., idToken, accessToken)

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const user = await wepinLogin.loginWithOauthProvider({
  provider: "google",
  clientId: "your-google-client-id"
});
```

***

## signUpWithEmailAndPassword

```typescript
await wepinLogin.signUpWithEmailAndPassword(email, password, locale?)
```

Sign up for Wepin Firebase with an email and password. For users who have not yet registered, a verification email will be sent, and a `REQUIRED_EMAIL_VERIFIED` error will occur if verification is required. If the user is already registered, an `EXISTED_EMAIL` error will occur, and you should proceed with the login process by calling [loginWithEmailAndPassword](#loginwithemailandpassword). Upon successful login, Firebase login information is returned.

#### **Parameters**

* **email** `<string>`

  User email
* **password** `<string>`

  User password
* **locale** `<string>` ***optional***

  Language for the verification email (default value: "en")

#### **Returns**

* **Promise\<LoginResult>**
  * **provider** `<string>`

    The provider used for the login (in this case, 'email')
  * **token** `<object>`
    * **idToken** `<string>`

      The Wepin Firebase ID token
    * **refreshToken** `<string>`

      The Wepin Firebase refresh token

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const user = await wepinLogin.signUpWithEmailAndPassword(
  'abc@defg.com', 
  'abcdef123&'
);
```

***

## loginWithEmailAndPassword

```typescript
await wepinLogin.loginWithEmailAndPassword(email, password);
```

Logs in to Wepin Firebase using an email and password. If the login is successful, it returns Firebase login information.

#### **Parameters**

* **email** `<string>`\
  User email
* **password** `<string>`\
  User password

#### **Returns**

* **Promise\<LoginResult>**
  * **provider** `<'email'>`

    The provider used for the login (in this case, 'email')
  * **token** `<object>`
    * **idToken** `<string>`

      The Wepin Firebase ID token
    * **refreshToken** `<string>`

      The Wepin Firebase refresh token

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const user = await wepinLogin.loginWithEmailAndPassword(
  'abc@defg.com', 
  'abcdef123&'
);
```

***

## loginWithIdToken

```typescript
await wepinLogin.loginWithIdToken(params);
```

Logs in to Wepin Firebase using an external ID token. If the login is successful, it returns Firebase login information.

#### **Parameters**

* **params**`<object>`
  * **idToken** `<string>`\
    ID token value to be used for login
  * **sign** `<string>` **optional** \
    Signature value for the token provided as the first parameter (returned value of [`getSignForLogin()`](#getsignforlogin))

{% hint style="warning" %}
&#x20;Note

Starting from WepinLogin version 1.0.0, the sign value is optional.

If you choose to remove the authentication key issued from the [Wepin Workspace](https://workspace.wepin.io/), you may opt not to use the `sign` value.

(Wepin Workspace > Development Tools menu > Login tab > Auth Key > Delete)

> The Auth Key menu is visible only if an authentication key was previously generated.
> {% endhint %}

#### **Returns**

* **Promise\<LoginResult>**
  * **provider** `<'external_token'>` \
    The provider used for the login (in this case, 'external\_token').
  * **token** `<object>`
    * **idToken** `<string>`

      The Wepin Firebase ID token
    * **refreshToken** `<string>`

      The Wepin Firebase refresh token

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const user = await wepinLogin.loginWithIdToken({
    idToken:'eyJHGciO....adQssw5c', 
    sign:'9753d4dc...c63466b9'
});
```

***

## loginWithAccessToken

```typescript
await wepinLogin.loginWithAccessToken(params);
```

Logs in to Wepin Firebase using an external access token. If the login is successful, it returns Firebase login information

#### **Parameters**

* **params**`<object>`
  * **provider** `<"naver"|"discord">`

    Provider that issued the access token
  * **accessToken** `<string>`

    Access token value to be used for login
  * **sign** `<string>` **optional**&#x20;

    Signature value for the Access token (returned value of [`getSignForLogin()`](#getsignforlogin))

{% hint style="warning" %}
&#x20;Note

Starting from WepinLogin version 1.0.0, the sign value is optional.

If you choose to remove the authentication key issued from the [Wepin Workspace](https://workspace.wepin.io/), you may opt not to use the `sign` value.

(Wepin Workspace > Development Tools menu > Login tab > Auth Key > Delete)

> The Auth Key menu is visible only if an authentication key was previously generated.
> {% endhint %}

#### **Returns**

* **Promise\<LoginResult>**
  * **provider** `<string>`

    The provider used for the login (in this case, 'external\_token')
  * **token** `<object>`
    * **idToken** `<string>`

      The Wepin Firebase ID token
    * **refreshToken** `<string>`

      The Wepin Firebase refresh token

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const user = await wepinLogin.loginWithAccessToken({
  provider: 'naver',
  token: 'eyJHGciO....adQssw5c',
  sign: '9753d4dc...c63466b9',
});
```

***

## getRefreshFirebaseToken

```typescript
await wepinLogin.getRefreshFirebaseToken();
```

Retrieves information on the current Wepin Firebase token.

#### **Parameters**

* None

#### **Returns**

* **Promise\<LoginResult>**
  * **provider** `<string>`\
    The provider used for the login
    * `<'google'|'apple'|'naver'|'discord'|'email'|'external_token'>`
  * **token** `<object>`
    * **idToken** `<string>`\
      The Wepin Firebase ID token
    * **refreshToken** `<string>`\
      The Wepin Firebase refresh token

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const user = await wepinLogin.getRefreshFirebaseToken();
```

***

## loginWepin

```typescript
await wepinLogin.loginWepin({ provider, token });
```

Logs the user into Wepin using the Wepin Firebase token.

#### **Parameters**

* **params** `<LoginResult>`\
  Parameters from the return value of methods like [`loginWithEmailAndPassword()`](#loginwithemailandpassword), [`loginWithIdToken()`](#loginwithidtoken), or [`loginWithAccessToken()`](#loginwithaccesstoken)

#### **Returns**

* **Promise\<IWepinUser>**&#x20;
  * **status** `<'success'|'fail'>`

    The login status.
  * **userInfo** `<object>`***optional***  - The user's information, including:
    * **userId** `<string>`

      The user's ID
    * **email** `<string>`

      The user's email
    * **provider** `<'google'|'apple'|'naver'|'discord'|'email'|'external_token'>`

      The login provider
    * **use2FA** `<boolean>`

      Whether the user uses two-factor authentication
  * **walletId** `<string>`

    The user's wallet ID
  * **userStatus** `<object>` - The user's Wepin login status, including:
    * **loginStats** `<'complete' | 'pinRequired' | 'registerRequired'>`

      If the user's `loginStatus` value is not 'complete', the user must register with Wepin.
    * **pinRequired** `<boolean>`***optional***&#x20;

      Whether a PIN is required
  * **token** `<object>`

    The user's Wepin token

    * **accessToken** `<string>`\
      The access token
    * **refreshToken** `<string>`\
      The refresh token

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const wepinLogin = WepinLogin({ appId: 'appId', appKey: 'appKey' });
const res = await wepinLogin.loginWithOauthProvider({
  provider: 'google',
  clientId: 'google_client_id'
});

const userInfo = await wepinLogin.loginWepin(res);
const userStatus = userInfo.userStatus;
if (
  userStatus.loginStatus === 'pinRequired' ||
  userStatus.loginStatus === 'registerRequired'
) {
  // wepin register
}
```

***

## getCurrentWepinUser

```typescript
await wepinLogin.getCurrentWepinUser();
```

Retrieves information about the currently logged-in user in Wepin.

#### **Parameters**

* None

#### **Returns**

* **Promise\<IWepinUser>**&#x20;
  * **status** `<'success'|'fail'>`

    The login status
  * **userInfo** `<object>`***optional***  - The user's information, including
    * **userId** `<string>`

      The user's ID
    * **email** `<string>`

      The user's email
    * **provider** `<'google'|'apple'|'naver'|'discord'|'email'|'external_token'>`

      The login provider
    * **use2FA** `<boolean>`

      Whether the user uses two-factor authentication
  * **walletId** `<string>`

    The user's wallet ID
  * **userStatus** `<object>` -The user's Wepin login status, including:
    * **loginStats** `<'complete' | 'pinRequired' | 'registerRequired'>`

      If the user's `loginStatus` value is not 'complete', the user must register with Wepin
    * **pinRequired** `<boolean>`***optional***&#x20;

      Whether a PIN is required
  * **token** `<object>`

    The user's Wepin token

    * **accessToken** `<string>`\
      The access token
    * **refreshToken** `<string>`\
      The refresh token

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const userInfo = await wepinLogin.getCurrentWepinUser();
const userStatus = userInfo.userStatus;
if (
  userStatus.loginStatus === 'pinRequired' ||
  userStatus.loginStatus === 'registerRequired'
) {
  // wepin register
}
```

***

## logout

```typescript
await wepinLogin.logout();
```

Logs out the currently logged-in user from Wepin.

#### **Parameters**

* None

#### **Returns**

* **Promise\<boolean>**

#### **Exception**

* [WepinLoginException](#wepinloginexception)

#### **Example**

```typescript
const result = await wepinLogin.logout();
if (result) {
  // Successfully logged out
}
```

***

## getSignForLogin

Generates a signature for verifying the issuer. This method is mainly used to generate signatures for login-related information such as ID tokens and access tokens.

```typescript
import { getSignForLogin } from '@wepin/login-rn';
const result = getSignForLogin(privKey, message);
```

#### **Parameters**

* **privateKey** `<string>`\
  The authentication key used for signature generation
* **message** `<string>`\
  The message or payload to be signed

{% hint style="info" %}
The key for signing can be obtained from [Wepin Workspace](https://workspace.wepin.io/). In the Development Tools menu, click **Get your authentication key** on the Login tab to retrieve the authentication key.
{% endhint %}

#### **Returns**

* **\<string>** \
  The generated signature

{% hint style="warning" %}
The private key (**privateKey**) must be securely stored and should not be exposed externally. To enhance security and protect sensitive information, it is recommended to execute the `getSignForLogin()` method on the backend rather than the frontend. For details on how to generate signatures, please refer to the following document:

* [Signature Generation Methods](https://github.com/WepinWallet/wepin-web-sdk-v1/blob/main/packages/login/SignatureGenerationMethods.md)
  {% endhint %}

#### **Example**

```typescript
const sign = getSignForLogin(
  '0400112233445566778899001122334455667788990011223344556677889900', 
  'idtokenabcdef'
);

const res = await wepinLogin.loginWithIdToken(
    'eyJHGciO....adQssw5c', 
    sign
);
```

***

## finalize

```typescript
await wepinLogin.finalize();
```

Terminates the Wepin Login Library.

#### **Parameters**

* None

#### **Returns**

* **Promise\<void>**

#### **Example**

```typescript
await wepinLogin.finalize();
```

***

## WepinLoginException

<table><thead><tr><th width="297">Error Code</th><th>Error Message</th><th>Error Description</th></tr></thead><tbody><tr><td><code>INVALID_APP_KEY</code></td><td>"InvalidAppKey"</td><td>The Wepin app key is invalid.</td></tr><tr><td><code>INVALID_PARAMETER</code></td><td>"InvalidParameters"</td><td>One or more parameters provided are invalid or missing.</td></tr><tr><td><code>INVALID_LOGIN_PROVIDER</code></td><td>"InvalidLoginProvider"</td><td>The login provider specified is not supported or is invalid.</td></tr><tr><td><code>INVALID_TOKEN</code></td><td>"InvalidToken"</td><td>The token does not exist.</td></tr><tr><td><code>INVALID_LOGIN_SESSION</code></td><td>"InvalidLoginSession"</td><td>The login session information does not exist.</td></tr><tr><td><code>NOT_INITIALIZED_ERROR</code></td><td>"NotInitialized"</td><td>The WepinLoginLibrary has not been properly initialized.</td></tr><tr><td><code>ALREADY_INITIALIZED_ERROR</code></td><td>"AlreadyInitialized"</td><td>The WepinLoginLibrary is already initialized, so the logout operation cannot be performed again.</td></tr><tr><td><code>USER_CANCLED</code></td><td>"UserCancelled"</td><td>The user has cancelled the operation.</td></tr><tr><td><code>UNKNOWN_ERROR</code></td><td>"UnknownError"</td><td>An unknown error has occurred, and the cause is not identified.</td></tr><tr><td><code>NOT_CONNECTED_INTERNET</code></td><td>"NotConnectedInternet"</td><td>The system is unable to detect an active internet connection.</td></tr><tr><td><code>FAILED_LOGIN</code></td><td>"FailedLogin"</td><td>The login attempt has failed due to incorrect credentials or other issues.</td></tr><tr><td><code>ALREADY_LOGOUT</code></td><td>"AlreadyLogout"</td><td>The user is already logged out, so the logout operation cannot be performed again.</td></tr><tr><td><code>INVALID_EMAIL_DOMAIN</code></td><td>"InvalidEmailDomain"</td><td>The provided email address's domain is not allowed or recognized by the system.</td></tr><tr><td><code>FAILED_SEND_EMAIL</code></td><td>"FailedSendEmail"</td><td>The system encountered an error while sending an email. This is because the email address is invalid or we sent verification emails too often. Please change your email or try again after 1 minute.</td></tr><tr><td><code>REQUIRED_EMAIL_VERIFIED</code></td><td>"RequiredEmailVerified"</td><td>Email verification is required to proceed with the requested operation.</td></tr><tr><td><code>INCORRECT_EMAIL_FORM</code></td><td>"incorrectEmailForm"</td><td>The provided email address does not match the expected format.</td></tr><tr><td><code>INCORRECT_PASSWORD_FORM</code></td><td>"IncorrectPasswordForm"</td><td>The provided password does not meet the required format or criteria.</td></tr><tr><td><code>NOT_INITIALIZED_NETWORK</code></td><td>"NotInitializedNetwork"</td><td>The network or connection required for the operation has not been properly initialized.</td></tr><tr><td><code>REQUIRED_SIGNUP_EMAIL</code></td><td>"RequiredSignupEmail"</td><td>The user needs to sign up with an email address to proceed.</td></tr><tr><td><code>FAILED_EMAIL_VERIFIED</code></td><td>"FailedEmailVerified"</td><td>The WepinLoginLibrary encountered an issue while attempting to verify the provided email address.</td></tr><tr><td><code>FAILED_PASSWORD_SETTING</code></td><td>"failedPasswordSetting"</td><td>Failed to set the password. This could be due to issues with the provided password or internal errors during the password setting process.</td></tr><tr><td><code>EXISTED_EMAIL</code></td><td>"ExistedEmail"</td><td>The provided email address is already registered. This error occurs when attempting to sign up with an email that is already in use.</td></tr></tbody></table>
