1

I am using Laravel 5.2.

I added a new field to the users table called username and I included it in the view and AuthController.php

I am also doing a check to see if the user is going to use a reserved slug for his username, if yes, I throw an error, if the username is good, I save in the database.

I am checking against 2 reserved slugs banner and image - that's all I have reserved.

If I remove the check, the data gets saved in the database normally, including the reserved slugs. So I must do the check.

However, when I apply the check, I get this error

ErrorException in SessionGuard.php line 439:

Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, string given, called in C:\xampp\htdocs\socialnet\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 63 and defined

This is my code in AuthController after I've modified it

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
        'username' => 'required|alpha_num|unique:users,username',
    ]);
}

protected function create(array $data)
{
    $check = $this->slugCheck($data['username']);

    if($check['status'] == 'nok') {
        return 'this username is a reserved name';
    } else {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'username' => $data['username'],
        ]);
    }
}

public function slugCheck($slug){

    $slug = strtolower($slug);

    // is the slug in the banned words list
    //
    if(array_search($slug, array('','banner','image'))){
        return array('status' => 'nok', 'message' => 'This slug is on a list of reserved names');
    }

    $user = User::where('username', '=', $slug)->first();

    if($user){
        return array('status' => 'nok', 'message' => 'This slug is already taken');
    } else {
        return array('status' => 'ok', 'message' => 'Slug is unique');
    }

}

If I dd($check) after declaring the $check variable and choosing a reserved name, I get the correct response.

array:2 [▼
  "status" => "nok"
  "message" => "This slug is on a list of reserved names"
]
4
  • 1
    make sure you added username to User.php $fillable array.
    – ClearBoth
    Jul 15, 2016 at 22:36
  • What does your User model looks like?
    – manniL
    Jul 15, 2016 at 23:30
  • @ClearBoth It's already there. @manniL it's a simple model with $fillable and $hidden
    – Halnex
    Jul 16, 2016 at 14:07
  • 1
    that error is straight forward. when $check['status'] == 'nok' is true it will not return instance of the class. instead it will return string. you can solve this by adding your check in some middleware
    – ClearBoth
    Jul 16, 2016 at 20:48

1 Answer 1

5

That is because you're returning a string and the class is expecting an instance of Authenticatable.

Moving your validation in a middleware is your option. Create a middleware that checks if the username is a 'Slug' and only procede to create() method when validation passes otherwise, return the string: 'The username is a reserved name'

Now you can clean up the AuthController by removing the slugCheck method and you can always certain that whenever create() method was called, that username isn't Slug.

You can check the laravel documentation

1
  • I tried this, however adding the middleware to the RegisterController.php's constructor did not work. I had to add my middleware to the route Route::post('register', [RegisterController::class, 'register'])->middleware('whitelisted.register'); for it to do what I wanted.
    – Kush
    Mar 15, 2022 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.