0

I'm trying to build a web app using laravel for the backend! I used the php artisan make:auth command to generate the basic login logic and views. Now I'm trying to redirect users according to the admin field in the users table in the database. The problem is this : it think i don't get the right values from the db so the redirect never works...

here is the routes.php

Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/register', function () {
    return redirect('/step1');
});
route::get('/dashboard', function () {
    return view('admin.dashboard');
});
// after login --> else "welcome"
Route::get('/home', 'HomeController@index');
// register user + agency
Route::get('/step1', 'AgencyController@showStep1');
Route::post('/step1', 'Auth\AuthController@register');
// complete registration step 2 --> 9
Route::get('/step{stepId}', ['middleware' => 'auth', 'uses' => 'AgencyController@show']);
Route::post('/step{stepId}', 'AgencyController@store');});

Here is the code for the redirect (in AuthController.php)

<?php

namespace App\Http\Controllers\Auth;

use App\Agency;
use App\Http\Controllers\Controller;
use App\User; 
use Auth;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request;
use Validator;

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, ThrottlesLogins;

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/';

/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct() {
    $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}

public function login() {
    if (Auth::check()) {

        if (Auth::User()->admin != '1') {

            return redirect('/dashboard');
        } else {
            return redirect('/step1');
        }
    } else {return 'doesnt work :(';}
}

when i run this and login i always get the 'doesnt work' text.

NOTE: the Auth command generated routes :

$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout');

// Registration Routes...
$this->get('register', 'Auth\AuthController@showRegistrationForm');
$this->post('register', 'Auth\AuthController@register');

thanks in advance !

1 Answers1

1

Are you using laravel 4.2? It seems like you're login manually, if you want to login a user, you shoule

if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
    if(Auth::User()->admin != 1){
        #your logic
    }
    return Redirect::intended('dashboard');
}
Hanson
  • 99
  • 8
  • no i'm using laravel 5.2 , and the login is already working because of the generated files , the problems start when i try to redirect after checking for admin field in user table. i think i can't access the right properties of the user. – user3615929 Mar 13 '16 at 10:32
  • Do you add a function name "login()"? – Hanson Mar 13 '16 at 10:36
  • yea i added the function 'login()' in AuthController.php and the route is automaticly generated by the Auth command : $this->post('login', 'Auth\AuthController@login'); – user3615929 Mar 13 '16 at 10:38
  • That's wired,laravel 5.2 should add only one Route "Route::auth()" .And I just check out the code, the login function is in the file name "AuthenticatesUsers", and if you change the login route to the authController::login(),from your code ,it will not login successful – Hanson Mar 13 '16 at 10:43
  • I think I figure it out . When you use function login , first, AuthController::login() will execute, then the AuthenticatesUsers::login(). If you want to redirect, you should rename your login() to whatever you like, such as admin(), and change you AuthController redirect to admin() function. http://php.net/manual/en/language.oop5.traits.php – Hanson Mar 13 '16 at 13:41
  • now i changed the function name from login() to admin() like you said , i provided a route for /admin in routes.php and changed the redirectTo to /admin now the login works ! But... the logic of the redirecting is failing , it always redirects to step1 wether or not the user is admin. any thoughts on that ? thanks for the help. – user3615929 Mar 13 '16 at 14:17
  • you should show me your code , I'm pretty dizzy now @_@ – Hanson Mar 14 '16 at 02:42
  • so i changed the function login() to : `public function admin() { if (Auth::check()) { if (Auth::User()->admin != '1') { return redirect('/admin.dashboard'); } else { return redirect('/agency.step1'); } } else {return 'doesnt work :(';} }` and i provided a route so the function will load `route::get('/admin', 'Auth\AuthController@admin');` and i set the $redirectTo : `protected $redirectTo = '/admin';` – user3615929 Mar 14 '16 at 08:27