简体   繁体   中英

Trying to create two way login form in Laravel - 'Array to string conversion'

I have normal login form which work great. Now I'm trying to make second simple step when user enter his password and user name and if they are correct to redirect him to new page where he must enter pass phrase in order to continue.

I'm not sure if this is correct what I have make so far. This is my route:

Route::get ('/users/login', ['uses' => 'UsersController@login', 'before' => 'guest']);
Route::post('/users/login', ['uses' => 'UsersController@loginSubmit', 'before' => 'guest']);

// new 
Route::get('/users/auth', ['uses' => 'UsersController@loginAuth', 'before' => 'guest']);
Route::post('/users/auth', ['uses' => 'UsersController@loginSubmitAuth', 'before' => 'auth|csrf']);

This is my auth.blade.php

{{ Form::open(['class' => 'form-horizontal']) }}

    <div class="form-group"> {{ Form::textarea('key', ['class' => 'form-control', 'id' => 'key', 'autocomplete' => 'off']) }} </div><br/>

    <hr />
    <div class="row">
        <button type="submit" class="btn btn-primary col-xs-4 col-xs-offset-4">Login</button>
    </div>
    <hr />

{{ Form::close() }}

This is my controller

public function login() {
    return View::make('site.users.login');
}

public function loginSubmit() {
    $validatorRules = array(
        'captcha'  => 'required|captcha',
        'username' => 'required|alpha_dash',
        'password' => 'required|min:6'
    );

    Input::merge(array_map('trim', Input::all()));
    $validator = Validator::make(Input::all(), $validatorRules);

    if ($validator->fails()) {
        return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    $user = User::where('username', Input::get('username'))->first();
    if (!$user) {
        $validator->messages()->add('username', 'Invalid login or password.');
        return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    if (!Hash::check(Input::get('password'), $user->password)) {
        $validator->messages()->add('username', 'Invalid login or password.');
        return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    //$user->last_login = \Carbon\Carbon::now();
    //$user->save();
    //Session::put('user', ['user_id' => $user->user_id]);
    return Redirect::to('/users/auth');
}
public function loginAuth() {
    return View::make('site.users.auth');
}
public function loginSubmitAuth() {
    $validatorRules = array(
        'key'  => 'required',
        'captcha'  => 'required|captcha'
    );

    Input::merge(array_map('trim', Input::all()));
    $validator = Validator::make(Input::all(), $validatorRules);

    if ($validator->fails()) {
        return Redirect::to('/users/auth')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    $user = User::where('key', Input::get('key'))->first();
    if (!$user) {
        $validator->messages()->add('key', 'Invalid Key.');
        return Redirect::to('/users/auth')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    $user->last_login = \Carbon\Carbon::now();
    $user->save();
    Session::put('user', ['user_id' => $user->user_id]);
    return Redirect::to('/');
}

Current error is 'Array to string conversion'

Any help is appreciated. Thank's

The issue of second parameter in Form::textarea you need to pass null to make it empty in second parameter and array in third parameter like,

<div class="form-group">
  {{ Form::textarea('key',null, ['class' => 'form-control', 'id' => 'key', 'autocomplete' => 'off']) }}
</div><br/>

You can refer to Creating a Textarea Input Field

Can you try if this fixes your problem:

{{ Form::textarea('key', null, ['class' => 'form-control', 'id' => 'key', 'autocomplete' => 'off']) }}

It's this way because the value argument comes second and it is mandatory in difference with the third one which is optional

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM