how-to-redirect-to-login-if-user-not-logged-in-laravel
if login is not there then rediret to other page using middleware in laravel with example
public function index()
{
if (Auth::check()) {
// your logic here
} else {
return redirect('other-page');
}
}
public function loginForm()
{
Log::info('we are in loginForm form');
$currentUrl = request()->url();
log::info($currentUrl);
$dataAfterLast = Str::afterLast($currentUrl, '/');
log::info($dataAfterLast);
$dataAfterLastSlash = Str::after($currentUrl, 'http://wz-account-admin-ms/');
log::info($dataAfterLastSlash);
$desiredSegment = Str::before($dataAfterLastSlash, '/');
log::info($desiredSegment);
if($desiredSegment=="influencer")
{
if (auth()->check()) {
// User is already authenticated, redirect to another place
return redirect()->route('influencer_dashboard', ['slug' => Str::slug(Auth::user()->name)]);
}
else{
log::info("ur desired segment");
return view('auth.influencer_login');
}
}
else
{
if (auth()->check()) {
return redirect()->route('publisher', ['slug' => Str::slug(Auth::user()->name)]);
}
else{
log::info("ur desired segment");
return view('auth.publisher_login');
}
}
}
output
if user is not logged in means auth()->check() condition is not satisfied
if user is logged in then redirect in dashboard
==============OR========================
public function login(Request $request)
{
$user = User::where('email', $request->email)->first();
if (Hash::check($request->password, $user->password)) {
// Log the user in
} else {
// Redirect the user back with an error
}
}
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Crypt;
class UserController extends Controller
{
public function store()
{
$user = new User;
$user->name = 'John Doe';
$user->email = 'johndoe@example.com';
$user->password = Crypt::encryptString('secret');
$user->save();
}
}
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Crypt;
class UserController extends Controller
{
public function show($id)
{
$user = User::find($id);
$password = Crypt::decryptString($user->password);
dd($password);
}
}
Using Middlewares
You can check for the presence of the user's authentication status in Laravel using the Auth facade and redirect them if they are not logged in using an if...else statement.
Here's an example:
This will create a new middleware file in the app/Http/Middleware directory.
In this example, the Auth::check() method returns true if the user is authenticated and false if they are not. If the user is not authenticated, they will be redirected to the other-page URL.
Next, you need to register this middleware in the app/Http/Kernel.php file:
Finally, you can assign this middleware to the routes that you want to protect:
In this example, all the routes inside the closure will be protected by the RedirectIfNotAuthenticated middleware. Unauthenticated users will be redirected to the other-page URL if they try to access any of these protected routes.
how to implement miidleware handle function in laravel Controller
You can use middleware in a Laravel Controller to apply the middleware to specific routes or actions within your application.
Here's an example of how you could use middleware in a Controller:
In this example, the __construct method is used to apply the auth middleware to the show method. This means that the show method will only be accessible to authenticated users. If an unauthenticated user tries to access the show method, they will be redirected to the login page (or another page, if you have configured the login route differently).
Summary
Using Auth::check() redirect page if user not logged in Laravel
entered password matches the stored hashed password using Hash::check
encrypt and dycrypt the password in database
Top comments (0)