소셜로그인 클라이언트 변경하는 경우 > 그누보드5 팁자료실

그누보드5 팁자료실

소셜로그인 클라이언트 변경하는 경우 정보

소셜로그인 클라이언트 변경하는 경우

본문

소셜로그인 하다보면 부득이하게 소셜로그인 클라이언트 아이디를 변경하게 되는데
변경하고나면 기존 회원이 로그인이 아닌 회원가입으로 페이지 전환이 된다.

같은 이메일 주소인 경우 Identifier를 업데이트하도록 하는 로직입니다.

 

 

<code>

// plugin/social/includes/functions.php:547 추가

// 함수 내 global $g5 추가 

// --- [ADD] 이메일 기반 재매핑: 클라이언트 변경으로 identifier가 바뀐 경우 병합 후 로그인 ---

            if (! $is_member && !empty($user_profile)) {
                // 네이버 응답: 이메일 동의가 되어 있어야 값이 온다 (emailVerified 우선)
                $email = '';
                if (property_exists($user_profile, 'emailVerified') && $user_profile->emailVerified) {
                    $email = strtolower(trim($user_profile->emailVerified));
                } elseif (property_exists($user_profile, 'email') && $user_profile->email) {
                    $email = strtolower(trim($user_profile->email));
                }

                if ($email) {
                    // 1) 동일 이메일 회원 존재 여부
                    $mb = sql_fetch("SELECT mb_id, mb_email, mb_leave_date, mb_intercept_date FROM {$g5['member_table']} WHERE mb_email = '".sql_real_escape_string($email)."'");
                    if ($mb && $mb['mb_id'] && empty($mb['mb_leave_date']) && empty($mb['mb_intercept_date'])) {
            
                        // 2) 같은 이메일에 연결된 기존 소셜 레코드(예전 identifier)를 찾는다
                        $old = sql_fetch("
                            SELECT mp_no, provider, identifier, mb_id
                            FROM {$g5['social_profile_table']}
                            WHERE provider='naver' AND mb_id='".sql_real_escape_string($mb['mb_id'])."'
                            ORDER BY mp_no DESC
                            LIMIT 1
                        ");
            
                        // 3) 현재 로그인 시도한 네이버의 새 identifier
                        $new_identifier = $user_profile->identifier; // Hybridauth profile->identifier (네이버 uid 매핑) 
            
                        // 4) 이미 새 identifier로 연결되어 있다면 바로 로그인
                        if ($old && $old['mb_id'] && $old['identifier'] === $new_identifier) {
                            // 바로 로그인 처리 (기존 로직과 동일)
                            $mb_password = sha1( str_shuffle( "0123456789abcdefghijklmnoABCDEFGHIJ" ) );
                            echo social_return_from_provider_page($provider_name, $login_action_url, $mb['mb_id'], $mb_password, $url, $use_popup);
                            exit;
                        }
            
                        // 5) 다른 계정에 이미 새 identifier가 묶여 있지 않은지 방어
                        $dup = sql_fetch("
                            SELECT COUNT(*) AS cnt
                            FROM {$g5['social_profile_table']}
                            WHERE provider='naver' AND identifier='".sql_real_escape_string($new_identifier)."'
                        ");
                        if (intval($dup['cnt']) > 0) {
                            // 동일 네이버 계정이 다른 계정에 연결된 상태 -> 충돌 안내
                            $msg = '동일 네이버 계정이 이미 다른 계정과 연결되어 있어 자동 병합할 수 없습니다.';
                            if ($use_popup == 1 || ! $use_popup) alert_close($msg); else alert($msg);
                            if (is_object($adapter)) social_logout_with_adapter($adapter);
                            exit;
                        }
            
                        // 6) 기존 소셜 레코드가 있으면 identifier만 새 값으로 업데이트(클라이언트 변경 대응)
                        if ($old && $old['mp_no']) {
                            sql_query("
                                UPDATE {$g5['social_profile_table']}
                                SET identifier = '".sql_real_escape_string($new_identifier)."', mp_latest_day = '".G5_TIME_YMDHIS."'
                                WHERE mp_no = ".intval($old['mp_no'])."
                            ");
                        } else {
                            // 과거 소셜 레코드가 없었다면 새 레코드로 연결
                            // 필요한 필드만 최소 입력. 세부 프로필은 social_user_profile_replace가 갱신
                            sql_query("
                                INSERT INTO {$g5['social_profile_table']}
                                    (mb_id, provider, identifier, mp_register_day, mp_latest_day)
                                VALUES
                                    ('".sql_real_escape_string($mb['mb_id'])."', 'naver', '".sql_real_escape_string($new_identifier)."', '".G5_TIME_YMDHIS."', '".G5_TIME_YMDHIS."')
                            ");
                        }
            
                        // 7) 최신 프로필 저장 및 로그인
                        social_user_profile_replace($mb['mb_id'], $provider_name, $user_profile);
                        set_session('ss_social_provider', $provider_name);
            
                        // 팝업/일반 로그인 처리 동일하게 반환
                        $mb_password = sha1( str_shuffle( "0123456789abcdefghijklmnoABCDEFGHIJ" ) );
                        echo social_return_from_provider_page($provider_name, $login_action_url, $mb['mb_id'], $mb_password, $url, $use_popup);
                        exit;
                    }
                }
            }
            // --- [END ADD] 이메일 기반 재매핑 ---

</code>

추천
2

댓글 1개

전체 8 |RSS
그누보드5 팁자료실 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT