I am currently writing an application that is using Laravel 5.3 on the backend, and I'm looking for a way to overwrite the default password reset behaviour.
The class that I need to change is "ResetPassword" located here:
/Illuminate/Auth/Notifications/ResetPassword.php
Reason for the change is, that the reset url generated in this file is not correct for my front-end - as it uses url(), which puts the API url rather then the front-end url in the reset email.
You can override CanResetPassword's sendPasswordResetNotification() method in your User.php
use Illuminate\Notifications\Notifiable;
use App\Notifications\CustomResetPasswordNotification;
public function sendPasswordResetNotification($token)
{
$this->notify(new CustomResetPasswordNotification($token));
}
and create CustomResetPasswordNotification.php according your requirements.
Check Password Reset Emails section here for more details
I found a quick and easy way to overwrite the password reset process by overwriting the User class located here:
/Illuminate/Foundation/Auth/User.php
Basically, I created my own version like this:
<?php
namespace App\Traits\Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use App\Traits\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
I saved it to /App/Traits/Auth and now use it in my User model.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use App\Traits\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* 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',
];
}
Once finished, you can create your own version of the "CanResetPassword" trait and replace the Notification class and make necesarry adjustments.
Here is an example replacement for the "CanResetPassword" trait:
namespace App\Traits\Auth\Passwords;
use App\Notifications\CustomResetPassword as ResetPasswordNotification;
trait CanResetPassword
{
/**
* Get the e-mail address where password reset links are sent.
*
* #return string
*/
public function getEmailForPasswordReset()
{
return $this->email;
}
/**
* Send the password reset notification.
*
* #param string $token
* #return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
}
Related
Laravel makes me crazy showing the next following error:
The error happens when login controller is triggered.
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
// $credentials['type']=1;
// return $credentials;die;
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles && ! $lockedOut) {
$this->incrementLoginAttempts($request);
}
return $this->sendFailedLoginResponse($request);
}
Seems like is an error related to the database though the login service is managed by an API that works well. I don't know why this error happens. I'm really newbie to Laravel so if anyone can guide me I will thank.
User Model
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password','phone','photo','first_name', 'contact_name', 'address', 'phone_number', 'fiscal_number', 'about_us',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function subscription()
{
return $this->hasMany('App\Models\EmployerSubscription','employer_id','_id');
}
public function jobseekers()
{
return $this->hasOne('App\Models\JobSeekers');
}
public function experience()
{
return $this->hasMany('App\Models\Experience')->where('status',true);
}
}
The default User model is extending Illuminate\Database\Eloquent\Model, where it has to extend Jenssegers\Mongodb\Eloquent\Model to work with MongoDB. By changing the User model in app/User.php, this can be resolved. Change your user model to following-
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends \Jenssegers\Mongodb\Eloquent\Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password','phone','photo','first_name', 'contact_name', 'address', 'phone_number', 'fiscal_number', 'about_us',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function subscription()
{
return $this->hasMany('App\Models\EmployerSubscription','employer_id','_id');
}
public function jobseekers()
{
return $this->hasOne('App\Models\JobSeekers');
}
public function experience()
{
return $this->hasMany('App\Models\Experience')->where('status',true);
}
}
As you are saying that login service is managed by API, but as i can see you have used laravel Auth service provider for authentication. Please check database settings in your project .env file. if issue still persist kindly follow below steps.
composer dump-autoload
php artisan cache:clear
php artisan view:clear
php artisan config:clear
and restart your server. Hope that will work.
Go to User model and add the following;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Eloquent implements AuthenticatableContract,
I'm using the library Bican Roles. I change User.php for:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Bican\Roles\Traits\HasRoleAndPermission;
use Bican\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract
{
use Authenticatable, CanResetPassword, HasRoleAndPermission,Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'surnames', 'email', 'password','phone',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
When try to register a new userthrow the following error:
Class 'App \ Model' not found
I have tried to add it
use User;
etc but still not working, any ideas? Thank you
You need to add
use Illuminate\Database\Eloquent\Model;
to the top of your class declaration, not use User;
The error you're getting is
Class 'App \ Model' not found
not
Class 'User' not found
try
use Illuminate\Database\Eloquent\Model;
I am using Laravel 5 to build a user based application. Some models have a manyToMany relationship in my app and therefore I am using pivot tables.
When I delete a user from the system, I use this simple function:
/**
* Delete user.
*
* #param $id
* #return mixed
*/
public function deleteUser($id)
{
return $this->user->whereId($id)->delete();
}
However, when the user is deleted, the rows in the pivot tables (for example role_user) do not get deleted.
I have read on the laravel site that I can use model events to "clear up" my pivot tables, but i'm really unsure how I would implement that.
Can anyone point me in the right direction?
Edit
Below is my current model setup:
namespace App\Models\User;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Scopes\MultiTenantTrait;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, MultiTenantTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['cust_id', 'first_name', 'last_name', 'email', 'status', 'activation_code'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* Boot the model.
*
*/
public static function boot()
{
parent::boot();
static::deleting(function($user)
{
$user->roles()->delete();
$user->supervisors()->delete();
$user->types()->delete();
$user->rates()->delete();
$user->miscs()->delete();
});
}
...
You can add a boot method to your models, like the following:
public static function boot() {
parent::boot();
// This is a deleting event on the model
static::deleting(function($model) {
$model->... //Here your model is still available
// You could add something like this
DB::table('role_user')->where('user_id', $model->id)->delete();
})
}
But you can also extend the delete method in your models:
public function delete() {
DB::table('role_user')->where('user_id', $this->id)->delete();
parent::delete();
}
I have the following tables:
User: [id,username,password]
Producer: [id,user_id,...]
Admin: [id,user_id,...]
I've configured Authentication through User table.
So, if I get logged in when I execute Auth::user() I only can access to the user's fields. However, I would need be able to get producer's fields. I have to say that, a user can be Producer or Admin but not both.
Do you know good documentation or something to do it? Thanks a lot.
You class should hasMany relation to it
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function producers()
{
return $this->hasMany('Producer');
}
}
$user = Auth::user();
print_r($user->producers);
I'm reading Laravel 5 documentation and have some problem with understanding route model binding.
I use example code from here
So, I added line to RouteServiceProvider.php:
public function boot(Router $router)
{
parent::boot($router);
$router->model('user', 'App\User');
}
I added route:
Route::get('/profile/{user}', function(App\User $user)
{
die(var_dump($user));
});
Default Laravel user model is used.
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
}
And I have MySQL table 'users'. When I go to URL http://blog.app/profile/1, I expect to see data for user with ID 1, but I don't understand how to get actual model values. Instead I see:
Is there any special methods for getting model values? Or did I miss something?
I have the same problem here. You just get an empty instance of App\User, since you declared that in your Route::get(). The model is never loaded.
Another way to bind model to the parameter:
Route::bind('user', function($value)
{
return App\User::find($value);
});
The Closure you pass to the bind method will receive the value of the URI segment, and should return an instance of the class you want to be injected into the route.
I have had the same problem.
Look for the "$namespace" var in the "RouteServiceProvider" and try to set it blank :
protected $namespace = '';