doupiai5597 2016-12-06 15:06
浏览 14

Laravel Auth问题

I just started to learn Laravel and I was trying to set up an authentication system. I could have used the laravel built in auth but I followed a tutorial to understand the concept. My auth system is built using some modals from bootstrap and the system works (kind of). The sign up works but as soon as I enter the sign in logic the sign up crashes somehow (no errors) and doesn't save my user in the database.

Here's what I'm using:

This is the modal window for the Sign Up. The Log In is pretty much the same, I just changed the action to point to the postSignin method.

<!-- Modal Register area -->
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myRegisterLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      {!! Form::open(['method' => 'POST', 'action' => 'UserController@postSignup', 'class' => 'login-form']) !!}
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myRegisterLabel">Sign Up</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label>Email:</label>
          {!! Form::text('email', Request::old('email'),['placeholder' => 'Email...']) !!}
        </div>
        <div class="form-group">
          <label>Password:</label>
          {!! Form::password('password', Request::old('password'),['placeholder' => 'Password...']) !!}
        </div>
        <div class="row">
          <div class="col-sm-12">
            <input type="submit" class="btn btn-primary login-btn" value="Sign Up">
          </div>
        </div>
      </div>
      {!! Form::close() !!}
      @if($errors->any())
        @foreach ($errors->all() as $error)
          <div class="alert alert-danger">
            <p>{{ $error }}</p>
          </div>
        @endforeach
      @endif
    </div>
  </div>
</div>
<!-- End Modal Register area -->

This is how I set up my controller:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Auth;

class UserController extends Controller
{
    public function postSignup(Request $request){
      $this->validate($request, [
        'email' => 'email|required|unique:users',
        'password' => 'required|min:4'
      ]);

      $user = new User([
        'email' => $request->input('email'),
        'password' => bcrypt($request->input('password'))
      ]);
      $user->save();

      return redirect('/');
    }

    public function postSignin(Request $request){
      $this->validate($request, [
        'email' => 'email|required',
        'password' => 'required|min:4'
      ]);

      if (Auth::attempt([
        'email' => $request->input('email'),
        'password' => $request->input('password')
      ])){
        return redirect()->route('user.profile');
      }
      return redirect()->back();
    }

    public function getProfile(){
      return view('user.profile');
    }
}

For my routes I used just:

Route::post('/','UserController@postSignup');

Route::post('/','UserController@postSignin');

So, as I said, as soon as I set up the Log In the Sign up is not registering my user into the DB. If I take it out, it works.. The Log In works either way if I have some users into the DB. Thanks for your help!

  • 写回答

2条回答 默认 最新

  • dongxu6418 2016-12-06 15:14
    关注

    Ok, for anyone wondering what was the problem here. It was because of my routes. Both were pointing to '/' which somehow lead to that issue.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度