Laravel BadMethodCallException not finding my model method - php

For some reasons I getting error on downloadHistorys() relationship method OneToMany:
My models User and DumpDownloadHistory:
<?php
namespace App\Models;
use App\Models\DumpDownloadHistory;
use App\Models\Groups;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $fillable = ['email', 'full_name', 'name', 'mobile', 'phone', 'fax', 'downloadPrice', 'tokens', 'dailyDownloads', 'added_by', 'groups_id', 'status', 'user_about','admin_note', 'password', 'last_access', 'last_ip'];
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
public function downloadHistorys(){
return $this->hasMany(DumpDownloadHistory::class);
}
}
<?php
namespace App\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
class DumpDownloadHistory extends Model
{
protected $fillable = ['user_id', 'dataset', 'user_ip', 'downloadCost'];
protected $table = 'dump_download_histories';
protected $primaryKey = 'id';
public function user(){
return $this->belongsTo(User::class);
}
}
My models are placed in App\Models folder. I added my models instance to the controller. I getting an error when I try to call my methods. Any help will be helpful.
auth()->user()->downloadHistorys()->create([
'user_id' => auth()->user()->id,
'dataset' => $id,
'user_ip'=> request()->ip(),
'downloadCost' => auth()->user()->downloadPrice
]);
P.S. I not using a schema builder for the database. I have connected to an existing MySQL database.
Edited
config/auth.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];

Looks like
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
this section is wrong. If you want to use App\Models\User model as your authentication, change model to App\Models\User.
And change your App\Models\User model like this
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable {}

Related

Laravel 8 Custom Login With Custom Model

Greetings to all I have a problem with my Laravel 8 code I'm getting
"ErrorException Undefined index: password" from vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php:159
I use custom model and I set up in config/auth.php to use my Client model not a User model and when I try Auth::attempt($credentials) it's failed and give me that error here is my code
Client.php Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Client extends Authenticatable
{
use HasFactory;
protected $table = 'clients';
protected $primaryKey = 'client_id';
public $incrementing = true;
protected $fillable = ['client_firstName', 'client_lastName', 'client_email', 'client_phoneNumber', 'client_password', 'client_isAdmin', 'client_created_at', 'client_updated_at'];
public $timestamps = true;
const CREATED_AT = 'client_created_at';
const UPDATED_AT = 'client_updated_at';
}
config/auth.php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
ClientController.php
public function signIn(Request $request){
if($request->isMethod('POST')){
$validator = Validator::make($request->all(), [
'signin_email' => 'required|string|email:rfc,dns|bail',
'signin_password' => 'required|string|bail',
], [
'signin_email.required' => 'The email address field is required.',
'signin_email.email' => 'You must provide an a valid email address.',
'signin_password.required' => 'The password field is required.',
]);
if($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}else{
$credentials = [
'client_email' => $request->signin_email,
'client_password' => $request->signin_password,
];
if(Auth::attempt($credentials)) {
return redirect()->to('dashboard');
}else{
return redirect()->back()->withErrors("Sorry, the passed email address or password is incorrect, try again!")->withInput();
}
}
}else{
return view('signIn');
}
}
I have found a solution I will need to add to my model getAuthPassword, also I need to change app/auth.php and ClientController.php
Client.php Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Client extends Authenticatable
{
use HasFactory;
protected $table = 'clients';
protected $primaryKey = 'client_id';
public $incrementing = true;
protected $fillable = ['client_firstName', 'client_lastName', 'client_email', 'client_phoneNumber', 'client_password', 'client_isAdmin', 'client_created_at', 'client_updated_at'];
public $timestamps = true;
const CREATED_AT = 'client_created_at';
const UPDATED_AT = 'client_updated_at';
public function getAuthPassword(){
return $this->client_password;
}
}
app/auth.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'clients',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'clients',
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'clients' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
'table' => 'clients',
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'clients' => [
'provider' => 'clients',
'table' => 'clients',
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
ClientController.php
public function signIn(Request $request){
if($request->isMethod('POST')){
$validator = Validator::make($request->all(), [
'signin_email' => 'required|string|email:rfc,dns|bail',
'signin_password' => 'required|string|bail',
], [
'signin_email.required' => 'The email address field is required.',
'signin_email.email' => 'You must provide an a valid email address.',
'signin_password.required' => 'The password field is required.',
]);
if($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}else{
$credentials = [
'client_email' => $request->signin_email,
'password' => $request->signin_password,
];
if(Auth::attempt($credentials)) {
return redirect()->to('dashboard');
}else{
return redirect()->back()->withErrors("Sorry, the passed email address or password is incorrect, try again!")->withInput();
}
}
}else{
return view('signIn');
}
}

How to use another table as the login table instead of 'Users'

I have a project that has 2 tables, 1 for users details and one for login, but I want my login table to be 'webusers' instead of 'users' as Laravel uses 'users' as the default table for authentication.
I tried to change my model and auth.php but to no luck. Has anyone tried this before?
My Model:
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
protected $table = "webusers";
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
auth.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'webusers',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'webusers',
],
'api' => [
'driver' => 'token',
'provider' => 'webusers',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'webusers' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'webusers',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
first change your model lets call it Admin like this.
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable //extends authenticable instead of Model
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
];
}
then go to confi/auth.php and change the provider method as shown below
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class, //change from User class to Admin
],
now everywhere instead of User model you need to call Admin models, the same logics can be used everywhere

