2016-08-30 5 views

Antwort

1

Sie können manuell die Benutzer authentifizieren:

public function authenticate(Request $request) 
{ 
    $password=$request->password; 
    $phone=$request->phone_number; 
    if (Auth::attempt(['phone_number' => $phone, 'password' => $password])) 
    {  
     return redirect()->intended('/'); 
    } 
    else 
    { 
     return redirect('/login'); 
    } 

} 
+0

nicht schlecht noch gut. –

1

verwenden, während die Validierung:

$rules = array(
     'mobile' => 'required', // make sure the mobile is an actual mobile 
     'password' => 'required' // password can only be alphanumeric and has to be greater than 3 characters 
    ); 


    // run the validation rules on the inputs from the form 
    //$validator = Validator::make(Input::all(), $rules); 

    $validator = Validator::make(Input::all(), $rules); 

    // if the validator fails, redirect back to the form 
    if ($validator->fails()) { 

     return Redirect::to('login') 
      ->withErrors($validator) // send back all errors to the login form 
      ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form 
    } else { 

     //Create our user data for the authentication 
     $userdata = array(
      'mobile'  => Input::get('mobile'), 
      'password' => Input::get('password') 
     ); 

     $user = User::where('mobile',Input::get('mobile'))->with('userRoles')- >first(); 

    if($user){ 
    if (Auth::attempt($userdata)) { 

      /*if(User::isAdmin($user->id)) { 
       return Redirect::to('/admin'); 
      } 
      if(User::isAuthor($user->id)) { 
       return Redirect::to('/dashboard'); 
      }*/ 
      // validation successful! 
      // redirect them to the secure section or whatever 
      // return Redirect::to('secure'); 
      // for now we'll just echo success (even though echoing in a controller is bad) 
      //echo 'SUCCESSSSS!'; 
      return Redirect::to('/'); 

     } else {   
      // validation not successful, send back to form 
      return Redirect::to('login')->withErrors(['Invalid Username/Password']); 

     } 
    } 
+0

danke, aber dies nicht mit Auth Laravel 5.2. –

+0

das ist auth von laravel..please siehe meine Antwort, die ich bearbeitet habe –

1

Diese Arbeit sollte:

// app/Http/Controllers/Auth/AuthController.php 

<?php 

namespace App\Http\Controllers\Auth; 

use App\User; 
use Validator; 
use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 

class AuthController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Registration & Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users, as well as the 
    | authentication of existing users. By default, this controller uses 
    | a simple trait to add these behaviors. Why don't you explore it? 
    | 
    */ 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 

    public $username = 'phonenumber'; 

    // ... 

Wenn Sie den Standard 5.2 Textvorschlag für die Registrierung verwenden sind, sollten Sie müssen auch die validator und create Methoden ändern bevor sie mit Ihren benutzerdefinierten Anmeldeinformationen arbeiten können.

Denken Sie daran, dies funktioniert nur für 5.2. Laravel 5.3 hat viele Dinge im Auth-Workflow verändert.

+0

Siehe https://stackoverflow.com/questions/42190620/authentication-on-username-instead-of-email/42191005#42191005 für Laravel 5.3 + kompatibel Antworten. – Tayler

1

in Ihrem Auth Controller

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

stellen Sie sicher, den Namen Ihres Texteingabefeld auch noch die gleiche genannt wird.

Verwandte Themen