@wepin/login-js
v0.0.35
Published
Wepin Login Library V1 for Web
Readme
@wepin/login-js
Wepin Login Library V1 from Web. This package is exclusively available for use in web environments.
⚠️ Notice:
This package is only available for web environments and cannot be used in Android or iOS hybrid apps (Webview).
If you are using this package in a Server Side Rendering (SSR) environment, make sure to load the package only on the Client Side Rendering (CSR) side.
Please refer to the following code for implementation:
const initWepinLogin = async () => { const { WepinLogin } = await import('@wepin/login-js'); const wepinLogin = new WepinLogin({ appKey: '', appId: '', }); await wepinLogin.init(); }
⏩ Document
⏩ Get App ID and Key
After signing up for Wepin Workspace, go to the development tools menu and enter the information for each app platform to receive your App ID and App Key.
⏩ Install
To install the Wepin Login Library, you can use npm, yarn, or a CDN: Using npm:
npm install @wepin/login-jsUsing yarn:
yarn add @wepin/login-jsUsing CDN: You can also include the library directly via CDN by adding the following script tag to your HTML file:
<script src="https://cdn.jsdelivr.net/npm/@wepin/login-js/dist/umd/wepin-login.umd.js"></script>⏩ Import SDK
Using npm or yarn:
import { WepinLogin } from '@wepin/login-js'Using CDN:
const { WepinLogin } = window.WepinLogin⏩ Initialize
const wepinLogin = new WepinLogin({
appId: 'wepinAppId',
appKey: 'wepinAppKey',
})init
await wepinLogin.init(language?)Parameters
language<string> optional- The language to be displayed on the widget (default:
'ko') Currently, only'ko','en'and'ja'are supported.
- The language to be displayed on the widget (default:
Example
await wepinLogin.init('ko')isInitialized
wepinLogin.isInitialized()The isInitialized() method checks Wepin Login Library is initialized.
Returns
- <boolean>
- true if Wepin Login Library is already initialized.
changeLanguage
wepinLogin.changeLanguage(language)Parameters
language<string>- The language to be displayed on the widget.
Currently, only
'ko','en'and'ja'are supported.
- The language to be displayed on the widget.
Currently, only
Returns
- void
Example
wepinLogin.changeLanguage('ko')⏩ Method
Methods can be used after initialization of Wepin Login Library.
loginWithOauthProvider
await wepinLogin.loginWithOauthProvider(params)A new window will open and proceed to log in to wepin firebase. Returns firebase login info upon successful login.
Parameters
params<object>provider<string> - Provider for Firebase login. The value must be one of the supported login provider names in lowercase, such as 'google', 'naver', 'discord', 'apple', 'facebook', or 'line'. Please refer to Wepin Social Login Auth Provider documentation to check the supported login providers.withLogout<boolean> optional
Returns
- Promise<LoginResult|LoginErrorResult>
- <LoginResult>
provider<string>token<object>idToken<string> - wepin firebase idTokenrefreshToken````` - wepin firebase refreshToken
- <LoginErrorResult>
error<string> - error messageidToken<string> optional - id token valueaccessToken<string> optional - accessToken token valueprovider<string> optional - Provider that issued the access token
- <LoginResult>
Exception
Invalid provider: Incorrect value of provider parameterUser canceled: When the user closes the window during the login process.Internal error: In others error situations.
Example
const user = await wepinLogin.loginWithOauthProvider(true)- response
- LoginResult
{ "provider": "google", "token": { "idToken": "ab2231df....ad0f3291", "refreshToken": "eyJHGciO....adQssw5c", } } - LoginErrorResult
{ "error": "required/register_email", "provider": "naver", "accessToken": "eyJHGciO....adQssw5c", }
- LoginResult
signUpWithEmailAndPassword
await wepinLogin.signUpWithEmailAndPassword(email, password, openWepinWallet?)It signs up on the wepin firebase with your email and password. Returns firebase login info upon successful login.
Parameters
email<string> - User emailpassword<string> - User passwordopenWepinWallet<boolean> - optional Whether to open the authentication email sending page of the Wepin Wallet.
Returns
- Promise<LoginResult>
provider<'email'>token<object>idToken<string> - wepin firebase idTokenrefreshToken````` - wepin firebase refreshToken
Exception
auth/email-verified: An authentication email has been sent for registration, and email verification is required.auth/existed-email: The email is already registered.fail/send-email: Failed to send authentication email.fail/email-verified: Email verification failed.
Example
const user = await wepinLogin.signUpWithEmailAndPassword('[email protected]', 'abcdef123&')- response
{
"provider": "email",
"token": {
"idToken": "ab2231df....ad0f3291",
"refreshToken": "eyJHGciO....adQssw5c",
}
}loginWithEmailAndPassword
await wepinLogin.loginWithEmailAndPassword(email, password)It logs in to the Wepin firebase with your email and password. Returns firebase login info upon successful login.
Parameters
email<string> - User emailpassword<string> - User password
Returns
- Promise<LoginResult>
provider<'email'>token<object>idToken<string> - wepin firebase idTokenrefreshToken````` - wepin firebase refreshToken
Example
const user = await wepinLogin.loginWithEmailAndPassword('[email protected]', 'abcdef123&')- response
{
"provider": "email",
"token": {
"idToken": "ab2231df....ad0f3291",
"refreshToken": "eyJHGciO....adQssw5c",
}
}loginWithIdToken
await wepinLogin.loginWithIdToken(params)It logs in to the Wepin firebase with external id token. Returns firebase login info upon successful login.
Parameters
params<object>token<string> - id token value to be used for loginsign<string> - optional signature value for the token provided as the first parameter.(Signature Generation Methods)
[!NOTE] Starting from @wepin/login-js version
0.0.29, the sign value is optional.If you choose to remove the authentication key issued from the Wepin Workspace, 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
Returns
- Promise<LoginResult|LoginErrorResult>
- <LoginResult>
provider<'external_token'>token<object>idToken<string> - wepin firebase idTokenrefreshToken````` - wepin firebase refreshToken
- <LoginErrorResult>
error<string> - error messageidToken<string> optional - id token value
- <LoginResult>
Example
const user = await wepinLogin.loginWithIdToken({
token:'eyJHGciO....adQssw5c',
sign:'9753d4dc...c63466b9'
})- response
- LoginResult
{ "provider": "external_token", "token": { "idToken": "ab2231df....ad0f3291", "refreshToken": "eyJHGciO....adQssw5c", } } - LoginErrorResult
{ "error": "required/register_email", "idToken": "eyJHGciO....adQssw5c", }
- LoginResult
loginWithAccessToken
await wepinLogin.loginWithAccessToken(params)It logs in to the Wepin firebase with external access token. Returns firebase login info upon successful login.
Parameters
params<object>provider<string> - Provider that issued the access token. The value must be one of the supported login provider names in lowercase, such as 'naver', 'discord', 'facebook'. Please refer to Wepin Simplified Login documentation to check the supported login providers.token<string> - access token value to be used for loginsign<string> - optional signature value for the token provided as the first parameter.(Signature Generation Methods)
[!NOTE] Starting from @wepin/login-js version
0.0.29, the sign value is optional.If you choose to remove the authentication key issued from the Wepin Workspace, 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
Returns
- Promise<LoginResult|LoginErrorResult>
- <LoginResult>
provider<'external_token'>token<object>idToken<string> - wepin firebase idTokenrefreshToken````` - wepin firebase refreshToken
- <LoginErrorResult>
error<string> - error messageaccessToken<string> optional - accessToken token valueprovider<string> optional - Provider that issued the access token
- <LoginResult>
Example
const user = await wepinLogin.loginWithAccessToken({
provider: 'naver',
token:'eyJHGciO....adQssw5c',
sign:'9753d4dc...c63466b9'
})- response
- LoginResult
{ "provider": "external_token", "token": { "idToken": "ab2231df....ad0f3291", "refreshToken": "eyJHGciO....adQssw5c", } } - LoginErrorResult
{ "error": "required/register_email", "provider": "naver", "accessToken": "eyJHGciO....adQssw5c", }
- LoginResult
sendVerifyEmail
await wepinLogin.sendVerifyEmail(params)Method for registering an email and requesting email verification.
If a required/register_email error occurs, you need to register an email and request email verification.
Once email verification is complete, you should use the loginWithAccessToken or loginWithIdToken method to log in again with the AccessToken or IdToken used for the initial login.
Parameters
params<ISendVerifyEmailParams>email<string>provider<string> - Provider for Firebase login. The value must be one of the supported login provider names in lowercase, such as 'google', 'naver', 'discord', 'apple', 'facebook', or 'line'. Please refer to Wepin Social Login Auth Provider documentation to check the supported login providers.idToken<string> - id token value to be used for loginaccessToken<string> - access token value to be used for login
Returns
- Promise<boolean>
Example
const res = await wepinLogin.sendVerifyEmail({
email:'[email protected]'
provider: 'naver',
accessToken:'eyJHGciO....adQssw5c',
})getRefreshFirebaseToken
await wepinLogin.getRefreshFirebaseToken(prevFBToken?)This method retrieves the current firebase token's information from the Wepin.
[!NOTE]
Starting from @wepin/login-js version
0.0.33, theprevFBTokenparameter has been added.
Parameters
prevFBToken<LoginResult>optional (since v0.0.33)provider<'email'|'apple'|'google'|'discord'|'naver'|'external_token'> - The login providertoken<object> - The login tokens from previous loginidToken<string> - wepin firebase idTokenrefreshToken<string> - wepin firebase refreshToken
Returns
- Promise<LoginResult>
provider<'external_token'>token<object>idToken<string> - wepin firebase idTokenrefreshToken````` - wepin firebase refreshToken
Example
// 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)- response
{
"provider": "external_token",
"token": {
"idToken": "ab2231df....ad0f3291",
"refreshToken": "eyJHGciO....adQssw5c",
}
}getSignForLogin
Generates signatures to verify the issuer. It is mainly used to generate signatures for login-related information such as ID Tokens and Access Tokens.
import {getSignForLogin} from '@wepin/login-js'
const result = getSignForLogin(privKey, message);Parameters
privKey<string> - The authentication key used for signature generation.message<string> - The message or payload to be signed.
Returns
- string - The generated signature.
‼️ Caution ‼️
The authentication key (
privKey) must be stored securely and must not be exposed to the outside. It is recommended to execute thegetSignForLogin()method on the backend rather than the frontend for enhanced security and protection of sensitive information.
Example
const privKey = '0400112233445566778899001122334455667788990011223344556677889900'
const idToken = 'idtokenabcdef'
const sign = getSignForLogin(privKey, idToken)
const res = await wepinLogin.loginWithIdToken({
token: idToken,
sign
})loginWepin
await wepinLogin.loginWepin({provider, token})This method logs the user into the Wepin application using the specified provider and token.
Parameters
The parameters should utilize the return values from the loginWithOauthProvider(), loginWithEmailAndPassword(), loginWithIdToken(), and loginWithAccessToken() methods within the this module.
provider<'google'|'apple'|'naver'|'discord'|'external_token'|'email'> - The login provider.token<{idToken: string; refreshToken: string}> - The login tokens.
Returns
- Promise<IWepinUser> - A promise that resolves to an object containing the user's login status and information. The object includes:
- 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 status of wepin login. including:
- loginStatus: <'complete' | 'pinRequired' | 'registerRequired'> - If the user's loginStatus value is not complete, it must be registered in the wepin.
- pinRequired?:
<boolean>
- token: <object> - The user's token of wepin.
- accessToken: <string>
- refreshToken <string>
Example
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
{
"status": "success",
"userInfo": {
"userId": "120349034824234234",
"email": "[email protected]",
"provider": "google",
"use2FA": true,
},
"walletId": "abcdsfsf123",
"userStatus": {
"loginRequired": "completed",
"pinRequired": false,
},
"token": {
"accessToken": "",
"refreshToken": "",
}
}getCurrentWepinUser
await wepinLogin.getCurrentWepinUser()This method retrieves the current logged-in user's information from the Wepin.
Parameters
- void
Returns
- Promise<IWepinUser> - A promise that resolves to an object containing the user's login status and information. The object includes:
- 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 status of wepin login. including:
- loginStatus: <'complete' | 'pinRequired' | 'registerRequired'> - If the user's loginStatus value is not complete, it must be registered in the wepin.
- pinRequired?:
<boolean>
- token: <object> - The user's token of wepin.
- accessToken: <string>
- refreshToken <string>
Example
const userInfo = await wepinLogin.getCurrentWepinUser()
const userStatus = userInfo.userStatus
if(userStatus.loginStatus === 'pinRequired'||userStatus.loginStatus === 'registerRequired') {
// wepin register
}- response
{
"status": "success",
"userInfo": {
"userId": "120349034824234234",
"email": "[email protected]",
"provider": "google",
"use2FA": true,
},
"walletId": "abcdsfsf123",
"userStatus": {
"loginRequired": "completed",
"pinRequired": false,
},
"token": {
"accessToken": "",
"refreshToken": "",
}
}logout
await wepinLogin.logout()The logout() method logs out the user logged into Wepin.
Parameters
- void
Returns
- Promise<boolean>
- true if success
Exception
Wepin login module Not initialized: If the Wepin login library is not initializedAlready logout: If the user is already logged out
Example
const result = await wepinLogin.logout()finalize
wepinLogin.finalize()The finalize() method finalizes the Wepin Login Library.
Parameters
- void
Returns
- void
Example
wepinLogin.finalize()