I am creating a Create User and Login User module in Laravel 11, currently with my progress I can now create account and save the credentials in the database with the hashedpassword.
I have this blade file that recognizes if the user is auth or not, and it always displays the guest user.
auth_page.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Auth Users Only</title>
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body>
@endif
@auth
<h1> AUTH USER </h1>
@endauth
@guest
<h1> GUEST USER </h1>
@endguest
</body>
</html>
here is my Model as well from the creation account to redirecting of the account in the auth_page.
<?php
namespace AppModels;
// use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
class AuthModel extends Authenticatable
{
use HasFactory, Notifiable;
protected $table = "users_table";
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'username',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'password' => 'hashed',
];
}
}
and lastly, here is the controller for Auth::create() for creating the account which is already working and Auth::login() for logging in the account, Auth::login() however does not work and the only one working now is the Auth:create() which stores the input credentials I have made.
<?php
namespace AppHttpControllers;
use AppModelsAuthModel;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
class AuthController extends Controller
{
public function createUser(Request $request)
{
$fields = $request->validate([
'username' => ['required', 'max:255'],
'password' => ['required', 'min:6', 'max:32', 'confirmed']
]);
$user = AuthModel::create($fields);
Auth::login($user);
return redirect()->route('auth_user');
}
}
I have tried using the “auth()->login($user)
this is the router for my auth_page that redirects the user after creation and should be authenticating of that newly created user.
Route::get('/auth_page', function () {
return view('auth_page');
})->name('auth_user');
gecko.eth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.