Invisnik Laravel Steamauth login errors - laravel

So I've just created a project in laravel 7 and added the invisnik steamauth to it, I haven't added the actual auth package yet so that may be why I'm having the issue, however I don't want to lose what I've done.
I ran this migration:
Schema::create('users', function (Blueprint $table) {
$table->id("steamid");
$table->string('username');
$table->string('avatar');
$table->string('rank');
$table->rememberToken();
$table->timestamps();
});
Then I edited the create function in the steamauth controller to this:
return User::create([
'username' => $info->personaname,
'avatar' => $info->avatarfull,
'steamid' => $info->steamID64,
'rank' => 0
]);
When I go to login, I get this error:
SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value (SQL: insert into `users` (`updated_at`, `created_at`) values (2020-05-06 18:32:42, 2020-05-06 18:32:42))
EDIT: Here's the User.php model
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',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Thanks.

Related

Unable to get the api token field to fill in Laravel 5.8

I am trying to complete an api registration system using Laravel 5.8. At the minute every time I run a registration the api_token field remains null. I am pretty certain I have followed the instructions correctly but I am still running into the error.
This was the migration I created.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::table('users', function ($table) {
$table->string('api_token', 80)->after('password')
->unique()
->nullable()
->default(null);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
and the following is the create method within the Registration Controller.
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}
It is almost like the "api_token" field is being completely ignored during the process. All other fields are completed during the process.
first let me see: did you add 'api_token' in the user $fillable array in your User model ?

Laravel 5.8 cannot generate api_token when new user register

I am playing around with Laravel Authentication.
On a freshly created Laravel app with Composer, I followed literally the instructions until this point (included)
https://laravel.com/docs/5.8/api-authentication#generating-tokens
When I register a new user, however, the api_token field is NULL.
What else do I need to do in order to start generating the api token when a user registers??
Create method in RegisterController:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}
Migration (I called it Token) updating the users table:
class Token extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 80)->after('password')
->unique()
->nullable()
->default(null);
});
}
App\User model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\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',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
In your user model add 'api_token' to your fillables
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
'api_token'
];
After creating migration , run the migrate Artisan command:
php artisan migrate
If you are using the Homestead virtual machine, you should run this command from within your virtual machine:
php artisan migrate --force

Eloquent updateOrCreate error: General error: 1364 Field doesn't exist

I am trying to use the updateOrCreate function and it was working earlier however when I added a new row called user it has stopped working even though my updateOrCreate look like this:
SystemCoreStats::updateOrCreate(['id' => $systemId],['type' => $type, 'stat_build_store' => $val, 'user' => $user->id]);
I don't understand what I am doing wrong.
Basically, this error means the user that your trying to updateOrCreate doesn't exist.
try to check in your Model if this is present:
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'column_name',
'column_name',
'column_name',
];
in your case
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'type',
'stat_build_store',
'user',
];
and try to check in your db if the column exist.
Can you post more of your code?

Override attempt auth method in laravel 5

I'm having a big problem with Auth:attempt method in laravel 5.2, and I wish I can override it.
I have the class users, by default, but doesn't containt the attribut 'email', this last one is containing in an other class user_contacts, with relation one to one with users.
Can I override this function? because by default, it takes 'email', which is not in users in my case
I tried to make that in the model, but not working:
lass User extends Authenticatable
{
public function __construct(array $attr = array())
{
parent::__construct($attr);
$this->email = $this->user_contacts()->email;
}
/**
* 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',
];
public function user_contacts()
{
return $this->hasOne('App\UserContact', 'user_id', 'id');
}
}
It just says that the email field is not defined.
Any suggestion plz?
Thanks alot.
Manually authenticating user https://laravel.com/docs/5.3/authentication#authenticating-users
You can use any field you want, 'email' field is just the default.
Something like this will work perfectly fine:
if(Auth::attempt(['username' => $username, 'password' => $password])){
return redirect()->intended('admin');
}

Laravel 5.2 - Registration

I'm building a simple user registration on Laravel 5.2 and it's really driving me crazy.
I started from the default Laravel built-in registration system and added some extra fields.
My case involves the following files:
routes.php
// Registration routes...
Route::get('auth/register', function(){
$organisations = Organization::all();
return view('auth.register', ['organizations' => $organisations]);
});
Route::post('auth/register', 'Auth\AuthController#postRegister');
User.php
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['organization_id', 'team_id', 'lastname', 'firstname', 'login', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* Get the user's organisation.
*/
public function organization(){
return $this->belongsTo('App\Organization');
}
/**
* Get the user's team.
*/
public function team(){
return $this->belongsTo('App\Team');
}
}
Organization.php
class Organization extends Model
{
protected $table = 'organizations';
public $timestamps = false;
protected $fillable = ['name'];
/**
* Get the authors for the organization.
*/
public function users()
{
return $this->hasMany('App\Users');
}
/**
* Get the teams for the organization.
*/
public function teams()
{
return $this->hasMany('App\Teams');
}
}
Team.php
class Team extends Model
{
protected $table = 'teams';
public $timestamps = false;
/**
* Get the team's organisation
*/
public function organisation()
{
return $this->belongsTo('App\Organisation');
}
/**
* Get the authors for the team.
*/
public function users()
{
return $this->hasMany('App\Users');
}
}
AuthController.php
/
/.......
/......
/
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'login' => 'required|max:255',
'organization' => 'required|max:255',
'team' => 'required|max:255',
'password' => 'required|confirmed|min:4',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'login' => $data['login'],
'organization_id' => $data['organization'],
'team_id' => $data['team'],
'password' => bcrypt($data['password']),
]);
}
I won't post my view code, my inputs are correct.
My problem is that I can store only ONE new user.
What's happening is:
my users table is empty
I fill up my form and then submit it to create my first user
it works (yay), I am correctly redirected to my home page
I want to create a second user
The new user isn't stored in my table
I am redirected to the home page however
If I delete my user entry in my table (so it means I empty my table), then I can create a new user again. But I can ALWAYS have one and only one entry. I can't add more.
Just out of curiosity I tried this in my routes file:
Route::get('/lol', function () {
return User::create([
'firstname' => 'what',
'lastname' => 'the',
'login' => 'f***',
'organization_id' => 1,
'team_id' => 4,
'password' => 'blabla',
]);
});
and of course it works like a charm each time I am calling the route /lol
So what the hell is going on in this AuthController? Am I out of my mind?
Alright the problem is that after my registration Laravel automatically logs the user in -.- (which is quite nice actually but I didn't know it). So now my problem is solved.
I went back to Laravel 5.1 (I was under 5.2), the authenticate and register system is better handled, in my opinion.

Resources