Skip to content

app6460/kakao-web

Repository files navigation

npm version License

KakaoWeb

Kakao web auth api client
카카오 웹 로그인 구현

Install

npm i --save kakao-web

Usage

Login

import { AuthApiClient } from 'kakao-web';

async function main() {
    const apiClient = await AuthApiClient.create({
        serviceURL: 'https://example.com',
    });

    const res = await apiClient.login({
        email: 'email@example.com',
        password: 'password',
    });

    console.log(res);
}

main();

Login with two-step verification

import { AuthApiClient, LoginStatus } from 'kakao-web';

async function main() {
    const apiClient = await AuthApiClient.create({
        serviceURL: 'https://example.com',
    });

    const res = await apiClient.login({
        email: 'email@example.com',
        password: 'password',
    });

    if (res.response.status === LoginStatus.TWO_FACTOR) {
        console.log('need two-step verification');
        
        const { token, expireSeconds } = await apiClient.sendTwoFactor('tms');
        console.log('token: ', token);

        const loop = setInterval(async () => {
            const { response, cookieJar } = await apiClient.verifyTwoFactor('tms', {
                token,
            });

            if (response.status === LoginStatus.SUCCESS) {
                console.log('success!');
                console.log(cookieJar);

                process.exit(0);
            } else if (response.status !== LoginStatus.TWO_FACTOR) {
                console.error(`error with status ${response.status}`);

                process.exit(1);
            }
        }, 1000);

        setTimeout(() => {
            clearInterval(loop);
            console.log('timeout!');

            process.exit(1);
        }, expireSeconds * 1000);
    } else if (res.response.status === LoginStatus.SUCCESS) {
        console.log(res);
    }
}

main()