IT 알쓸신잡

그누보드에서 소셜 로그인 연동 해제 [카카오] 본문

Development

그누보드에서 소셜 로그인 연동 해제 [카카오]

솦트웰러 2023. 3. 8. 16:47
728x90
반응형

그누보드에서 소셜 로그인 적용 테스트 중,

회원 탈퇴 시 소셜 로그인 연동 해제를 시켜줘야 하는데 DB에서는 삭제가 되지만 연동은 해제가 안되더군요...

 

확인은 탈퇴 후 재가입 시 사용자에게 아래와 같이 "동의하기" 요청이 떠야 하는데 안뜨고 바로 가입이 되어버립니다.

연동 해제가 안되었다는 거죠...

검색해봐도 다른 대안이 없더군요...

해서... 그누보드 소스에 직접 기능을 넣었습니다.

 

1. DB 삭제 함수에 기능 추가
function social_member_link_delete($mb_id, $mp_no=''){
    global $g5;

    if(!$mb_id)
        return;

    $mp_no = (int) $mp_no;

    if( G5_SOCIAL_DELETE_DAY > 0 ){

        //mb_id가 없는 소셜 데이터 중에 해당 기간이 넘어간 db 데이터를 삭제합니다.
        $time = date("Y-m-d H:i:s", time() - (86400 * (int) G5_SOCIAL_DELETE_DAY));

        $sql = "delete from {$g5['social_profile_table']} where mb_id = '' and mp_latest_day < '$time' ";
        sql_query($sql);

        $sql = "update {$g5['social_profile_table']} set mb_id='', object_sha='', profileurl='', photourl='', displayname='', mp_latest_day = '".G5_TIME_YMDHIS."' where mb_id= '".$mb_id."'";
    } else {
        $sql = "delete from {$g5['social_profile_table']} where mb_id= '".$mb_id."'"; //바로 삭제합니다.
    }

    if($mp_no){
        $sql .= " and mp_no=$mp_no";
    }

    sql_query($sql, false);
//------------------------------추가-------------------------------------------
    $provider_name = get_session('ss_social_provider');
    $adaptor = social_login_get_provider_adapter($provider_name);
    $unlinkID = $adaptor->unlink($provider_name);
    //kakao return : array[id]
    //naver return : array[result]
    //google return : "Success"
    //echo "<script>alert('$unlinkID[id]');</script>";
//-----------------------------------------------------------------------------
}

www/plugin/social/includes/functions.php 파일에,

social_member_link_delete 함수에서 DB에 소셜 계정 정보를 삭제 후, 기능을 추가했는데요.. 

 

각 개별 adaptor (카카오, 네이버, 구글 등) 선언하여 각각의 php 파일에 unlink 라는 함수를 호출하여 연동 해제를 하도록 하고 결과를 리턴받도록 하는 코드 입니다.

 

2. Kakao.php에 unlink 함수 만들기

전체적인 소스에 영향을 안주면서 필요한 기능을 구현하기 위해 내부적인 변수 및 함수 등을 이용했습니다.

www/plugin/social/Hybrid/Providers/Kakao/php 파일에,

    function initialize()
    {
        parent::initialize();

        // Provider API end-points
        $this->api->api_base_url  = "https://kapi.kakao.com/v2/";
        $this->api->authorize_url = "https://kauth.kakao.com/oauth/authorize";
        $this->api->token_url     = "https://kauth.kakao.com/oauth/token";
//---------------------------------------추가-----------------------------------        
        $this->api->unlink_url    = "https://kapi.kakao.com/v1/user/unlink";
//------------------------------------------------------------------------------        

		// redirect uri mismatches when authenticating with Kakao.
		if (isset($this->config['redirect_uri']) && !empty($this->config['redirect_uri'])) {
			$this->api->redirect_uri = $this->config['redirect_uri'];
		}
    }

initialize 함수에 카카오 개발자 문서에서 알려주는 unlink_url 주소를 선언하고,

아래와 같이 unlink 함수를 만들어 줍니다.

    function unlink($code)
    {
      $params = array(
          "grant_type"    => "authorization_code",
          "client_id"     => $this->api->client_id,
          "redirect_uri"  => $this->api->redirect_uri,
          "code"          => $code
      );

      if( $this->api->client_secret && ($this->api->client_secret !== $this->api->client_id) ){
          $params['client_secret'] = $this->api->client_secret;
      }

      $response = $this->request($this->api->unlink_url, $params, $this->api->curl_authenticate_method);
      $unlink_responseArr = json_decode($response, true);

      return $unlink_responseArr;
    }

파라미터에 client_id / redirect_uri / code(kakao) 를 담구요.

request 함수에 unlink_url 과 파라미터를 전달해줍니다.

 

    private function request($url, $params=false, $type="GET")
    {
        if(Class_exists('Hybrid_Logger')){
            Hybrid_Logger::info("Enter OAuth2Client::request( $url )");
            Hybrid_Logger::debug("OAuth2Client::request(). dump request params: ", serialize( $params ));
        }
        $this->http_info = array();
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL           , $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT       , $this->api->curl_time_out);
        curl_setopt($ch, CURLOPT_USERAGENT     , $this->api->curl_useragent);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->api->curl_connect_time_out);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->api->curl_ssl_verifypeer);
//--------------------------------------------변경-------------------------------------------------------------        
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $this->api->curl_header);        
        curl_setopt($ch, CURLOPT_HTTPHEADER    , array("Authorization: Bearer " . $this->api->access_token));
//-------------------------------------------------------------------------------------------------------------        

        if ( $this->api->curl_proxy ) {
            curl_setopt( $ch, CURLOPT_PROXY, $this->curl_proxy);
        }
        if ( $type == "POST" ) {
            curl_setopt($ch, CURLOPT_POST, 1);
            if ($params) curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($params) );
        }

        $response = curl_exec($ch);
        if(Class_exists('Hybrid_Logger')){
            Hybrid_Logger::debug( "OAuth2Client::request(). dump request info: ", serialize(curl_getinfo($ch)) );
            Hybrid_Logger::debug( "OAuth2Client::request(). dump request result: ", serialize($response ));
        }
        $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $this->http_info = array_merge($this->http_info, curl_getinfo($ch));

	curl_close ($ch);

        return $response;
    }

request 함수는 curl 방식의 통신을 통해 카카오에 전송을 하구요.

여기서 CURLOPT_HTTPHEADER 는 수정을 해줍니다. 

 

3. 결과 확인
$unlink_responseArr = json_decode($response, true);
echo "<script>alert('$unlink_responseArr[id]');</script>";

json_decode를 통해 결과값을 정리하면, 아이디가 넘어오는군요.

굳이 기존 회원 정보와 비교할 필요는 없을 거 같습니다.

값이 넘어온 것 자체가 연동 해제 되었다는 걸 알려주니까요~~

 

그누보드에서 카카오 소셜로그인 연동 해제

 

다음 포스팅에서는 네이버에 대한 소셜 로그인 연동 해제에 대해 알아보겠습니다.

728x90
반응형
Comments