Laravel php auth system

Asked

Viewed 1,965 times

0

How to authenticate the user login and password in Windows, I’ve been trying for a while but I can’t.

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 personal form

              <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>   

On my check I can never log in or log in. If someone has a similar project send the link for me to look at or download

2 answers

1

Joan Marcos,

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 file. ENV creates the tables and mounts the user login page using the Bootstrap template.

Take a look: https://laravel.com/docs/master/authentication

As friend Marcos mentioned you can protect your pages through the routes using a Middleware https://laravel.com/docs/master/middleware that makes it easier and the code ends up getting much smaller and cleaner.

You can also enter 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’ve helped you!

See you around!

  • vlw man helped yes ^^

1


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

Missed you telling Laravel that it is for that route to be protected by calling the middleware function as I did above. This is not the only way to protect a route as it gets very laborious and difficult to maintain you put a call to mddleware on each route one way out would be to use the following syntax:

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

Everything that is inside that group will go through the verification that the user is logged in or not. If the user is not logged in, an error will appear or you can set a page to show, it is usually redirected to the login screen. You can find more information at https://laravel.com/docs/5.4/authentication#protecting-Routeshttps://laravel.com/docs/5.4/authentication#protecting-Routes

  • Marcos, I still can not get my return in the checkLogin that is in my controller, because I want to know if his data has in the database and then redirect it to a route... did not take

  • In my controller it is always returning 0 even when I put the correct data that is in the database

  • Joan inside your controller where you are: if (Auth::Attempt(['email' => $email, 'password' => $password])) { Return 1; } you have to put Return view('name_da_view), and the Laravel will redirect you to the view and log in. Do the following, protect the route and try to access without logging in you have to give an error. Then you log in and close the page without closing the browser and enter the page then you have to be redirected to the protected page.

  • that’s when I log in and the password my controller has to return me '1' and not '0'

  • Tell me one thing when you registered the password in the database you applied the hash or directly recorded the password without encryption?

  • the password is without hash recorded right password tried several different things but never picks

  • The problem must be there does so uses the bcrypt() function to encrypt the password. It gets more or more like $user->password = bcrypt("your password"); The Standard when you go to check the password encrypts the password you pass and checks. After you save the encrypted password in your data flock will work, and you can not forget that every new user must have use encrypted password before recording in the bank, because in the bank you only record the encrypted password. Gives a feedback to know if it worked.

  • OK man I’ll forehead here

  • this command is for me to register the encrypted password or to check if the password is hashed ?

  • To register the encrypted password.

  • it worked man it was just that

  • You’re the man, vlw man

  • Good that it worked friend, thank you for marking the answer. Good luck!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.