John Davidson

php - Laravel auth won't let me "get" from Database

0 comments
Message:


So I've cloned my own laravel project, that works on my main machine, to my test pc and set it up as follows:



  1. Installed XAMPP and set it up.

  2. git clone project to the machine.

  3. Installed composer on the machine.

  4. executed the command composer install.

  5. php artisan key:generate.

  6. I installed Postgres and set up a DB

  7. uncommented Postgres extensions in php.ini

  8. Edited the .env file to connect to the DB

  9. php artisan migrate:fresh.

  10. php artisan db:seed. (New users)


Now when trying to log in, the page just refreshes. I found out that I'm able to read and write to the database when creating a new page that is not protected by the login.


So something with the whole auth situation must be wrong. Did I miss anything when setting the whole project up? Or where the commands executed in the wrong order?


Oh btw. The auth was made with php artisan make:auth


Here is part of my web.php. The whole thing is really big.


<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');

Route::get('/user', 'UsersController@index')->middleware(['auth.admin']);
Route::post('/user', 'UsersController@store')->middleware(['auth.admin']);
Route::get('/user/edit/{id}', 'UsersController@edit')->middleware(['auth.admin']);
Route::post('/user/update/{id}', 'UsersController@update')->middleware(['auth.admin']);
Route::get('/user/delete/{id}', 'UsersController@delete')->middleware(['auth.admin']);
Route::get('/usersearch', 'UsersController@search');
Route::get('/user/paginate/{id}', 'UsersController@paginate')->middleware(['auth.admin']);

Route::get('/order', 'OrderItemController@index');
Route::get('/order/delete/{id}', 'OrderItemController@delete');
Route::get('/order/edit/{id}', 'OrderItemController@edit');
Route::post('/order/update/{id}', 'OrderItemController@update');
Route::post('/order', 'OrderItemController@store');
Route::get('/ordersearch', 'OrderItemController@search');
Route::get('/order/paginate/{id}', 'OrderItemController@paginate');

Route::get('/customer/delete/{id}', 'CustomerController@delete');
Route::get('/customer/edit/{id}', 'CustomerController@edit');
Route::post('/customer/update/{id}', 'CustomerController@update');
Route::get('/customer', 'CustomerController@index');
Route::post('/customer', 'CustomerController@store');
Route::get('/customer/paginate/{id}', 'CustomerController@paginate');



My Answer:
Laravel provides a built-in authentication system that makes it easy to implement user authentication in your application. The `php artisan make:auth` command can be used to generate the necessary authentication scaffolding, including login and registration views, controllers, and routes.

Once the authentication scaffolding has been generated, you can use the `Auth` facade to handle user authentication in your application. This includes methods for logging in users, logging out users, and checking if a user is authenticated.

For example, you can use the `Auth::attempt()` method to authenticate a user based on their email and password:

php
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication successful
} else {
// Authentication failed
}


You can also use the `Auth::check()` method to check if a user is currently authenticated:

php
if (Auth::check()) {
// User is authenticated
} else {
// User is not authenticated
}


Laravel's authentication system also provides middleware that can be used to restrict access to certain routes based on whether a user is authenticated. You can apply the `auth` middleware to a route or group of routes to require authentication:

php
Route::get('/dashboard', function () {
// Only authenticated users can access this route
})->middleware('auth');


Overall, Laravel's authentication system provides a convenient and secure way to implement user authentication in your application.

Rate this post

3 of 5 based on 7133 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us