Good day. I'm trying to use laravel auth . But i'm bit confused.
I'm already Login and Register.
Here is my login process
public function index(){
if(!Auth::check()){
return view::make("Login");
}else{
dd(Auth::user());
}
}
function loginproses(Request $request){
extract(Maincontroller::populateform());
$a = Loginmodel::where('username',$username)
->where('password',md5($password))
->first();
if($a === NULL){
return redirect('/')->with('status', 'Username atau Password salah');
}else{
$userdata = array(
'username' =>$username
);
if(Auth::attempt($userdata)){
return redirect('/');
}
}
}
I'm using Userlogin as my table so i change this
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Loginmodel::class,
'table' => 'Userlogin'
],
but the problem after the login. I get this error
Argument 1 passed to
Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an
instance of Illuminate\Contracts\Auth\Authenticatable, instance of
App\Loginmodel given, called in
D:\xampp\htdocs\sitestarter\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php
on line 380 and defined
how can i fix it ?
when using google for searching how to login in laravel i always see Laravel auth. Then do i really need to use Laravel Auth ? and do really need to use the auth controller ?
Is your Loginmodel extend Authenticatable like
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
class Loginmodel extends Authenticatable
{
}
Related
I have a class User that extends
<?php
namespace App;
class User extends \Cartalyst\Sentinel\Users\EloquentUser
{
public function chalets(){
return $this->hasMany('App\Chalet');
}
}
and i have Chalet Class
class Chalet extends Model
{
protected $fillable = [
'name', 'description',
];
public function user(){
return $this->belongsTo('App\User');
}
}
And i have method to add chalet by user :
public function postCreateChalet(Request $request){
$chalet = new Chalet([
'name' => $request->input('name'),
'description' => $request->input('description')
]);
Sentinel::getUserRepository()->setModel('App\User');
$user = Sentinel::getUser();
$user->chalets()->save($chalet);
return ('chalet has created');
}
and its give me an error :
BadMethodCallException
Call to undefined method Cartalyst\Sentinel\Users\EloquentUser::chalets()
Is it a right way to extend User class ?
I have searched for ways to extend the User class. I found this question:Model Inheritance in Laravel didn't help me though.
I'm using Laravel 5.7
The exception you're getting indicates Sentinel is still referring to the default stock Sentinel's EloquentUser model. Make sure you point to your extended user model with the published Sentinel configurations.
Run the below command
php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
Open up the published config file at 'config\cartalyst.sentinel.php'
Modify it from the below content:
'users' => [
'model' => 'Cartalyst\Sentinel\Users\EloquentUser',
],
to:
'users' => [
'model' => 'App\User',
],
For more information, refer to https://github.com/cartalyst/sentinel/wiki/Extending-Sentinel
You won't need the following line after you configured it via config:
Sentinel::getUserRepository()->setModel('App\User');
I've created a multi auth test in Laravel 5.4. It's using custom middlewares, custom Eloquent providers, etc. The auth flow is working, I can login in both ways. But if the user is signed in, in the home controller when I want to check the user with Auth::user() or Auth::guard()->user(), it's empty. The Auth::guard() is empty as well. But I don't understand, why?! It should contains the signed in user instance, shouldn't it?
Also the $request->getUserResolver() says that the guard is null... o.O
What did I do wrong?
Here it is my test repo, if you want to check my code.
Thank you in advance!
Edit 1:
In the \app\Http\Controllers\Employee\HomeController.php the Auth::guard()->user() and the Auth::user() are empty.
namespace App\Http\Controllers\Employee;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{
public function __construct(Request $request)
{
$this->middleware('auth.employee:employee');
}
public function index(Request $request)
{
$users[] = Auth::user();
$users[] = Auth::guard()->user();
$users[] = Auth::guard('employee')->user();
dd($users);
return view('employees.home.index');
}
}
Auth::shouldUse(your_guard_name);
call this in your login function
Change the driver name in the config/auth.php as..
'employees' => [
'driver' => 'eloquent',
'model' => App\Models\UserEmployee::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
I've using Sentinel for user authentication in Laravel.
I've followed this guide to login using username but it doesn't seem to work!
I have a test route to test the login which is as follows:
Route::get('/test-login', function()
{
Sentinel::authenticate([
'login' => 'nvaughan84',
'password' => 'password',
]);
//check login details
if($user = Sentinel::check())
{
print_r($user);
}
else
{
echo 'Not Logged';
}
});
I've created a Users model which extends SentinelUser as follows:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cartalyst\Sentinel\Users\EloquentUser as SentinelUser;
class Users extends SentinelUser
{
protected $fillable = [
'email',
'username',
'password',
'last_name',
'first_name',
'permissions',
];
protected $loginNames = ['username'];
}
And I've updated my sentinel config:
'users' => [
'model' => 'App\Users',
],
If I add 'username' to the $loginNames array in the Sentinel Class it works but not using my Users class which extends the Sentinel class.
Can someone tell me what I might be doing wrong?
Thanks :)
Solved it.
I stupidly still had
use Cartalyst\Sentinel\Native\Facades\Sentinel;
at the top of my routes.php file and so it was using this instead of my new Users class!
I have created separate table called subscribers in mysql changed config/auth.php settings to 'model' => App\Subscribers::class, 'table' => 'subscribers'.
I have login form on home page, that submits to the home page.
so in routes i have below
Route::get('/', function () {
return view('home');
});
Route::post('/', 'LoginController#validate');
my LoginController
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
public function validate()
{
// attempt to do the login
$auth = Auth::attempt(
[
'email' => strtolower(Input::get('email')),
'password' => Hash::make(Input::get('password'))
]
);
if ($auth) {
return Redirect::to('dashboard');
}
}
}
when i login i get below error
Declaration of App\Http\Controllers\LoginController::validate() should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)
You can't use 'validate' as a name for a function. It will conflict with:
App\Http\Controllers\Controller::validate
Also add an 'else' to your if statement so if your authentication fails you can redirect the user back to the login screen for example.
I'm using Sentry 2.1 for authentication.
My User Model:
<?php namespace App\Models;
use Eloquent;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends \Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface {
/* Sentry Defaults omitted for brevity */
public function children()
{
return $this->hasMany('App\Models\Children');
}
public function getFullNameAttribute()
{
return trim($this->attributes['first_name'] . ' ' . $this->attributes['last_name']);
}
}
My login function:
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($credentials))
{
$user = Sentry::authenticate($credentials, $remember);
return Redirect::to('/');
}
The reason why I'm using Auth::attempt and then Sentry::authenticate is because I am migrating from an old database to a new one, so I attach a hook/listener on auth.attempt so I can process checking for old password.
Now, after I'm logged in, I can't access the full_name accessor attribute.
$user = Sentry::getUser();
echo $user->full_name; // results in NULL
I think I'm missing a small thing here but I just can't find that missing piece.
Thanks for the help!
did you edit config of Sentry (dir: /app/config/packages/cartalyst/sentry/config.php") ??
from
'model' => 'Cartalyst\Sentry\Users\Eloquent\User',
to
'model' => 'App\Models\User',