在会话中存储用户详细信息,但在Laravel中还没有登录他



是否有可能当用户输入他的登录用户名和密码存储他的详细信息,并将他重定向到另一个页面,我将查询数据库的其他信息,他提供之前登录他?考虑这个场景:

  1. 用户输入用户名/密码
  2. 如果他们是正确的存储此用户信息并重定向到另一个页面(尚未登录)
  3. 查询该用户的附加信息

这是我到目前为止写的

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
    ]);
}

或者有其他方法可以做到这一点?这部分源代码给出了简单的错误

production.ERROR: exception 'ErrorException' with message 'Undefined variable: user'
On session get function
Session::get('user', ['user_id' => $user->user_id]);

你可以这样修改你的代码:

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
    ]);
}

相关内容

最新更新