Asked  4 Months ago    Answers:  5   Viewed   38 times

I use manual login function in Laravel 5.5. Stuck in login. and check all(5 relevant ) Stack links and didn't find any clue on it.

Achievement is once user get registered, automatically sign in that user.


Error is

"Type error: Argument 1 passed to IlluminateAuthSessionGuard::login() must implement interface IlluminateContractsAuthAuthenticatable, string given, called in Server/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 294 ◀"

if ($validator->fails()) {

//            $messages = $validator->messages();

            return Redirect::to('register')
                ->withErrors($validator)
                ->withInput();

        } else {

            $email = Input::get('email');
            $user = new user;
            $user->name     = Input::get('name');
            $user->email    = Input::get('email');
            $user->password = Hash::make(Input::get('password'));

            $user->save();
//            $userMail = $user->find($email);
            $userMail = User::where('email','=',$email)->first();
            Auth::login($userMail->email, TRUE);

am i doing anything wrong. Please guide me.

 Answers

78

Login function needs user of type Authenticatable and you just given email which is string thats why you get this error, Either use Auth::loginUsingId($id);

 $user = User::where('email','=',$email)->first();
 Auth::loginUsingId($user->id, TRUE);

Or just

Auth::login($user);
Friday, August 13, 2021
 
lechup
answered 4 Months ago
22

You can use this to loop errors

@if(count($errors) > 0)
         <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
         </div>
    @endif
Wednesday, March 31, 2021
 
Hexaholic
answered 9 Months ago
61

I ended up coding my own Auth system...

Using session() and Input()

Saturday, May 29, 2021
 
Noob_Programmer
answered 7 Months ago
82

You are almost there, just a few changes more, Events and Listeners for authentication have changed a little in Laravel 5.2: the handle method in UpdateLastLoginOnLogin should have just an event as parameter

namespace AppListeners;

use CarbonCarbon;
use Auth;

class UpdateLastLoginOnLogin
{
    public function handle($event)
    {
        $user = Auth::user();
        $user->last_login_at = Carbon::now();
        $user->save();
    }
}

And for the EventServiceProvider you specify the listeners like this :

protected $listen = [
    'IlluminateAuthEventsLogin' => [
        'AppListenersUpdateLastLoginOnLogin@handle',
    ],
];
Saturday, May 29, 2021
 
EastSw
answered 7 Months ago
78

The controller is not the right place to check if a user is authenticated or not. You should use a middleware for that. To get info on what a middleware is check here

Let's see how you can use the default Laravel's auth middleware for this purpose:

First of all get rid of your AdminBaseController and use only AdminController

Then you have to check that the auth middleware is enabled in the file appHttpKernel.php

You should have the line:

protected $routeMiddleware = [
    'auth' => AppHttpMiddlewareAuthenticate::class,

This means that the middleware is active and usable for your routes.

Now let's go inside the middleware class in appHttpMiddlewareAuthenticate.php to specify the middleware's behaviour :

//this method will be triggered before your controller constructor
public function handle($request, Closure $next)
{
    //check here if the user is authenticated
    if ( ! $this->auth->user() )
    {
        // here you should redirect to login 
    }

    return $next($request);
}

Now the only thing left to do is to decide for what routes you should apply the middleware. Let's suppose you have two routes that you want to be only accessible from authenticated users, you should specify to use the middleware for these two routes in this way:

Route::group( ['middleware' => 'auth' ], function()
{
    Route::get('admin/index', 'AdminController@index');
    Route::get('admin/ajuda', 'AdminController@ajuda');
});
Monday, September 27, 2021
 
Angshuman Agarwal
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share