> For the complete documentation index, see [llms.txt](https://docs.wepin.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wepin.io/en/widget-integration/web-javascript-sdk/login-library/methods.md).

# Methods

Methods can be used after initializing the Wepin Login Library.

## loginWithOauthProvider

```javascript
await wepinLogin.loginWithOauthProvider(params)
```

Logs in to Wepin Firebase in a new window. Returns Firebase login information upon successful login.

If an error of required/register\_email occurs, you will need to call the `sendVerifyEmail` method.

### **Parameters**

* `params` \<object>
  * `provider` <'google'|'naver'|'discord'|'apple'|'line'|'facebook'> - Provider for Firebase login
  * `withLogout` \<boolean> **optional**&#x20;
    * true : Does not log out if already logged in.
    * false  : Logs out and logs in again if already logged in.

### **Return value**

* `Promise` \<LoginResult| LoginErrorResult>
  * \<LoginResult>
    * `provider` <'google'|'naver'|'discord'|'apple'|'line'|'facebook'>
    * `token` \<object>
      * `idToken` \<string> \
        wepin firebase idToken
      * `refreshToken` \
        wepin firebase refreshToken
  * \<LoginErrorResult>
    * `error` \<string> \
      error message
    * `idToken` \<string> ***optional***\
      id token value
    * `accessToken` \<string> ***optional*** \
      accessToken token value
    * `provider` <'google'|'naver'|'discord'|'apple'|'line'|'facebook'> ***optional*** \
      Provider that issued the access token

### **Exception**

* `Invalid provider`:  If the provider parameter is incorrect
* `User canceled` : If the user closes the window during login
* `Internal error` : For other exceptions

### **Example**

```javascript
const user = await wepinLogin.loginWithOauthProvider(true)
```

* response
  * LoginResult

    ```json
    {
      "provider": "google",
      "token": {
          "idToken": "ab2231df....ad0f3291",
          "refreshToken": "eyJHGciO....adQssw5c",
      }
    }
    ```
  * LoginErrorResult

    ```json
    {
      "error": "required/register_email",
      "provider": "naver",
      "accessToken": "eyJHGciO....adQssw5c",
    }
    ```

## &#x20;signUpWithEmailAndPassWord

```javascript
await wepinLogin.signUpWithEmailAndPassword(email, password, openWepinWallet?)
```

Sign up for Wepin Firebase with an email and password. For users who have not yet registered, a verification email will be sent, and an `auth/email-verified` error will occur if verification is required. If the user is already registered, an `auth/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 address
* `password` \<string>

  User email password
* `openWepinWallet` \<boolean> **optional** \
  Whether to show the Wepin Wallet authentication email page

### **Return Value**

* `Promise`\<LoginResult>
  * `provider` <'email'>
  * `token` \<object>
    * `idToken` \<string>\
      wepin firebase idToken
    * `refreshToken` \
      wepin firebase refreshToken

### **Exception**

* `auth/email-verified`: A verification email has been sent, email verification is required
* `auth/existed-email` : The email is already registered
* `fail/send-email` : Failed to send verification email
* `fail/email-verified` : Email verification failed

### **Example**

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

* response

<pre class="language-json"><code class="lang-json"><strong>    {
</strong>        "provider": "email",
        "token": {
            "idToken": "ab2231df....ad0f3291",
            "refreshToken": "eyJHGciO....adQssw5c",
        }
    }
</code></pre>

## loginWithEmailAndPassWord

```javascript
await wepinLogin.loginWithEmailAndPassword(email, password)
```

Logs in to Wepin Firebase using email and password. Returns Firebase login information upon successful login.

### **Parameters**

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

  User email password

### **Return Value**

* `Promise`\<LoginResult>
  * `provider` <'email'>
  * `token` \<object>
    * `idToken` \<string>\
      wepin firebase idToken
    * `refreshToken` \
      wepin firebase refreshToken

### **Example**

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

* response

```json
    {
        "provider": "email",
        "token": {
            "idToken": "ab2231df....ad0f3291",
            "refreshToken": "eyJHGciO....adQssw5c",
        }
    }
```

## loginWithIdToken

```javascript
await wepinLogin.loginWithIdToken(params)
```

Logs in to Wepin Firebase using an external IdToken. Returns Firebase login information upon successful login.

If an error of `required/register_email` occurs, you will need to call the `sendVerifyEmail` method.

### **Parameters**

* `params` \<object>
  * `token` \<string> \
    External IdToken used for login
  * `sign` \<string>&#x20;

    Signature of the `token` provided as the first parameter ([Signature Generation Methods](https://github.com/WepinWallet/wepin-widget-js-sdk/blob/main/doc/SignatureGenerationMethods.md))<br>

    <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>Starting from <code>@wepin/login-js</code> version <mark style="color:red;"><strong>0.0.29</strong></mark>, the <code>sign</code>value is optional.<br><br>If you choose to remove the authentication key issued from the <a href="https://workspace.wepin.io/">Wepin Workspace</a>, you may opt not to use the <code>sign</code>value.<br>(Wepin Workspace > Development Tools menu > Login tab > Auth Key > Delete)</p><blockquote><p><mark style="background-color:yellow;">The Auth Key menu is visible only if an authentication key was previously generated.</mark></p></blockquote></div>

### **Return Value**

* `Promise`\<LoginResult | LoginErrorResult>
  * \<LoginResult>
    * `provider` <'external\_token'>
      * `token` \<object>
        * `idToken` \<string> \
          wepin firebase idToken
        * `refreshToken` \
          wepin firebase refreshToken
  * \<LoginErrorResult>
    * `error` \<string>\
      error message
      * `idToken` \<string> ***optional*** \
        id token value

### **Example**

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

* response
  * LoginResult

    ```json
    {
      "provider": "external_token",
      "token": {
          "idToken": "ab2231df....ad0f3291",
          "refreshToken": "eyJHGciO....adQssw5c",
      }
    }
    ```
  * LoginErrorResult

    ```json
    {
      "error": "required/register_email",
      "idToken": "eyJHGciO....adQssw5c",
    }
    ```

## loginWithAccessToken

```javascript
await wepinLogin.loginWithAccessToken(params)
```

Logs in to Wepin Firebase using an external Access Token. Returns Firebase login information upon successful login.

If an error of `required/register_email` occurs, you will need to call the `sendVerifyEmail` method.

### **Parameters**

* `params` \<object>
  * `provider` <'naver'|'discord'|'facebook'>\
    Login provider from which the Access Token was issued
  * `token` \<string> \
    External Access Token used for login
  * `sign` \<string>&#x20;

    Signature of the token provided as the second parameter ([Signature Generation Methods](https://github.com/WepinWallet/wepin-widget-js-sdk/blob/main/doc/SignatureGenerationMethods.md))<br>

    <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>Starting from <code>@wepin/login-js</code> version <mark style="color:red;"><strong>0.0.29</strong></mark>, the <code>sign</code>value is optional.<br><br>If you choose to remove the authentication key issued from the <a href="https://workspace.wepin.io/">Wepin Workspace</a>, you may opt not to use the <code>sign</code>value.<br>(Wepin Workspace > Development Tools menu > Login tab > Auth Key > Delete)</p><blockquote><p><mark style="background-color:yellow;">The Auth Key menu is visible only if an authentication key was previously generated.</mark></p></blockquote></div>

### **Return Value**

* `Promise`\<LoginResult | LoginErrorResult>
  * \<LoginResult>
    * `provider` <'external\_token'>
      * `token` \<object>
        * `idToken` \<string>  \
          wepin firebase idToken
        * `refreshToken` \
          &#x20;wepin firebase refreshToken
  * \<LoginErrorResult>
    * `error` \<string> \
      error message
    * `accessToken` \<string> ***optional***\
      accessToken token value
    * `provider` \<string> ***optional*** \
      Provider that issued the access token

### **Example**

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

* response
  * LoginResult

    ```json
    {
      "provider": "external_token",
      "token": {
          "idToken": "ab2231df....ad0f3291",
          "refreshToken": "eyJHGciO....adQssw5c",
      }
    }
    ```
  * LoginErrorResult

    ```json
    {
      "error": "required/register_email",
      "provider": "naver",
      "accessToken": "eyJHGciO....adQssw5c",
    }
    ```

## getSignForLogin

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

It generates a signature to verify the issuer. Mainly used to generate signatures for login-related information such as IdToken and Access Token.

### **Parameters**

* `privKey` \<string> \
  Authentication key used to generate the signature
* `message` \<string> \
  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 %}

### **Return Value**

* \<string>

  Signature value

{% hint style="danger" %}
The authentication key (privKey) should be stored securely and not exposed externally. To enhance security and protection of sensitive information, it is recommended to execute the `getSignForLogin()` method on the backend rather than the frontend.
{% endhint %}

### **Example**

```javascript
const privKey = '0400112233445566778899001122334455667788990011223344556677889900'
const idToken = 'idtokenabcdef'
const sign = getSignForLogin(privKey, idToken)

const res = await wepinLogin.loginWithIdToken({
    token: idToken, 
    sign
})
```

## getRefreshFirebaseToken

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

This method retrieves the current firebase token's information from the Wepin.

### **Parameters**

* `prevFBToken` \<LoginResult> ***optional***
  * `provider` <'google'|'naver'|'discord'|'apple'|'line'|'facebook'>
  * `token` \<object>
    * `idToken` \<string> \
      wepin firebase idToken
    * `refreshToken` \
      wepin firebase refreshToken

{% hint style="warning" %}
Starting from `@wepin/login-js` version <mark style="color:red;">**0.0.33**</mark>, the `prevFBToken`value is added.
{% endhint %}

### **Return Value**

* `Promise` \<LoginResult>
  * \<LoginResult>
    * `provider` <'google'|'naver'|'discord'|'apple'|'line'|'facebook'>
    * `token` \<object>
      * `idToken` \<string> \
        wepin firebase idToken
      * `refreshToken` \
        wepin firebase refreshToken

### **Example**

```javascript
// Without parameter (legacy way)
const result = await wepinLogin.getRefreshFirebaseToken()

// With prevFBToken parameter (since v0.0.33)
const prevToken = {
    provider: 'google',
    token: {
        idToken: 'previous_id_token',
        refreshToken: 'previous_refresh_token'
    }
}
const result = await wepinLogin.getRefreshFirebaseToken(prevToken)
```

## sendVerifyEmail

```javascript
await wepinLogin.sendVerifyEmail(params)
```

The `sendVerifyEmail` method allows you to register an email and request email verification. If an error of r`equired/register_email` occurs, you must register the email and request verification. Once verification is completed, use the `loginWithAccessToken` or `loginWithIdToken` method to log in again with the previously used AccessToken or IdToken.

### **Supported Version**

* Supported in version <mark style="color:orange;">**`0.0.18`**</mark> and later

### **Parameters**

* `params` \<ISendVerifyEmailParams>
  * `email` \<string>\
    The email address to be registered and verified
  * `provider` \<string> \
    The login provider for Firebase. It must be one of the supported providers in lowercase, such as ‘google’, ‘naver’, ‘discord’, ‘apple’, ‘facebook’, or ‘line’. Refer to [Wepin Social Login Auth Provider documentation](/en/login/social-login-auth-providers.md) for the full list of supported providers.
  * `idToken` \<string> \
    The id token to be used for login
  * `accessToken` \<string> \
    The access token to be used for login

### **Return Value**

* Promise\<boolean>

### **Example**

```javascript
const res = await wepinLogin.sendVerifyEmail({
    email:'test@abcde.com'
    provider: 'naver', 
    accessToken:'eyJHGciO....adQssw5c', 
})
```

## loginWepin

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

It logs the user into Wepin using the specified Login Provider and Token.

### **Parameters**

The parameters for this method should utilize the return values from the [<mark style="color:blue;">`loginWithOauthProvider()`</mark>](#loginwithoauthprovider), [<mark style="color:blue;">`loginWithEmailAndPassword()`</mark>](#loginwithemailandpassword), [<mark style="color:blue;">`loginWithIdToken()`</mark>](#loginwithidtoken), and [<mark style="color:blue;">`loginWithAccessToken()`</mark>](#loginwithaccesstoken) methods within this module.

* `provider` <'google'|'apple'|'naver'|'discord'|'external\_token'|'email'>\
  Login Provider
* `token` \<object>
  * `idToken` \<string>\
    wepin firebase idToken
  * `refreshToken` \
    wepin firebase refreshToken

### **Return Value**

* `Promise`\<IWepinUser>
  * `status` <'success'|'fail'>\
    Login result
  * `userInfo` \<object> **optional**\
    Information of the logged-in user
    * userId \<string>\
      User's Wepin ID
    * email \<string>\
      User's email address
    * provider<'google'|'apple'|'naver'|'discord'|'email'|'external\_token'>\
      Logged-in Provider
    * use2FA \<boolean>\
      Whether 2FA is enabled for the user
  * `userStatus` \<object>\
    Status information of the logged-in user
    * `loginStats`: <'complete' | 'pinRequired' | 'registerRequired'>\
      If not 'complete', registration with Wepin is required
    * `pinRequired` \<boolean> **optional**\
      Whether the user's PIN number is required
  * `token` \<object>\
    User's wepin token
    * `accessToken` \<string>\
      wepin accessToken
    * `refreshToken` \<string>\
      wepin refreshToken

### **Example**

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

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

* response

```json
{
    "status": "success",
    "userInfo": {
      "userId": "120349034824234234",
      "email": "abc@gmail.com",
      "provider": "google",
      "use2FA": true,
    },
    "walletId": "abcdsfsf123",
    "userStatus": {
        "loginRequired": "completed",
        "pinRequired": false,
    },
    "token": {
        "accessToken": "",
        "refreshToken": "",
    }
}
```

## logout

```javascript
await wepinLogin.logout()
```

It logs out the Wepin user.

### **Parameters**

* \<void>

### **Return Value**

* `Promise` \<boolean>

  true if successful

### **Exception**

* `Wepin login module Not initialized`: If the Wepin Login Library has not been initialized
* `Already logout` : If the user is already logged out

### **Example**

```javascript
const result = await wepinLogin.logout()
```

## finalize

```javascript
wepinLogin.finalize()
```

It terminates the use of the Wepin Login Library.

### **Parameters**

* \<void>

### **Return Value**

* \<void>

### **Example**

```javascript
wepinLogin.finalize()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.wepin.io/en/widget-integration/web-javascript-sdk/login-library/methods.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