Call to undefined method App\Models\User::createToken()

I'm new to Laravel and i'm getting this error: Call to undefined method App\Models\User::createToken()
Laravel Framework 8.34.0
PHP 7.4.3
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
class UserController extends Controller
{
private $sucess_status = 200;
public function createUser(Request $request){
$validator = Validator::make($request->all(),
[
'first_name' => 'required',
'last_name' => 'required',
'phone' => 'required|numeric',
'email' => 'required|email',
'password' => 'required|alpha_num|min:5'
]
);
if($validator->fails()){
return response()->json(["validattion_errors"=>$validator->errors()]);
}
$dataArray = array(
"first_name"=>$request->first_name,
"last_name"=>$request->last_name,
"full_name"=>$request->first_name . " " . $request->last_name,
"phone"=>$request->phone,
"email"=>$request->email,
"password"=>bcrypt($request->password),
);
$user = User::create($dataArray);
if(!is_null($user)){
return response()->json(["status" => $this->sucess_status, "success" => true, "data" => $user]);
}else {
return response()->json(["status" => "failed", "success" => false, "message" => "User not created"]);
}
}
public function userLogin(Request $request){
$validator = Validator::make($request->all(),
[
'email' => 'required|email',
'password' => 'required|alpha_num|min:5'
]
);
if($validator->fails()){
return response()->json(["validation_errors"=>$validator->errors()]);
}
if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){
$user = Auth::user();
$token = $user->createToken('token')->accessToken;
return response()->json(["status" => $this->sucess_status, "success" => true, "login" => true, "token" => $token, "data" => $user]);
} else{
return response()->json(["status" => "failed", "success" => false, "message" => "Invalid email or password"]);
}
}
public function userDetail(){
$user = Auth::user();
if(!is_null($user)){
return response()->json(["status" => $this->sucess_status, "success" => true, "user" => $user]);
}else {
return response()->json(["status" => "failed", "success" => false, "message" => "No user found"]);
}
}
}
My Auth.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
I had run the php artisan passport: install command 3 times
because I forgot to save the file with the settings and I thought it would be necessary to run the code again after saving the file
And returned me this:
Encryption keys already exist. Use the --force option to overwrite them.
Personal access client created successfully.
Client ID: 3
Client secret: JoZbAGCSOZ6t0hn7YnnT6PdN4EMUUZa7H1vU6Sk2
Password grant client created successfully.
Client ID: 4
Client secret: yAxjrBnvPWCiAdXod5FmJDQTNDmneRoO1LtM6B0x
cristiansto in Laravel/API/todoList
❯ php artisan passport:install --force
Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 5
Client secret: 8CtWyvXIwapZnfO5dTGDsyF0iXvJsxNyiZeUksTL
Password grant client created successfully.
Client ID: 6
Client secret: 9jThPxOfgNxJINFKIbDz0WU5yYEup0pIkboEJLr0
cristiansto in Laravel/API/todoList
I dont know if this is the cause.
What could it be?
the method createToken is in HasApiTokens trait, you should use it In your User Model
:
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
I discovered the issue, i had to put
use Laravel\Passport\HasApiTokens; inside the User.php
and then
use HasFactory, Notifiable, HasApiTokens;
It works on me
Laravel Framework 8.83.5
PHP 7.4.19
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
....
}
HasFactory and Notifiable are default User Model, just add HasApiTokens so it becomes:
use HasApiTokens, HasFactory, Notifiable;

Laravel Custom PasswordBroker not sending password reset link email

