###前提・実現したいこと
データベースのデータを管理するWebアプリケーションを作っています。
既存のデータベースのユーザーテーブルを使って、ログイン機能を実装しようとしているのですが、
Registerからユーザー登録はできるのですが、ログインができない状態です。
ユーザーテーブルの構造は次のようになっています。
+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | id | varchar(20) | NO | PRI | NULL | | | password | varchar(255) | NO | | NULL | | | Role | smallint(6) | NO | | NULL | | | status | smallint(6) | NO | | NULL | | +----------+--------------+------+-----+---------+-------+
上記のuserテーブルを使ってログイン機能を実装したいです。
php artisan make:authでLaravelのデフォルトの認証機能を作成してそれらを編集しています。
###発生している問題
Registerからユーザーデータするのはうまくいくのですが登録したユーザーデータでログインするとIDのみが保持された状態でログイン画面に戻ります。
###該当のソースコード
ログイン機能でいじったのは以下の部分です。
AuthController.php
php
1<?php 2 3namespace App\Http\Controllers\Auth; 4 5use App\User; 6use Validator; 7use App\Http\Controllers\Controller; 8use Illuminate\Foundation\Auth\ThrottlesLogins; 9use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 10 11class AuthController extends Controller 12{ 13 /* 14 |-------------------------------------------------------------------------- 15 | Registration & Login Controller 16 |-------------------------------------------------------------------------- 17 | 18 | This controller handles the registration of new users, as well as the 19 | authentication of existing users. By default, this controller uses 20 | a simple trait to add these behaviors. Why don't you explore it? 21 | 22 */ 23 24 use AuthenticatesAndRegistersUsers, ThrottlesLogins; 25 26 /** 27 * Where to redirect users after login / registration. 28 * 29 * @var string 30 */ 31 protected $redirectTo = '/home'; 32 33 /** 34 * Create a new authentication controller instance. 35 * 36 * @return void 37 */ 38 public function __construct() 39 { 40 $this->middleware($this->guestMiddleware(), ['except' => 'logout']); 41 } 42 43 /** 44 * Get a validator for an incoming registration request. 45 * 46 * @param array $data 47 * @return \Illuminate\Contracts\Validation\Validator 48 */ 49 protected function validator(array $data) 50 { 51 return Validator::make($data, [ 52 'id' => 'required|max:20', 53 'password' => 'required|min:6|confirmed', 54 'Role'=> 'required', 55 'status' => 'required' 56 ]); 57 } 58 59 /** 60 * Create a new user instance after a valid registration. 61 * 62 * @param array $data 63 * @return User 64 */ 65 protected function create(array $data) 66 { 67 return User::create([ 68 'id' => $data['id'], 69 'password' => bcrypt($data['password']), 70 'Role' => $data['Role'], 71 'status' => $data['status'] 72 ]); 73 } 74} 75 76
login.blade.php
PHP
1@extends('layouts.app') 2 3@section('content') 4<div class="container"> 5 <div class="row"> 6 <div class="col-md-8 col-md-offset-2"> 7 <div class="panel panel-default"> 8 <div class="panel-heading">Login</div> 9 <div class="panel-body"> 10 <form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}"> 11 {{ csrf_field() }} 12 13 <div class="form-group{{ $errors->has('id') ? ' has-error' : '' }}"> 14 <label class="col-md-4 control-label">id</label> 15 16 <div class="col-md-6"> 17 <input type="text" class="form-control" name="id" value="{{ old('id') }}"> 18 19 @if ($errors->has('id')) 20 <span class="help-block"> 21 <strong>{{ $errors->first('id') }}</strong> 22 </span> 23 @endif 24 </div> 25 </div> 26 27 <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> 28 <label class="col-md-4 control-label">Password</label> 29 30 <div class="col-md-6"> 31 <input type="password" class="form-control" name="password"> 32 33 @if ($errors->has('password')) 34 <span class="help-block"> 35 <strong>{{ $errors->first('password') }}</strong> 36 </span> 37 @endif 38 </div> 39 </div> 40 41 <div class="form-group"> 42 <div class="col-md-6 col-md-offset-4"> 43 <div class="checkbox"> 44 <label> 45 <input type="checkbox" name="remember"> Remember Me 46 </label> 47 </div> 48 </div> 49 </div> 50 51 <div class="form-group"> 52 <div class="col-md-6 col-md-offset-4"> 53 <button type="submit" class="btn btn-primary"> 54 <i class="fa fa-btn fa-sign-in"></i>Login 55 </button> 56 57 <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a> 58 </div> 59 </div> 60 </form> 61 </div> 62 </div> 63 </div> 64 </div> 65</div> 66@endsection 67
User.php
PHP
1<?php 2 3namespace App; 4 5use Illuminate\Foundation\Auth\User as Authenticatable; 6 7class User extends Authenticatable 8{ 9 protected $table = 'M_USER'; 10 /** 11 * The attributes that are mass assignable. 12 * 13 * @var array 14 */ 15 16 17 protected $fillable = [ 18 'id', 'password', 'Role', 'status', 19 ]; 20 21 22 /** 23 * The attributes that should be hidden for arrays. 24 * 25 * @var array 26 */ 27 protected $hidden = [ 28 'password', 29 ]; 30 31 public $timestamps = false; 32}
route.php
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::auth(); Route::get('/home', 'HomeController@index');
###試したこと
bcrypt()を外してからデータ登録をし直してみましたが、結果は変わりませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
PHP 5.6.21
Laravel Framework version 5.2.32
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/02 12:48