Support the ongoing development of Laravel.io →
posted 10 years ago
Authentication
Last updated 2 years ago.
0
Event::listen('illuminate.query', function($sql)
{
	var_dump($sql);
});

Paste this to routes.php and it will display all the queries that are ran.

Last updated 2 years ago.
0

Or use this to show your queries: https://github.com/barryvdh/laravel-debugbar

Last updated 2 years ago.
0

Ok, both good suggestions. Thank you...

Last updated 2 years ago.
0

Check your app/config/auth.php file to see what auth driver you have defined.

I am not 100% but I am pretty sure that if you choose

'driver'=>'eloquent'

then you can define a model that isn't tied to any database, which should in turn should prevent any DB queries.

Last updated 2 years ago.
0

If you look at Guards user method you'll see that if you call user() it will hit the database the first time and store that user so that all subsequent calls will reuse that user object.

Last updated 2 years ago.
0

I wrote a custom Auth driver to authenticate with a webservice and I noticed Auth::attempt will return a valid user but Auth.Guard wont store that user for subsequent requests. Doing Auth::user() will go an re-authenticate with my webservice everytime. I bypassed this by saving a serialized user object in the session but would much rather see if Auth.guard can store the logged in user on it's own. My custom AuthProvider for reference below

class RestAuthProvider implements UserProviderInterface { /** * The user object. */ private $user;

/**
 * Constructor
 *
 * @return void
 */
public function __construct()
{
    if (!empty(Session::get('user'))) {
        $this->user = unserialize(Session::get('user'));
    } else {
        $this->user = null;
    }
}

/**
 * Retrieve a user by their unique identifier.
 *
 * @param  mixed  $id
 * @return \Illuminate\Auth\UserInterface|null
 */
public function retrieveById($id)
{
    try {

        // check if we have a user
        if (is_null($this->user)) {
            $this->user = WebserviceController::getUserAccount($id);
            Session::set('user', serialize($this->user));
        }

        // return user object
        return $this->user;

    } catch(\GuzzleHttp\Exception\ClientException $e) {

        Log::error($e->getRequest());
        if ($e->hasResponse()) {
            Log::error($e->getResponse());
        }

    }
}

/**
 * Retrieve a user by the given credentials.
 *
 * @param  array  $credentials
 * @return \Illuminate\Auth\UserInterface|null
 */
public function retrieveByCredentials(array $credentials)
{
    // make login request via Guzzle
    try {

        $user = WebserviceController::loginUser($credentials);

        // store user in session
        Session::set('user', serialize($this->user));

        // returns GenericUser object
        return $user;

    } catch(\GuzzleHttp\Exception\ClientException $exception) {

        Log::error($exception->getRequest());
        if ($exception->hasResponse()) {
            Log::error($exception->getResponse());
        }

    }

}

/**
 * Validate a user against the given credentials.
 *
 * @param  \Illuminate\Auth\UserInterface  $user
 * @param  array  $credentials
 * @return bool
 */
public function validateCredentials(UserInterface $user, array $credentials)
{
    return true;
}

/**
 * Needed by Laravel 4.1.26 and above
 */
public function retrieveByToken($identifier, $token)
{
    return new \Exception('retrieve by token not implemented');
}

/**
 * Needed by Laravel 4.1.26 and above
 */
public function updateRememberToken(UserInterface $user, $token)
{
    print_r("update token".$user.$token); die;
}

damienadermann said:

If you look at Guards user method you'll see that if you call user() it will hit the database the first time and store that user so that all subsequent calls will reuse that user object.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

bangweb bangweb Joined 7 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.