Welcome Guest, Not a member yet? Register   Sign In
Authenticate Against Database with Phil Sturgeon's Rest-Server
#1

[eluser]blcArmadillo[/eluser]
I'm trying to modify Phil Sturgeon's rest-server to authenticate against ion-auth. I changed _check_login to:

Code:
private function _check_login($username = '', $password = NULL)
{
    if (empty($username))
    {
        return FALSE;
    }
    
    $this->rest->db = $this->load->database('default', TRUE);
    $sql = 'SELECT api_key FROM users WHERE email = ?';
    $query = $this->rest->db->query($sql, urldecode($username));
    
    if ($query->num_rows() > 0) {
        $user = $query->row_array();
    } else {
        return FALSE;
    }

    // If actually NULL (not empty string) then do not check it
    if ($password !== NULL AND $user['api_key'] != $password)
    {
        return FALSE;
    }

    //return TRUE;
    return $user['api_key'];
}

My first question is do I need to do:
Code:
$this->rest->db = $this->load->database('default', TRUE);

It seems to me like $this->rest->db should already be accessible since it's set in the constructor however I get an error saying I'm trying to query on a non-object if I don't add the load->database() line in _check_login like above. NOTE: I removed the if statement in the constructor that only initializes the db connection if keys or logging are enabled so this shouldn't be the issue.

My second issue I think is a result of question 1. Anyhow, after making the changes to REST_Controller my models in my REST controllers no longer work. The error I'm getting is:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: User::$db

Filename: core/Model.php

Line Number: 50

which appears to be occurring when I make a call to a method in one of my model's.

Hopefully that's enough info to help pinpoint the problem. Any ideas on what I might be doing wrong? Thanks!
#2

[eluser]geotravel[/eluser]
I am looking ready solution function to API REST descibed above with add auth from existing DB application use username/password (stored with MD5 in DB).
#3

[eluser]LastRoseStudios[/eluser]
All you should need to do is replace

Code:
$this->rest->db = $this->load->database('default', TRUE);
    $sql = 'SELECT api_key FROM users WHERE email = ?';
    $query = $this->rest->db->query($sql, urldecode($username));
    
    if ($query->num_rows() > 0) {
        $user = $query->row_array();
    } else {
        return FALSE;
    }

    // If actually NULL (not empty string) then do not check it
    if ($password !== NULL AND $user['api_key'] != $password)
    {
        return FALSE;
    }

    //return TRUE;
    return $user['api_key'];
}

with

Code:
if ($this->ion_auth->login($username, $password, 0)) {
    return true
} else {
    return false
}
#4

[eluser]groggster[/eluser]
I was about to post the same question.

I will have to give this a try.


Thanks Smile




Theme © iAndrew 2016 - Forum software by © MyBB