Support the ongoing development of Laravel.io →
Authentication
Last updated 2 years ago.
0

All that's in the current AuthController is this code:

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {

	/*
	|--------------------------------------------------------------------------
	| Registration & Login Controller
	|--------------------------------------------------------------------------
	|
	| This controller handles the registration of new users, as well as the
	| authentication of existing users. By default, this controller uses
	| a simple trait to add these behaviors. Why don't you explore it?
	|
	*/

	use AuthenticatesAndRegistersUsers;

}
What exactly do I add now to login
0

Yes, still looking for example on how to use that trait in auth controller.

0

It is used in this line: use AuthenticatesAndRegistersUsers;. That line includes everything that is in the Trait. So if you create an route to AuthController@getLogin the getLogin method in the Trait will be resolved.

0

I use a userid rather than email as part of a login. So I would actually modify:

Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

The actual trait file to use a userid rather than email, correct?

0

No, you would override the trait method inside your auth controller.

0

Something like:

public function mylogin(Request $request)
{
$mylog = new AuthenticatesAndRegistersUsers();
$mylog->postLogin(Request $request)
}
0

No you need to override the postLogin() method so it will look like this

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers;

    public function postLogin()
    {
        // Perform the authentication based on your requirements
    }

}

In this setup you will use the methods as definde above and for the undefind ones you will use laravels predefined ones.

Last updated 9 years ago.
0

In previous L5 before the background trait my login looked like:

public function postLogin(LoginRequest $request)
	{
		
           $tvar = $request->input('userid');
            $pw = $request->input('password');
            if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
			{
                    
                    
                    return redirect('petlist');
			}
            else
                {
                    echo ' not logged in';
                }
                
		
	}


How do I redo that code to work with the trait?
Sorry, I knew about traits, but never used traits.

Last updated 9 years ago.
0

You don't need to do anything.

0

I got it working, I only changed email to userid in AuthenticatesAndRegistersUsers.php. The only problem I see with this is an update will change it back to it's original, thus I will probably have to make a backup of this file. Any thoughts on this? Also can I just make a regular auth page/controller like L5 had Nov 25, 2014 before the use trait.
And thanks to all of you.

Last updated 9 years ago.
0

This is all you need to do:

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers;

    public function postLogin(LoginRequest $request)
    {

		$tvar = $request->input('userid');
		$pw = $request->input('password');
		
		if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
		{
			return redirect('petlist');
		}

		echo ' not logged in';

    }
}

You should never change code in the vendor directory, this will be overridden.

Check out this Laracast video: https://laracasts.com/lessons/whats-a-trait it will help you understand traits and then you will understand why the above code should work.

0

Worked, only a minor change
From:

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers;

    public function postLogin(LoginRequest $request)
    {

        $tvar = $request->input('userid');
        $pw = $request->input('password');

        if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
        {
            return redirect('petlist');
        }

        echo ' not logged in';

    }
}

To:

<?php namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers;

    public function postLogin(Request $request)
    {

        $tvar = $request->input('userid');
        $pw = $request->input('password');

        if ($this->auth->attempt(['userid' => $tvar, 'password' => $pw]))
        {
            return redirect('petlist');
        }

        echo ' not logged in';

    }
}

Request instead of LoginRequest, the trait has Request now. Thanks a bunch

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jimgwhit jimgwhit Joined 13 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.