简体   繁体   中英

Storing user details in session but without login him yet in Laravel

Is it possible when user type his login username and password to store his details and redirect him to another page where I will query database for addition information which he provide before to log him? Consider this scenario:

  1. User enter username/password
  2. If they are correct store this user information and redirect to another page ( not logged yet )
  3. Query database for addition information about this user

This is what I have so far

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

public function loginSubmit() {

    $validatorRules = array(
        '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']));
    }

    Session::put('user', ['user_id' => $user->user_id]);
    return Redirect::to('/users/auth/' . $user->user_id . '?_token=' . csrf_token())->with('message_success', '.');
}

public function loginAuth() {
    Session::get('user', ['user_id' => $user->user_id]);
    $key = DB::table('users')->select('key')->where('user_id', '=', $data->user_id)->first();
    return View::make('site.users.auth', [
        'key' => $key
    ]);
}

Or there is another way to do this? This portion of source gave simple error

production.ERROR: exception 'ErrorException' with message 'Undefined variable: user'

On session get function

Session::get('user', ['user_id' => $user->user_id]);

You can fix your code like this:

public function loginAuth()
{
    $data = Session::get('user');
    $key = DB::table('users')->select('key')->where('user_id', '=', $data->user_id)->first();

    return View::make('site.users.auth', [
        'key' => $key
    ]);
}

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