Auth::logout() on missing remember_token database field - php

I was simply trying to log out admin user from the Dashboard while I used this code for doing it:
Routes.php
Route::get('logout',array('uses'=>'AuthController#LogOut'));
AuthController.php
class AuthController extends Controller{
public function LogOut(){
Auth::logout();
return Redirect::to('login');
}
}
while it is giving me such error for log out
as i don't have such field in Database, and also it is not added to Database while Migration also.

The error is most likely occurring because the remember_token field is required by the Auth to be present in the users table. So you should add a remember_token field (likely a string field) in your users migration table and migrate it. Then, you should create a user, log in the user and then try logging out. Hopefully, doing this will solve your problem.

I guess you've run an update with composer recently and have upgraded the Laravel core. You'll need to perform a few steps to upgrade to the newest Laravel version as documented in the Laravel upgrade info here: http://laravel.com/docs/upgrade#upgrade-4.1.26
Laravel 4.1.26 introduces security improvements for "remember me"
cookies...change requires the addition of a new remember_token
column to your users (or equivalent) database table.
You'll also need to update your User class with these methods if you're using Eloquent:
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}

Related

Gate not allows user while he has already that permission

I'm working with Laravel 8.5 and I wanted to develop my own ACL.
So I made this ManyToMany relationship between Permission & User models:
User.php:
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
Permission.php:
public function users()
{
return $this->belongsToMany(User::class);
}
Then I have inserted this custom permission into permissions table:
And then inserted this also into the pivot table pemrission_user:
Then at web.php, I tried this:
Route::get('/', function () {
$user = auth()->user();
// dd($user->permissions()->get());
if(Gate::allows('edit-user')){
dd(2);
}else{
dd(1);
}
});
So I tried checking if the logged in user has the permission edit-user, then shows 2 as result but now it returns 1 somehow, meaning that user has not this permission!
However if I uncomment dd($user->permissions()->get());, I can see this:
So as it shows user already has this edit-user permission but I don't why the Gate does not authorize user in this case.
So if you know, I beg you to help me cause I really don't know how to solve this...
You need to define edit-user for your Gate as well because your permission model means nothing to the Gate at the moment.
Gate::define('edit-user', function (User $user) {
return $user->permissions()->whereName('edit-user')->exists();
});
More information can be found here: https://laravel.com/docs/8.x/authorization#writing-gates
Otherwise, you can use policies:
class UserPolicy
{
public function update(User $user)
{
return $user->permissions()->whereName('edit-user')->exists();
}
}
And then to allow the user:
$user->can('update', User::make());
More information about policies can be found here: https://laravel.com/docs/master/authorization#creating-policies
There's also an open source package called laravel-permission made by Spatie that you can have a look at to learn more.

Cant access Auth status in Model

I am trying to access my auth status in on a Model, i used an eloquent mutator to add a field to my model.
//use Illunimate\Facades\Support\Auth;
protected $appends = ['value'];
public function getValueAttribute(){ return Auth::user()->id()}
That is the code,
but its returning false even when logged in
You write the code in the wrong way. you miss the user() and then you use parentheses after id your code must be like this:
public function getValueAttribute(){ return Auth::user()->id;}
Corrected version of your code
public function getValueAttribute(){ return Auth::user()->id}
from your view you could check first to avoid crashes :
if (Auth::check()) {
// The user is logged in...
{{Auth::user()->id}}
}else{
// The user is NOT logged in...
}
Your code seems correct except its missing ;
So try this
use Illuminate\Support\Facades\Auth;
public function getValueAttribute(){ return Auth::user()->id();}
If that doesn't work try this alsophp artisan make:auth and then php artisan migrate
This will reset auth and migratation .
Hope this helps

Laravel 5.4 Eloquent Relationship Issue

I need to clear my doubt about Eloquent Relationships. I have 2 models User (which comes with Laravel) and Other is Role which I created.
in migration, I added role_id as an additional column as I want every user must have a role now I want to retrieve a user role based on user's id so, I created a public function named roles inside the User Model.
public function roles(){
return $this->belongsTo(Role::class);
}
now when i try to run the query like this.
App\User::find(1)->roles;
it won't return any result, just empty screen but when I change it to
public function role(){
return $this->belongsTo(Role::class);
}
after that i run code
App\User::find(1)->role;
now it returns the exact row where the user with id 1 has a proper role. it's confusing why with the roles() function it's not working but with the role() function it's working.
Sorry, If this question is already posted you can redirect me to that question.
Thank You!
You have to specify the foreign key
public function roles()
{
return $this->belongsTo(Role::class, 'role_id');
}
However, calling it role() is more accurate, since your are assuming that a User can only have one role.

Laravel 5.2 reset password with another column name (not email)

I'm taking over a project from the other, the old database schema uses "username" instead of "email". It causes the conflict when I add reset password functionality.
public function getEmailForPasswordReset()
{
return $this->email;
}
Is there some way to customize that trait to use another column name over "email"?
How about this way?
https://laravel.com/docs/5.0/schema#renaming-columns
Rename the old column to email. It's a lot better for the future rather than making a new attribute from the old column :)
I add a new accessor to User Model:
public function getEmailAttribute()
{
return $this->attributes['username'];
}
Hope someone got similar issues could use it. Welcome to better solution.

Laravel Cashier saving to stripe but not DB

Laravel Cashier is not saving stripe data to my DB after a user is created.
When I create a new user and then try to assign a subscription() to that user (or any other user for that matter) the data is being sent to Stripe approriately, as I can see the customer id, plan chosen, etc... But my database user stripe columns do not get updated in my database.
When I remove the code to create() a user and just subscribe a user, the data persists to the database like it should. It only doesn't work after I've created a new user THEN try to subscribe any user.
I'm using Jeffrey Way's Model Validation Package.
$user = $this->userModel->create($input);
if ($user->hasErrors()) {
throw new \Exception($user->getErrors());
}
$this->createStripeSubscription($user->id, $input['creditCardToken']);
private function createStripeSubscription($user_id, $token) {
//$this->userModel->find(1)->subscription('monthly')->create($token); // Doesn't work either
$this->userModel->find($user_id)->subscription('monthly')->create($token);
}
I had similar problem with Ardent validation in Confide.
Overriding saving method of BillableTrait in user model solved the issue.
class User extends ConfideUser implements BillableInterface {
use BillableTrait;
public function saveBillableInstance()
{
$this->forceSave();
}
}
The problem was a conflict with Jeffrey Way's Validation Model package. Or at least the way I was trying to implement it while using Cashier. I decided to abstract out my Validation in it's own class and remove the Validation Model package. All is good.

Categories