Kakao Login SDK v2 Migration Guide: Fixing In-App Browser Issues in Next.js

This post covers upgrading Kakao Login SDK from version 1 to version 2 because of issues in in-app browsers. It walks through implementing Kakao Login SDK v2 in Next.js and the problems I worked through during development.

What Was the Problem?

We started marketing on Instagram. Around the same time, reports appeared in our bug-report channel that Kakao Login was not working inside Instagram.

Logging in showed only a white screen,

and signing up failed to redirect back to our site.

Kakao Login screen in Instagram's in-app browser

This happened when entering through Instagram’s browser, and intermittently in other in-app browsers too.

Finding the Cause

It worked normally in Chrome and other web browsers, but failed in in-app browsers such as Instagram. I searched Kakao DevTalk, which has extensive information on Kakao SDK issues, for similar reports.

Posts about in-app browser issues found on Kakao DevTalk

As expected, there were many similar reports. Our Kakao Login also used a popup, and I confirmed the issue occurred because Instagram does not support popups.

The Solution

To keep the product sustainable and address the problem properly, we decided to migrate to SDK v2 using the approach Kakao recommends.

https://developers.kakao.com/docs/latest/ko/javascript/migration

Kakao Login offers an SDK approach and a REST API approach. We chose the Kakao SDK approach.

A major difference is that the Kakao SDK approach launches the Kakao app directly. The REST API approach could require additional server work, so we chose the Kakao SDK approach.

Implementation

Configuring the Script

I added the script for Kakao SDK v2 as provided in Kakao’s developer documentation.

Here, integrity contains information associated with the upgraded version.

<!-- Before -->
<script src="https://t1.kakaocdn.net/kakao_js_sdk/v1/kakao.min.js"></script>

<!-- After -->
<script src="https://t1.kakaocdn.net/kakao_js_sdk/${VERSION}/kakao.min.js"
  integrity="${INTEGRITY_VALUE}" crossorigin="anonymous"></script>

After making the change, however, I encountered a new build error.

The error was No sync scripts, and I learned it was a Next.js script issue. Following the Next.js recommendation, I switched to the Next/Script component and added beforeInteractive, resolving the Kakao SDK loading issue.

https://nextjs.org/docs/messages/no-sync-scripts

https://web.dev/articles/efficiently-load-third-party-javascript?hl=ko

  <Script
         src="https://t1.kakaocdn.net/kakao_js_sdk/${VERSION}/kakao.min.js"
  integrity="${INTEGRITY_VALUE}" crossorigin="anonymous"
          strategy="beforeInteractive"
        />

After confirming that SDK v2 was integrated correctly, I discussed the flow in detail with the backend developer.

Login Flow

Kakao Login SDK v2 flow diagram

1. authorize

When authorize runs, it starts the simple login flow and sends redirectUri, as shown in Kakao’s demo code.

   // Demo code
    Kakao.Auth.authorize({
      redirectUri: 'https://developers.kakao.com/tool/demo/oauth',
    });

2. Authorization Code

Step 5: the authorization code is sent to the URL specified in redirectUri.

  • This URL must be registered with Kakao.

Enter the URI configured under [My Applications > Kakao Login > Redirect URI]. Once Kakao login completes and an authorization code is generated, the user is taken to the redirect URL like this:

https://developers.kakao.com/tool/demo/oauth?code={인가코드}

We considered having redirectUri point to the client, which would then send the code to the server to log in,

or having it point to the server, which would complete the entire login process and then redirect to the client.

Kakao recommends the latter approach. (See the image.)

That flow is more efficient and secure.

3. Redirecting to the Client

After the server completes the login logic, it needs to redirect to the client. Because that client redirect URL can change dynamically, we decided to use the state parameter in authorize. I agreed with the backend developer that we would use state for the redirect. Using state allows us to receive dynamic query values. (The redirect URI itself must be registered statically with Kakao.)

    Kakao.Auth.authorize({
      redirectUri: 'https://developers.kakao.com/tool/demo/oauth',
      state: clientRedirectUrl 
    });

4. Sending the Token to That URL

Send it in an HttpOnly cookie.

This part involved the most trial and error. I think it was because I had started without checking the exact cookie policies.

https://velog.io/@hjch0211/Apis-redirect%EC%97%90-cookie%EB%A5%BC-%EB%8B%B4%EC%95%84%EB%B3%B4%EC%95%84%EC%9A%94-%EC%8B%A4%ED%97%98

The situation in this article matched ours exactly, so it was a great help.

With the cookie sent by the server, the login flow was complete.