CI 묻고 답하기

제목 tank_auth와 restful 라이브러리를 사용해서 안드로이드 로그인을 하고싶습니다.
글쓴이 일용직노동자 작성시각 2014/10/02 23:57:43
댓글 : 4 추천 : 0 스크랩 : 0 조회수 : 18071   RSS

안드로이드 네이티브앱과의 통신을 위해서 rest 라이브러리를 다운받았는데요
엄청 어렵네요;

만약 www.aaa.com/auth/login에 폼이 있고
tank_auth login을 통해 ci_session을 리턴해주는데
이걸 json으로 파싱받고싶습니다.

curl을 사용해야될것같은데 음..
rest 쪽에서 기본 예제로 올려놓은것이

class Example extends REST_Controller{

    function user_get(){
        if(!$this->get('id')){
            $this->response(NULL, 400);
        }

        // $user = $this->some_model->getSomething( $this->get('id') );
        $users = array(
            1 => array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com', 'fact' => 'Loves swimming'),
            2 => array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com', 'fact' => 'Has a huge face'),
            3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
        );
        
        $user = @$users[$this->get('id')];
        
        if($user)
        {
            $this->response($user, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'User could not be found'), 404);
        }
    }
    
    function user_post(){
        //$this->some_model->updateUser( $this->get('id') );
        $message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');
        
        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function user_delete(){
        //$this->some_model->deletesomething( $this->get('id') );
        $message = array('id' => $this->get('id'), 'message' => 'DELETED!');
        
        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function users_get(){
        //$users = $this->some_model->getSomething( $this->get('limit') );
        $users = array(
            array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
            array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
            3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
        );
        
        if($users)
        {
            $this->response($users, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any users!'), 404);
        }
    }
이런 식인데요
저기서 저 user 부분( $user = @$users[$this->get('id')];)을
http://localhost/auth/login 에서 받아서 json으로 출력하고싶은데
안드로이드는 저걸 그대로 받으면 json을 받으면 될 것같은데 잘 안되네요

결과적으로 tank_auth와 restful을 엮고싶습니다.
tank_auth의 view는

//<?php echo form_open($this->uri->uri_string()); ?>        
//                <fieldset>
//                    <legend>로그인 정보 입력</legend>
//                    <div class="input_box">
//                        <?php echo form_input($login); ?>
//                        <?php echo form_password($password); ?>    
//                    </div>
//                    <p class="login_btn">
//                        <?php echo form_submit('submit', '로그인'); ?>
//                    </p>
//                    <!-- <div class="login_check">
//                        <p class="checkbox">
//                        <?php echo form_checkbox($remember); ?>
//                        <span><?php echo form_label('자동로그인', $remember['id']); ?></span>
//                        </p>
//                    </div> -->
//                </fieldset>
//                <?php echo form_close(); ?>

이런식으로 넘어가고
auth/login method에 action을 주고 그 후에

if ($this->form_validation->run()) {                                // validation ok
            if ($this->tank_auth->login(
                    $this->form_validation->set_value('login'),
                    $this->form_validation->set_value('password'),
                    $this->form_validation->set_value('remember'),
                    $data['login_by_username'],
                    $data['login_by_email'])) {   

이런식으로 폼검증이 완료되면 tank_auth 라이브러리에 login 메소드에 저 값들을 보냅니다.

function login($login, $password, $remember, $login_by_username, $login_by_email){
        
        if ((strlen($login) > 0) AND (strlen($password) > 0)) {

            // Which function to use to login (based on config)
            if ($login_by_username AND $login_by_email) {
                $get_user_func = 'get_user_by_login'; //이메일로그인인지 아이디로그인인지 체크
            } else if ($login_by_username) {
                $get_user_func = 'get_user_by_username';
            } else {
                $get_user_func = 'get_user_by_email';
            }
            if (!is_null($user = $this->ci->users->$get_user_func($login))) {    // login ok
                
                // // Does password match hash in database?
                // $hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
                // if ($hasher->CheckPassword($password, $user->M_PW)) {        // password ok
                //         var_dump($user->M_PW);
                //         var_dump($password);
            
                if($password == $user->M_PW){
                        if ($user->M_STATE == 'D') {                                    // fail - banned
                            $this->error = array('banned' => $user->ban_reason);

                        } else {
                            $this->ci->session->set_userdata(array(
                                    'user_id   '=> $user->IDX,
                                    'userid'    => $user->M_ID,
                                    'username'  => $user->M_NAME,
                                    'status'    => ($user->M_STATE == 'Y') ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED,
                            ));

일단은 암호화는 안했는데요 이런식으로 인자로 넘어온 $password와 db상의 password와 일치하면
session을 넘기고 로그인되었습니다를 리턴하고 넘어갑니다.

그 후 웹에서 로그인이 되었습니다 알럿 및
  if($this->session->userdata('userid'))
{
?>
}
이런식으로 세션값으로 로그아웃보이는 부분 처리하니 로그아웃까지 잘나오는데

 

이걸 이제 안드로이드랑 통신할려고합니다.
어떻게해야될지 막막하네요;
예제는 많은데 으.. 혼란스럽습니다.
지금은 restful 라이브러리와 tank_auth를 엮어서
tank_auth success후 session을 받아서 json으로 안드로이드에 뿌려서
세션가지고 놀게하고싶은데

머리가 아프네요 ㅠㅠ
 

 다음글 질문... (2)
 이전글 쿼리 오류를 캐치 하고 싶습니다. (4)

댓글

일용직노동자 / 2014/10/03 03:15:24 / 추천 0

그냥
 $android_sess = $this->session->all_userdata(); 로 그냥 로그인 웹용/안드로이드용 2개 만들어서

안드로이드에서 json 파싱하고 session json decode하고 배열로 세션다시 넣는식으로 작업했습니다.

혹시 이렇게 되면 문제가 있을려나요?

taegon / 2014/10/03 14:19:03 / 추천 0
꼭 restful을 쓰셔야 되는 이유가 있으신가요? restful을 쓰면야 좋지만 필요에 따라서는 의미가 없을 수도 있습니다. 저 같은 경우는 모델은 그대로 가고 컨트롤러만 API 용으로 따로 만들어서 리턴을 무조건 json으로 주도록 했습니다. 넘어오는 header를 보면 안드로이드인지 아이폰/아이패드인지 구분이 가니까요.
 
일용직노동자 / 2014/10/06 08:06:52 / 추천 0

API용으로 만든다는게 정확히 어떤 의미인지 잘 모르겠네요 ㅠㅠ

user agent로 안드로이드, 아이폰/아이패드를 구분하는법은 알겠는데 API를 만든다는 개념이 머리속에 잘 안잡히네요

일용직노동자 / 2014/10/06 09:06:18 / 추천 0
찾아가면서 공부해보니 해결된것같습니다. 답변감사합니다 ㅠㅠ