Laravel 5.2 Auth Registration page blocked

I’m currently making a webapp with laravel. I’m using the build-in laravel authentication system, and added ranks myself. However, when you are logged in, you can’t reach the registration page. THe thing here is, I want only admins to be able to create new users.

My routes:

Route::group(['middleware' => ['web']], function () {
    Route::get('login', ['as' => 'auth.login', 'uses' => 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85c4f0f1edc6eaebf1f7eae9e9e0f7c5f6edeaf2c9eae2ecebc3eaf7e8">[email protected]</a>']);
    Route::post('login', ['as' => 'auth.login', 'uses' => 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c485b1b0ac87abaab0b6aba8a8a1b684a8aba3adaa">[email protected]</a>']);
    Route::get('logout', ['as' => 'auth.logout', 'uses' => 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcfdc9c8d4ffd3d2c8ced3d0d0d9cefcd0d3dbd3c9c8">[email protected]</a>']);

// Password Reset Routes...
    Route::get('password/reset/{token?}', ['as' => 'auth.password.reset', 'uses' => 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d5d6c7e7e7a627f694e6263797f626161687f4d7e65627a5f687e68794b627f60">[email protected]</a>']);
    Route::post('password/email', ['as' => 'auth.password.email', 'uses' => 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3363524040445c4157705c5d47415c5f5f56417340565d5761564056477f5a5d58765e525a5f">[email protected]</a>']);
    Route::post('password/reset', ['as' => 'auth.password.reset', 'uses' => 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5000312323273f2234133f3e24223f3c3c3522102235233524">[email protected]</a>']);

    Route::get('/home', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="efa780828aac80819b9d8083838a9daf86818b8a97">[email protected]</a>');
});

Route::group(['middleware' => ['CheckAdmin']], function () {
    // Registration Routes...
    Route::get('/register', 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e1f2b2a361d31302a2c3132323b2c1e2d3631290c3b39372d2a2c3f2a37313018312c33">[email protected]</a>');
    Route::post('/register', 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cdde9e8f4dff3f2e8eef3f0f0f9eedceef9fbf5efe8f9ee">[email protected]</a>');
});

My AuthController
<?php

namespace AppHttpControllersAuth;

use AppModelsUser;
use Validator;
use AppHttpControllersController;
use IlluminateFoundationAuthThrottlesLogins;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;
use IlluminateSupportFacadesAuth;

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;

    protected $redirectTo = '/admin';

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

    public function getRegister()
    {
        return redirect('/');
    }

    public function postRegister()
    {
        return redirect('/');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return IlluminateContractsValidationValidator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    /**
     * Returns either ADMIN or USER based on user's rank
     * Returns false if user is not logged in
     */
    public static function getType()
    {
        if (Auth::check()) {
            return Auth::user()->type;
        }
        else {
            return false;
        }
    }
}

I’ve tried many tutorials found online, but neither one of those work for me. Does anyone have a solution for me? Thanks in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this few things to add:

Create Admin middleware with:

public function handle($request, Closure $next)
    {
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return $next($request);
    }
    return redirect('/home');
}

Then add this line to your Kernel.php under $routeMiddleware:
    'admin' => AppHttpMiddlewareAdmin::class,

Put Admin routes in:
Route::group(['middleware' => ['auth', 'admin']], function () {
   //here 
});

This goes inside middleware =>web group

Other routes here:

Route::group(['middleware' => 'web'], function () {
   //here
});

Add this to every controller you have:
public function __construct() { $this->middleware('auth'); }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x