Laravel php auth system

0

How to authenticate user login and password in laravel, I am trying already has some time but I can not.

Routes

Route::get('/login',['uses' => 'loginController@login','as' => 'login']);    
Route::post('/login',['uses'=>'loginController@checkLogin','as' => 'VerificarLogin']);

My controller

    class loginController extends Controller
    {

    public function login(){

        return view('Inicial.login');
    }
    public function teste(){

        return view('teste');
    }

     public function checkLogin(Request $request ){

        $email = Input::get('email');
        $password = Input::get('password');

       if (Auth::attempt(['email' => $email, 'password' => $password])) {

            return 1;
        }

        return 0; 
    }

My auth.php file

 'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],  

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'table' => 'usuarios',
        'model' => App\Usuario::class,
    ],  

My User Class

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Usuario extends Authenticatable
{

    protected $fillable = ['nome','password','email','data_nascimento','rg','funcao','telefone'];

    protected $table = 'usuarios';    
}' 

And my form galera

              <form class="form-horizontal new-lg-form" id="loginform" action="{{ route('VerificarLogin') }}" method="post">
                    {{ csrf_field() }}

                <div class="form-group  m-t-20">
                  <div class="col-xs-12">
                    <label>Endereço de Email</label>
                    <input class="form-control" type="text" required="" placeholder="Email" id="email" name="email">
                  </div>
                </div>

                <div class="form-group">
                  <div class="col-xs-12">
                    <label>Senha</label>
                    <input class="form-control" type="password" required="" placeholder="Senha" id="senha" name="password">
                  </div>
                </div>

                <div class="form-group">
                  <div class="col-md-12">
                    <div class="checkbox checkbox-info pull-left p-t-0">
                      <input id="checkbox-signup" type="checkbox">
                      <label for="checkbox-signup"> Lembrar-me </label>
                    </div>
                    <a href="javascript:void(0)" id="to-recover" class="text-dark pull-right"><i class="fa fa-lock m-r-5"></i> Esqueceu a senha ?</a> </div>
                </div>

                <div class="form-group text-center m-t-20">
                  <div class="col-xs-12">
                    <button class="btn btn-info btn-lg btn-block btn-rounded text-uppercase waves-effect waves-light" type="submit">Entrar</button>
                  </div>
                </div>

                </form>   

In my verification I can never log in or log in. If someone has a similar project send the link to me to look or download

    
asked by anonymous 24.01.2018 / 15:10

2 answers

1
Route::post('/login',['uses'=>'loginController@checkLogin','as' => 'VerificarLogin'])->middleware('auth');

You have not told Laravel that it is for this route to be protected by calling the middleware function as above. This is not the only way to protect a route because it is very hard work and difficult to put a call to mddleware in each route an exit would use the following syntax:

Route::group(['middleware' => 'web'], function () {
    Route::auth();
    Route::post('/login',['uses'=>'loginController@checkLogin','as' => 'VerificarLogin']);
});

Anything within that group will be checked to see if the user is logged in or not. If the user is not logged in you will get an error or you can set a page for it to display, it is usually redirected to the login screen. You can find more information at link link

    
24.01.2018 / 15:28
1

Joan Mark,

Laravel has the authentication functionality ready, just through the console run: php artisan make: auth

If you have the connection to the database configured in the .ENV file, it creates the tables and mounts the page for user login using Bootstrap template.

Take a look: link

As your friend Mark mentioned you can protect your pages through the routes using a Middleware link that facilitates a lot and the code ends up getting much smaller and cleaner.

You can also insert an if in your view to show only if the user is logged in using:

@if (Auth :: check ())

... YOUR HTML CODE ...

@endif

I hope I have helped you!

See you soon!

    
24.01.2018 / 20:05