Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:21:28+00:00 2026-06-17T18:21:28+00:00

I have a REST API setup with CakePHP 1.3, which uses the Security component

  • 0

I have a REST API setup with CakePHP 1.3, which uses the Security component combined with the Auth component to facilitate HTTP Authentication for user login, like this:

UsersController:

function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('view', 'add');

    if ( $this->params['url']['ext'] == 'json' && !$this->Auth->user() ) {
        $this->Auth->allow('edit', 'add_friend');
        $this->Security->loginOptions = array(  
            'type'=>'basic',  
            'login'=>'authenticate',  
            'realm'=>'MyRealm'  
        );  
        $this->Security->loginUsers = array();  
        $this->Security->requireLogin('edit', 'add_friend');  
    }
}

AppController:

function authenticate($args) {  
    $data[ $this->Auth->fields['username'] ] = $args['username'];  
    $data[ $this->Auth->fields['password'] ] = $this->Auth->password($args['password']);  
    if ( $this->Auth->login($data) ) {
        return true;  
    } else {  
        $this->Security->blackHole($this, 'login');  
        return false;  
    }  
}

This is all working fine, my problem though is that I have a method which can optionally have authentication or not. It appears to me that the Security component in CakePHP has the requireLogin() method, which forces you to be authenticated.

I tried creating a new authenticate() method which always returned true:

function optionalAuthenticate($args) {  
    $data[ $this->Auth->fields['username'] ] = $args['username'];  
    $data[ $this->Auth->fields['password'] ] = $this->Auth->password($args['password']);  
    $this->Auth->login($data);
    return true;
}

But unfortunately that hasn’t worked.

Does anyone know a way with the Security component that I can accomplish some kind of optional authorization?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:21:30+00:00Added an answer on June 17, 2026 at 6:21 pm

    I ended up accomplishing this by not using the Security component at all, by declaring the optionalAuthenticate() method in the app_controller.php:

    function optionalAuthenticate() { 
        if(empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW']))
            return false;
        $data[ $this->Auth->fields['username'] ] = $_SERVER['PHP_AUTH_USER'];  
        $data[ $this->Auth->fields['password'] ] = $this->Auth->password($_SERVER['PHP_AUTH_PW']);
        if($this->Auth->login($data))
            return true;
        else
            return false;
    } 
    

    And then just calling it from within whatever methods I want to attempt to authenticate (eg. in users_controller.php):

    function view($id = null) {
        $this->optionalAuthenticate();
        $user = $this->Auth->user();
        if($user)
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a REST api which dumps some json data (user info, etc). Now
I have a REST API which accepts XML in HTTP Posts. When I send
I have a scenario in which I have REST API which manages a Resource
I have set up a REST API using the Slim PHP framework, which works
I have a rest API which is able to produce objects keys with null
I have a REST api written with JAX-RS, and I need to add authentication
I have a REST API server that uses Express, Mongoose and config . I
BACKGROUND: I have a REST API implemented in Java using Jersey. My API uses
I have a REST API using Django Tastypie. Given the following code The models
I have a REST api with Gzip compression enabled, it's developed using the ASP.net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.