using a starter kit? Breeze / Fortify ?
Jun 27, 2023
3
Level 1
Login route with parameters in url before
I am trying to create my laravel website app with an original login route pattern. I need to have the custom name and alias in the URL because I have created a kind of portal where all customers have their own landing page. In my previous website version, I used sub-domain names but it's too heavy to manage.
This is what I want:
The login URL has to be:
-
my-app.com/{customer_name}/{customer_alias}/login -
my-app.com/{customer_name}/{customer_alias}/home -
my-app.com/{customer_name}/{customer_alias}/list-product
I have tried some stuff, but after login in, I never got routed to the full URL like I want. In the best case after login in, I'm redirected to https//home and I don't understand why!
My Route file : Web.php
Route::get('/{customer_name}/{customer_alias}/login', [ LoginController::class, 'login'])->name('login');
// The app part when user are logged
Route::prefix('/{customer_name}/{customer_alias}')->middleware( ['auth', 'access'] )->group(function () {
Route::get('/', 'DashController@index');
}
My MiddleWare access
<?php
namespace App\Http\Middleware;
use App\Customers;
use Closure;
use Illuminate\Support\Facades\Auth;
use MongoDB\Driver\Session;
class Access
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$customers = null;
if( session('customer_alias') != "" ){
$customer_alias= session('customer_alias');
} else {
$customer_alias= $request->route('customer_alias');
}
$customer = customer ::where( 'customer_alias', $customer_alias)->first();
if(!$customer ){
abort(404);
} else {
session(['customer_id' => $customer ->id]);
session(['customer_name' => $customer->name]);
session(['customer_alias' => $customer->alias]);
}
return $next($request);
}
My Auth\LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Customers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function authenticated()
{
$url = explode('.', $_SERVER['HTTP_HOST'])[0];
if($url ==''){
abort(404);
}
return redirect(session('customer_name')."/".session('customer_alias')."/home");
}
protected function credentials(Request $request)
{
$credentials = [
$this->username() => strtolower($request->input($this->username())),
'password' => $request->get('password'),
];
return $credentials;
}
public function showLoginForm(Request $request, $customer_name, $customer_alias)
{
$this->redirectTo = '/' . $customer_name .'/'. $customer_alias;
$customer = Customer::where( 'alias', $customer_alias)->first();
if( !$customer ){
abort('404');
}
return view('auth.login', compact( 'customer ' ) );
}
}
Thanks for your help, I'm very confused about it.
Please or to participate in this conversation.