1
votes

i need to create three authentication: user, admin, restUser. I managed to create multiple login for user and admin but when try to add login for restUser it returns user form...

this is my code:

app/Teretaneusers.php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Teretaneusers extends Authenticatable
{
use Notifiable;

protected $guard = 'teretaneuser';

/**
 * 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',
];

}

and I create table in MySQL database teretaneusers with column: name, email, password

config/auth.php

    'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
    'admin-api' => [
        'driver' => 'token',
        'provider' => 'admins',
    ],
    'teretaneuser' => [
        'driver' => 'session',
        'provider' => 'teretaneusers',
    ],
    'teretaneuser-api' => [
        'driver' => 'token',
        'provider' => 'teretaneusers',
    ],
],


 'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
       'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admins::class,
    ],

       'teretaneusers' => [
        'driver' => 'eloquent',
        'model' => App\Teretaneusers::class,
    ],

],

Controllers/UserGymController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserGymController extends Controller
 {

 public function __construct()
 {
    $this->middleware('auth:teretaneuser');
 }

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
 public function index()
 {
     return view('teretaneuser');
 }
 }

Controllers\Auth\UserGymLoginController.php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

 class UserGymLoginController extends Controller
{

 public function __construct()
  {
      $this->middleware('guest:teretaneuser');
  }



 public function showLoginForm(){

 return view('auth.teretaneuser-login');
 }



  public function login(Request $request){

 //validate the form data
 $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required|min:6'
    ]

    );

 //attempt to log user in
 if(Auth::guard('teretaneuser')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
           return redirect()->intended(route('userGym.dashboard'));

 }

 return redirect()->back()->withInput($request->only('email','remember'));

 }
 }

auth/teretaneuser-login.blade.php

form class="form-horizontal" method="POST" action="{{ 
  route('userGym.login.submit') }}"

and web.php

Auth::routes();

Route::get('/home', 'HomeController@index');



Route::prefix('admin')->group( function() {
 Route::get('/login', 'Auth\AdminLoginController@showLoginForm')->name('admin.login');
 Route::post('/login', 'Auth\AdminLoginController@login')->name('admin.login.submit');
 Route::get('/', 'AdminController@index')->name('admin.dashboard');


});


Route::prefix('userGym')->group( function() {
 Route::get('/login', 'Auth\UserGymLoginController@showLoginForm')->name('userGym.login');
 Route::post('/login', 'Auth\UserGymLoginController@login')->name('userGym.login.submit');
 Route::get('/', 'UserGymController@index')->name('userGym.dashboard');


});

Can somebody tell me where I'm wrong? When I try login from adress http://localhost/logovanje/public/userGym/login it redirest me to http://localhost/logovanje/public/home

I use Laravel 5.4

I did the same for the admin and it worked.

2
You don't have the providers for teretaneusers - Ashutosh Sharma

2 Answers

0
votes

Most likely you still have a valid session and you got a middleware (possibly RedirectIfAuthenticated) that is coming into play.

0
votes

I think you could use Sentinel for this as it has an authentication package called roles and permissions

Here's a link for its documentation.