testy's avatar

Lumen Auth::attempt does not work

Hey, i tried to make an Auth system in Lumen. I have implements the Authenticatable Contract to my User Model. The users table is set.

My env:

AUTH_DRIVER=ELOQUENT
AUTH_MODEL=USER
AUTH_TABLE=USERS

SESSION_DRIVER=file

The Auth::check() method works, but the Auth::attempts() not. I became an 500 error, without more infos :(

My User Model:

<?php

  namespace App;

  use Illuminate\Contracts\Auth\Authenticatable;
  use Illuminate\Database\Eloquent\Model;

  class User extends Model implements Authenticatable {

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
      // TODO: Implement getAuthIdentifier() method.
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
      // TODO: Implement getAuthPassword() method.
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
      // TODO: Implement getRememberToken() method.
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string $value
     *
     * @return void
     */
    public function setRememberToken($value)
    {
      // TODO: Implement setRememberToken() method.
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
      // TODO: Implement getRememberTokenName() method.
    }}

I dont have any methods on work, is this required?

The middlewares are all active.

And this is my try:

if( ! Auth::attempt(Request::all())) { ... }

The Request::all() return an array with username and password.

0 likes
13 replies
mabua's avatar

I have the same problem. When I attempt im not getting a reply. Only a 500 Error.

henninghorn's avatar

@testy

I'm not sure if the .env is case sensitive, but perhaps you should do this

AUTH_DRIVER=ELOQUENT
AUTH_MODEL=User
AUTH_TABLE=users

SESSION_DRIVER=file

Furthermore, you need to actually implement those methods defined by the contract. Use the Authenticatable trait.

Like this:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthContract;

class User extends Model implements AuthContract {

    use Authenticatable;

}
dawiyo's avatar

I'm having the same issue. Did you ever find a solution?

jekinney's avatar

I know you have to copy and paste the larval conf files up to your root from the vender file. But keep in mind lumen wasn't created for these methods. Might just want to use laravel if your using authentication etc.

jekinney's avatar

@bestmomo it's not readily available in lumen. One of the features that is in the vender file under laravel, but not initiated out of the box.

jekinney's avatar

@bestmomo I stand corrected. The few lumen apps I created/played with I put my models in a folder in which case I had to pull up the conf file for auth.php to change the default values etc to get it to work properly.

But my actual live lumen apps don't require any auth as they either run scheduled tasks and/or artisan commands.

shiny's avatar

@bestmomo Thanks for the effort to help, but how am I to find the solution that jekinney seems to have found? I am new to lumen and haven't worked with laravel since version 3. Can you offer more direction on what he solution is?

bestmomo's avatar

@shiny there is no secret, the core is complete, just add all stuff that is in Laravel (controllers, routes...) and it works.

shiny's avatar

@bestmomo - actually it is not working for me. Unrelated to Service Providers, it is using Sessions and caching when I have left those services commented out in app.php. When my form POST fails validation the app errors out trying to work with a cache and having started a Session to send a message in some way. I have spent over 2 hours trying to discover and fix this issue and am about to recommend not using Laravel or Lumen to my boss.

ffsantos92's avatar

@testy @bestmomo

This is working for me (Lumen v5.0):

.env:

AUTH_DRIVER=eloquent
AUTH_MODEL=\App\Models\User
AUTH_TABLE=users

User model:

<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];
}

User controller:

    public function login(Request $request)
    {
        $this->validate($request, [
            'email'    => 'required|email',
            'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return ['result' => 'ok'];
        }

        return ['result' => 'not ok'];
    }
liberating's avatar

namespace App\Http\Controllers\Customer;

use Illuminate\Http\Request; use App\Http\Controllers\Controller; //use Validator; use App\Http\Model\Customers\Customer; use Auth;

class CustomerController extends Controller { public function postCustomerLoginForm(Request $request) { $this->validate($request, ['email' => 'email|required', 'password' => 'required|min:4']);

    $credentials = $request->only('email', 'password');
    //dd($credentials);

    if(Auth::attempt($credentials))
    {
        //return redirect()->route('customer.account');
        return ['result' => 'ok'];
    }

    //return redirect()->back();
    return ['result' => 'not ok'];
}

}

//Model namespace App\Http\Model\Customers;

use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Customer extends Model implements AuthenticatableContract, CanResetPasswordContract { // protected $table = 'customers';

protected $fillable = ['firstname', 'lastname', 'email', 'password'];

}

//View {{-- @include('admin.inc.header') --}}

Customer Register Form
            <div class="panel-body">
                <form class="form-horizontal" method="POST" action="{{ route('customer.account.login.form.post') }}">
                    {{ csrf_field() }}

                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" >

                            @if ($errors->has('email'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Password</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control" name="password" >

                            @if ($errors->has('password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    {{-- <div class="form-group">
                        <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
                        </div>
                    </div>

--}} Checkboxes Checkbox 1

                                    </div>
                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Register
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{{-- @include('admin.inc.footer') --}}

//web.php //get Route::get('/customer/account', [ 'as' => 'customer.account', 'uses' => 'Customer\CustomerController@getCustomerAccount', ]);

// customer account login form Route::get('/customer/account/login/form', [ 'as' => 'customer.account.login.form', 'uses' => 'Customer\CustomerController@getCustomerLoginForm', ]);

Route::post('/customer/account/login/form/post', [ 'as' => 'customer.account.login.form.post', 'uses' => 'Customer\CustomerController@postCustomerLoginForm', ]);

//Results

result "not ok"

Please or to participate in this conversation.