Laravel 5注册后不会自动登录

时间:2016-01-01 08:41:19

标签: php laravel

如何在Laravel 5中完成未在登记后自动登录。 我使用here

中的基本身份验证

我还没有从AuthController.php重新编写postRegister()代码,只使用Laravel的基础而不改变任何内容。

这是我的AuthController.php

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

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

这是我的路线

Route::get('auth/register', ['middleware' => 'auth', 'uses' => 'Auth\AuthController@getRegister']);
Route::post('auth/register', ['middleware' => 'auth', 'uses' => 'Auth\AuthController@postRegister']);

这是我的注册表格

<form method="POST" action="{{ url('auth/register') }}">
    {!! csrf_field() !!}

    <div>
        Name
        <input type="text" name="name" value="{{ old('name') }}">
    </div>

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password">
    </div>

    <div>
        Confirm Password
        <input type="password" name="password_confirmation">
    </div>

    <div>
        <button type="submit">Register</button>
    </div>
</form>

谢谢!

7 个答案:

答案 0 :(得分:1)

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function postRegister(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    // Commenting this line should help.
    Auth::login($this->create($request->all())); 

    return redirect($this->redirectPath());
}

这是您正在寻找的功能。只需根据您的意愿编辑/覆盖它。

答案 1 :(得分:1)

对于任何寻找 Laravel 5.2 解决方案的人来说,将其放在AuthController.php文件中,请记住在控制器顶部包含/使用请求

应用程序/ HTTP /控制器/认证/ AuthController.php

use Illuminate\Http\Request;
...

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{   
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );  
    }   

    $this->create($request->all());

    return redirect($this->redirectPath());
} 

答案 2 :(得分:0)

为什么不在你的postRegister函数中只有Use URLEncoder.encode()(成功时,即在验证器通过后),然后Auth::logout();其他一些页面,也许你想在能够之前执行电子邮件验证或其他内容登录。

答案 3 :(得分:0)

/**
* Handle a registration request for the application.
*
* @param  \Illuminate\Http\Request  $request
* @return \Illuminate\Http\Response
*/
public function postRegister(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::login($this->create($request->all())); // Comment this line

    return redirect('/'); // Change the redirection
}

答案 4 :(得分:0)

打开您的注册控制器并覆盖注册方法,您可以在vendor / laravel / framework / src / Illuminate \ Foundation \ Auth \ RegistersUsers中找到它。

use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
  use RegistersUsers;
  protected $redirectTo = '/'; // redirect after user registration

  public function register(Request $request)
  {
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);   // comment this line to stop autologin after registration

    return redirect($this->redirectPath());
  }
}

答案 5 :(得分:0)

您可以覆盖postRegister()方法以禁用自动登录,login()仅在活动字段为真时调用。

您的postRegister()将类似于:

public function postRegister(Request $request)
{

    // validation

    // insert record in user entity

    // check user is active or not

    if ($request->get('active')) {
        $this->auth->login($user);
    }

    // do further as you need
}

答案 6 :(得分:0)

我想我找到了一个快速修复。我不确定它是否是最好的解决方案,对Laravel来说是全新的。所以,我做的是进入RedrectsUsers.php文件夹并打开redirectPath()文件,然后更改了public function redirectPath() {return property_exists($this, 'redirectTo') ?$this->redirectTo : '/login';} 功能。那么我现在的功能是:

home

基本上,我将login替换为Inputs | Outputs ------------------- S2 S1 S0 | N2 N1 N0 -------------------- 0 0 0 | 0 0 0 0 0 1 | 0 0 1 0 1 0 | 0 1 1 0 1 1 | 0 1 0 1 0 0 | 1 1 0 1 0 1 | 1 1 1 1 1 0 | 1 0 1 1 1 1 | 1 0 0 。希望它没问题。等待评论,建议或任何其他观察。祝你好运。