質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

解決済

2回答

11687閲覧

Laravel5でログインの認証がうまく行かない

nirakka-

総合スコア7

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2016/07/01 07:39

編集2016/07/01 07:46

###前提・実現したいこと
データベースのデータを管理する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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

ユーザテーブルにユーザ名に相当するカラムが無いのでちょっと特殊な作りのように見えますが、「id」と「password」を入力してログインする(idが所謂ユーザ名に相当する)という前提で書きます。

AuthController

use AuthenticatesAndRegistersUsers

しています。
ログインや登録といったロジックはその名の通りAuthenticatesAndRegistersUsersトレイトにまとめられています。

AuthenticatesAndRegistersUsersトレイトでは

use AuthenticatesUsers

のように、AuthenticatesUsersトレイトをuseしています。
こちらにユーザ認証に関するメソッドが収められています。その中に、

/** * Get the login username to be used by the controller. * * @return string */ public function loginUsername() { return property_exists($this, 'username') ? $this->username : 'email'; }

というメソッドがあります。
デフォルトではパスワードとusername、またはemailのセットで認証しますが今回のケースではどちらも存在しません。
今回はidがusernameに相当するので、このメソッドがidをreturnするようにします。

もちろんvendor以下のファイルを直接弄ることは出来ないので、AuthControllerあたりでオーバーライドします。

public function loginUsername() { return 'id'; }

こちらの環境ではとりあえずこれでログインできました。

--
(追記)
寝ぼけてました。
property_existsで判定してるのでAuthControllerloginUsername()をオーバーライドせずとも

protected $username = 'id';

のように指定してあげれば良いですね。

投稿2016/07/01 15:16

編集2016/07/01 15:30
romiogaku

総合スコア546

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nirakka-

2016/07/02 12:48

まさに、自分が求めていた答えです。 ありがとうございました。
guest

0

追記です。laravelのデフォルトのAuthではuserテーブルにprimary keyとなる連番を付与しないといけないようです。

+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| no | int(10) unsigned | NO | PRI | NULL | |
| id |varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| Role | smallint(6) | NO | | NULL | |
| status | smallint(6) | NO | | NULL | |
+----------+--------------+------+-----+---------+-------+

このテーブル構造でうまくいきました。

投稿2016/07/08 00:52

nirakka-

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問