본문 바로가기

카카오 로그인 후 이전 페이지 리다이렉트하기

카테고리 없음 by 코나인 2025. 5. 27.
반응형

카카오 로그인 후 이전 페이지로 리다이렉트를 하려면 state에 redirect주소를 넣어서 처리한다.

 

login.js

  $("#kakao_btn").click(function () {

    const params = new URLSearchParams(window.location.search);
    const redirect = params.get('redirect');
    console.log(redirect);

    const state = encodeURIComponent(redirect);

    const kakaoAuthUrl =
      "https://kauth.kakao.com/oauth/authorize?" +
      "client_id=client_id" +
      "&redirect_uri=https://redirect_uri/kakao_callback.php" +
      "&response_type=code" +
      "&scope=profile_nickname,account_email,phone_number" +
      "&prompt=consent" +
      "&state=" + state;

    window.location.href = kakaoAuthUrl;

  });

 

 

kakao_callback.php

$redirect = isset($_GET['state']) ? urldecode($_GET['state']) : '/';

header("Location: $redirect");
반응형

댓글