hmones/laravel-redirect

Redirects users automatically after login to the previous protected route they were trying to access

v1.1.0 2022-05-09 08:29 UTC

This package is auto-updated.

Last update: 2024-04-09 14:12:09 UTC


README

Build Status Style CI Total Downloads Latest Stable Version License

This package is a configurable add-on to your laravel application that allows you to do the following:

  • Redirect users back to the protected (needs-authorization) page they wanted to visit right after they log-in instead of the default page.
  • Redirect users to a particular destination inside or outside your application by providing that link in a url parameter when logging in or on any of your application pages (e.g. https://your-domain.com/login?redirect=https://another-domain.com).
  • Configure the redirect parameter, disable it, make it only accept certain regex or change the name of that parameter
  • Configure the middleware group used for authentication in case you are not using laravel defaults or want to use the redirect for another middleware group.

Installation

Via Composer

composer require hmones/laravel-redirect

Configuration

To publish the package configuration

php artisan vendor:publish --tag=laravel-redirect-config

The configuration file contains the following parameters:

  • web_middleware: the name of your application's web middleware
    • Env variable: WEB_MIDDLEWARE
    • Default value: web
  • parameter.enabled whether you would like to enable parameter redirect on your application, once enabled a user can be redirected for example after login given that their login url was as in this example: https://mydomain.com/login?redirect=https://another-url.com
    • Env variable: REDIRECT_PARAMETER_ENABLED
    • Default value: true
  • parameter.name if parameter redirect is enabled, you can customize the query parameter used to capture the redirect link
    • Env variable: REDIRECT_PARAMETER
    • Default value: redirect
  • parameter.regex if parameter redirect is enabled, you can add a regex to check the redirect query parameter e.g. /^.*mydomain\.com$/
    • Env variable: REDIRECT_REGEX
    • Default value: null
  • routes.login the name of the login route used by your application
    • Env variable: LOGIN_ROUTE_NAME
    • Default value: login
  • routes.logout the name of the logout route used by your application
    • Env variable: LOGOUT_ROUTE_NAME
    • Default value: logout
  • routes.default the name of the route that the user is redirected to by default after login
    • Env variable: DEFAULT_ROUTE_NAME
    • Default value: home

Usage

  • Install the package
  • Adjust the configuration parameters in your .env file or publish the package configuration file to modify it.
  • Add the package service provider at the end of the providers array in your app configuration file config\app.php
    'providers' => [
        ...
        Hmones\LaravelRedirect\LaravelRedirectServiceProvider::class
    ],

Change log

Please see the changelog for more information on what has changed recently.

Testing

composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

License

Please see the license file for more information.