I'm using customized password reset function in Laravel.
Laravel version 5.8
I followed this document https://laravel.com/docs/5.8/passwords#password-customization
And also adding custom guard I checked here https://laravel.com/docs/5.8/authentication#adding-custom-guards
I don't know what I'm doing wrong with these settings below.
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'customers' => [
'driver' => 'session',
'provider' => 'customers',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\CustomerLoginInfo::class,
],
'users' => [
'driver' => 'database',
'table' => 'users',
],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
// 'users' => [
// 'provider' => 'users',
// 'table' => 'password_resets',
// 'expire' => 60,
// ],
'customers' => [
'provider' => 'customers',
'table' => 'customer_password_resets',
'expire' => 60,
],
],
];
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
class ForgotPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset emails and
| includes a trait which assists in sending these notifications from
| your application to your users. Feel free to explore this trait.
|
*/
use SendsPasswordResetEmails;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
protected function broker()
{
return Password::broker('customers');
}
/**
* Validate the email for the given request.
*
* #param \Illuminate\Http\Request $request
* #return void
*/
protected function validateEmail(Request $request)
{
$request->validate(['login_email' => 'required|email']);
}
/**
* Get the needed authentication credentials from the request.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
protected function credentials(Request $request)
{
return $request->only('login_email');
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
class ResetPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
/**
* Where to redirect users after resetting their password.
*
* #var string
*/
protected $redirectTo = '/form/03';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest:customers');
}
protected function guard()
{
return Auth::guard('customers');
}
protected function broker()
{
return Password::broker('customers');
}
}
My Model extends authenticatable
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class CustomerLoginInfo extends Authenticatable
{
use Notifiable;
use SoftDeletes;
protected $table = 'customer_login_info';
protected $fillable = ['customer_id', 'login_email', 'password'];
public function customer()
{
return $this->belongsTo('App\Models\Customer');
}
public function getEmailForPasswordReset()
{
return $this->login_email;
}
}
Even though I get success message I don't receive password reset link email.
I'm using docker mailhog and I don't have any problem receiving other kinds of mails.
If you found anything weird in this code please let me know.
Replying to myself, I found a solution, I digged into this problem for hours and in Laravel 5.8 you need
public function routeNotificationForMail($notification)
{
return $this->login_email;
}
in your authenticatable, if you are using email column's name other than email.
https://laravel.com/docs/5.8/notifications#customizing-the-recipient

Symfony\Component\Debug\Exception\FatalThrowableError: Argument 2 passed to Illuminate\Auth\SessionGuard::__construct()

I'm trying to implement Laravel passport for two tables, so one would be default User and second one would be for Devices.
I created in the config the Guard for the new model and if I try to register the devices its writing to the Device table everything but, right now if I try to login it gives me this error:
Symfony\Component\Debug\Exception\FatalThrowableError: Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must implement interface Illuminate\Contracts\Auth\UserProvider, null given, called in /Users/petarceho/laravel/api/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 127 in file /Users/petarceho/laravel/api/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 99
I'm new to Laravel so sorry for not able to explain it well, but can I use Laravel passport for two tables.
Here is my Controller class for Devices :
<?php
namespace App\Http\Controllers;
use App\Device;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DeviceController extends Controller{
//register
public function signupDevice(Request $request)
{
//cant registed with the same email twice
if(sizeof(Device::where('name','=',$request->query('name'))->get()) > 0)
return response()->json(['name has already been taken'],500);
$request->validate([
'name' => 'required|string',
'password' => 'required|string|confirmed']);
$device =new Device(
[
'name'=>$request->name,
'password'=>bcrypt($request->password)
]);
$device->save();
return response()->json([
'message' => 'Successfully created device!'
], 201);
}
public function login(Request $request)
{
if (Auth::guard('device')->attempt(['name' => request(['name']), 'password' => request(['password'])])) {
$details = Auth::guard('device')->user();
$user = $details['original'];
return $user;}
else {return 'auth fail';}
}
public function login1(Request $request){
//validate the data input
$request->validate([
'name' => 'required|string', // |string|name
'password' => 'required|string',
// 'remember_me' => 'boolean'
]);
$credentials = request(['name', 'password']);
if(!Auth::attempt($credentials))
return response()->json([
'message' => 'Unauthorized'
], 401);
$device = $request->user('device');
// $device = Auth::guard('device')->device();
$tokenResult = $device->createToken('Personal Access Token');
$token = $tokenResult->token;
if ($request->remember_me)
$token->expires_at = Carbon::now()->addWeeks(1);
$token->save();
return response()->json([
'access_token' => $tokenResult->accessToken,
'token_type' => 'Bearer',
'expires_at' => Carbon::parse(
$tokenResult->token->expires_at
)->toDateTimeString()
],200);
}
}
Here is my config auth.php file.
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
'device'=>
[
'driver'=>'session',
// 'model'=>App\Device::class,
'provider'=>'devices',
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'devices' => [
'driver' => 'eloquent',
'model' => App\Device::class,
],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
and my routes:
//routes for device auth
Route::group(
[
'prefix'=>'auth/device'
],function ()
{
Route::post('signup','DeviceController#signupDevice');
Route::post('login','DeviceController#login');
});
the App/Device class
the Illuminate\Foundation\Auth\Device looks exactly like User class
<?php
namespace App;
use Illuminate\Foundation\Auth\Device as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class Device extends Authenticatable{
use Notifiable;//,HasApiTokens;
protected $table ='device';
protected $fillable = [
'name', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Any idea how to use Laravel passport for Multi auth authentication for two tables ?

Categories