Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to redirect to Login if user not logged in Laravel

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');
    }
}
Enter fullscreen mode Exit fullscreen mode

==============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
    }
}
Enter fullscreen mode Exit fullscreen mode
<?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();
    }
}
Enter fullscreen mode Exit fullscreen mode
<?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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Image description

Image description

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:

Image description

Finally, you can assign this middleware to the routes that you want to protect:

Image description

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:

Image description

Image description

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

Top comments (0)