Not idea without seeing your code; maybe you already are authenticated??
Login link on Laravel Welcome page disappeared
Hi,
On the Laravel welcome screen, there was a login link that just disappeared after I moved my dashboard folder into the newly created user folder. When I reversed the action, I can still not see the login link. Any idea?
@tykus It's working now. Strange. Sorry
The problem persists as the login link is missing from the laravel welcome page after I created the user folder and moved folders and files into it. To clarify better, the user folder contains the dashboard folder. The dashboard folder contains the address folder and the layouts folder. It also contains the dashboard.blade.php and profile.blade.php files.
This is inside the user.php route:
<?php
use App\Http\Controllers\UserProfileController;
use App\Http\Controllers\UserAddressController;
use App\Http\Controllers\UserDashboardController;
use Illuminate\Support\Facades\Route;
/** Laravel Breeze routes that update the User's username, email and password */
Route::middleware(['auth', 'verified', 'role:user'])
->prefix('user')
->name('user.')
->group(function () {
Route::get('dashboard', [UserDashboardController::class, 'index'])->name('user.dashboard');
Route::get('profile', [UserProfileController::class, 'index'])->name('profile');
Route::put('profile', [UserProfileController::class, 'updateProfile'])->name('profile.update');
Route::post('profile', [UserProfileController::class, 'updatePassword'])->name('profile.update.password');
});
/** User Dashboard route that will take you back to the main dashboard page of the user when you click on the Dashboard link */
Route::get('user/dashboard/dashboard', [UserDashboardController::class, 'index'])->name('user.dashboard');
/** User Address route */
Route::get('user/dashboard/create', [UserAddressController::class, 'create'])->name('address.create');
Route::post('user/dashboard/address', [UserAddressController::class, 'store'])->name('address.store');
Route::get('user/dashboard/address/{id}/edit', [UserAddressController::class, 'edit'])->name('address.edit');
Route::put('user/dashboard/address/{id}', [UserAddressController::class, 'update'])->name('address.update');
Route::delete('user/dashboard/address/{id}', [UserAddressController::class, 'destroy'])->name('address.destroy');
This is the UserDashboardController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDashboardController extends Controller
{
public function index ()
{
return view('user.dashboard.dashboard');
}
}
This is the UserAddressController:
<?php
namespace App\Http\Controllers;
// This adds the UserAddress Model to the controller
use App\Models\UserAddress;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class UserAddressController extends Controller
{
// This will return the address page
public function address()
{
$addresses = UserAddress::where('user_id', Auth::user()->id)->get();
return view('user.dashboard.address.index', compact('addresses'));
}
// This will return the create page
public function create()
{
return view('user.dashboard.address.create');
}
public function store(Request $request)
{
// This adds validation to the user input fields
$request->validate([
'name' => ['required', 'max:200'],
'email' => ['required', 'max:200', 'email'],
'phone' => ['required', 'max:200'],
'country' => ['required', 'max:200'],
'state' => ['required', 'max:200'],
'city' => ['required', 'max:200'],
'zip' => ['required', 'max:200'],
'address' => ['required', 'max:200'],
]);
// This stores the user input into the database
$address = new UserAddress();
$address->user_id = Auth::user()->id;
$address->name = $request->name;
$address->email = $request->email;
$address->phone = $request->phone;
$address->country = $request->country;
$address->state = $request->state;
$address->city = $request->city;
$address->zip = $request->zip;
$address->address = $request->address;
$address->save();
// This displays the toast message
toastr('Updated Successfully!')->success('Address successfully added!');
return redirect()->route('address');
}
// This will update an existing address
public function edit(string $id)
{
$address = UserAddress::findOrFail($id);
return view('user.dashboard.address.edit', compact('address'));
}
public function update(Request $request, string $id)
{
$request->validate([
'name' => ['required', 'max:200'],
'email' => ['required', 'max:200', 'email'],
'phone' => ['required', 'max:200'],
'country' => ['required', 'max:200'],
'state' => ['required', 'max:200'],
'city' => ['required', 'max:200'],
'zip' => ['required', 'max:200'],
'address' => ['required'],
]);
$address = UserAddress::findOrFail($id);
$address->user_id = Auth::user()->id;
$address->name = $request->name;
$address->email = $request->email;
$address->phone = $request->phone;
$address->country = $request->country;
$address->state = $request->state;
$address->city = $request->city;
$address->zip = $request->zip;
$address->address = $request->address;
$address->save();
toastr('Updated Successfully!', 'success', 'Success')->success('Updated Successfully!');
return redirect()->route('address');
}
// This will delete an address
public function destroy(string $id)
{
$address = UserAddress::findOrFail($id);
$address->delete();
return response(['status' => 'success', 'message' => 'Deleted Successfully!']);
}
}
This is the UserProfileController:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use File;
class UserProfileController extends Controller
{
public function index()
{
return view('user.dashboard.profile');
}
public function updateProfile(Request $request)
{
$request->validate([
'name' => ['required', 'max:100'],
'email' => ['required', 'email', 'unique:users,email,' . Auth::user()->id],
'image' => ['image', 'max:2048'],
]);
$user = Auth::user();
if ($request->hasFile('image')) {
if (File::exists(public_path($user->image))) {
File::delete(public_path($user->image));
}
$image = $request->image;
$imageName = rand() . '_' . $image->getClientOriginalName();
$image->move(public_path('images'), $imageName);
$path = 'images/' . $imageName;
$user->image = $path;
}
$user->name = $request->name;
$user->email = $request->email;
$user->save();
toastr()->success('Profile Updated Successfully!');
return redirect()->back();
}
public function updatePassword(Request $request)
{
$request->validate([
'current_password' => ['required', 'current_password'],
'password' => ['required', 'confirmed', 'min:8']
]);
$request->user()->update([
'password' => bcrypt($request->password)
]);
toastr()->success('Profile Password Updated Successfully');
return redirect()->back();
}
}
This is the sidebar.blade.php file which lives inside the user/dashboard/layouts folder
<div class="dashboard_sidebar">
<span class="close_icon">
<i class="far fa-bars dash_bar"></i>
<i class="far fa-times dash_close"></i>
</span>
<a href="dsahboard.html" class="dash_logo"><img src="images/logo.png" alt="logo" class="img-fluid"></a>
<ul class="dashboard_link">1
<li><a class="active" href="{{ route('user.dashboard') }}"><i class="fas fa-tachometer"></i>Dashboard</a></li>
<li><a href=""><i class="fas fa-list-ul"></i> Orders</a></li>
<li><a href="{{ route('user.profile') }}"><i class="far fa-user"></i> My Profile</a></li>
<li><a href="{{ route('user.address') }}"><i class="fal fa-gift-card"></i> Addresses</a></li>
<li>
<form method="POST" action="{{ route('logout') }}">
@csrf
<a href="{{ route('logout') }}"onclick="event.preventDefault();
this.closest('form').submit();"><i class="far fa-sign-out-alt"></i> Log out</a>
</form>
</li>
</ul>
</div>
This is the create.blade.php file which lives inside the user/dashboard/address folder:
@extends('dashboard.layouts.master')
@section('content')
<section id="wsus__dashboard">
<div class="container-fluid">
@include('dashboard.layouts.sidebar')
<div class="row">
<div class="col-xl-9 col-xxl-10 col-lg-9 ms-auto">
<div class="dashboard_content mt-2 mt-md-0">
<h3><i class="fal fa-gift-card"></i>create address</h3>
<div class="wsus__dashboard_add wsus__add_address">
<form action="{{ route('address.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>name <b>*</b></label>
<input type="text" placeholder="Name" name="name">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>email</label>
<input type="email" placeholder="Email" name="email">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>phone <b>*</b></label>
<input type="text" placeholder="Phone" name="phone">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>country <b>*</b></label>
<div class="wsus__topbar_select">
<select class="select_2" name="country">
<option>Country</option>
<option>Austria</option>
<option>Germany</option>
</select>
</div>
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>State <b>*</b></label>
<input type="text" placeholder="State" name="state">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>City <b>*</b></label>
<input type="text" placeholder="City" name="city">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>zip code <b>*</b></label>
<input type="text" placeholder="Zip Code" name="zip">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>Address <b>*</b></label>
<input type="text" placeholder="Address" name="address">
</div>
</div>
<div class="col-xl-6">
<button type="submit" class="common_btn">Create</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
@endsection
This is inside the edit.blade.php which lives inside the user/dashboard/address folder:
@extends('dashboard.layouts.master')
@section('content')
<section id="wsus__dashboard">
<div class="container-fluid">
@include('dashboard.layouts.sidebar')
<div class="row">
<div class="col-xl-9 col-xxl-10 col-lg-9 ms-auto">
<div class="dashboard_content mt-2 mt-md-0">
<h3><i class="fal fa-gift-card"></i>create or update address</h3>
<div class="wsus__dashboard_add wsus__add_address">
<form action="{{ route('address.update', $address->id) }}" method="POST">
@csrf
@method('PUT')
<div class="row">
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>name <b>*</b></label>
<input type="text" placeholder="Name" name="name" value="{{$address->name}}">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>email</label>
<input type="email" placeholder="Email" name="email" value="{{$address->email}}">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>phone <b>*</b></label>
<input type="text" placeholder="Phone" name="phone" value="{{$address->phone}}">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>country <b>*</b></label>
<div class="wsus__topbar_select">
<select class="select_2" name="country" value="{{$address->country}}">
<option>Country</option>
<option>Austria</option>
<option>Germany</option>
</select>
</div>
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>State <b>*</b></label>
<input type="text" placeholder="State" name="state" value="{{$address->state}}">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>City <b>*</b></label>
<input type="text" placeholder="City" name="city" value="{{$address->city}}">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>zip code <b>*</b></label>
<input type="text" placeholder="Zip Code" name="zip" value="{{$address->zip}}">
</div>
</div>
<div class="col-xl-6 col-md-6">
<div class="wsus__add_address_single">
<label>Address <b>*</b></label>
<input type="text" placeholder="Address" name="address" value="{{$address->address}}">
</div>
</div>
<div class="col-xl-6">
<button type="submit" class="common_btn">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
@endsection
This is the index.blade.php which lives inside the user/dashboard/address:
@extends('dashboard.layouts.master')
@section('content')
<section id="wsus__dashboard">
<div class="container-fluid">
@include('dashboard.layouts.sidebar')
<div class="row">
<div class="col-xl-9 col-xxl-10 col-lg-9 ms-auto">
<div class="dashboard_content">
<h3><i class="fal fa-gift-card"></i> address</h3>
<div class="wsus__dashboard_add">
<div class="row">
@foreach ($addresses as $address)
<div class="col-xl-6">
<div class="wsus__dash_add_single">
<h4>Billing Address</h4>
<ul>
<li><span>name :</span> {{ $address->name }}</li>
<li><span>Phone :</span> {{ $address->phone }}</li>
<li><span>email :</span> {{ $address->email }}</li>
<li><span>country :</span> {{ $address->country }}</li>
<li><span>city :</span> {{ $address->state }}</li>
<li><span>zip code :</span> {{ $address->city }}</li>
<li><span>company :</span> {{ $address->zip }}</li>
<li><span>address :</span> {{ $address->address }}</li>
</ul>
<div class="wsus__address_btn">
<a href="{{ route('address.edit', ['id' => $address->id]) }}"
class="edit"><i class="fal fa-edit"></i> edit</a>
<a href="{{route('address.destroy', $address->id)}}" class="del delete-item"><i class="fal fa-trash-alt"></i> delete</a>
</div>
</div>
</div>
@endforeach
<div class="col-12">
<a href="{{ route('address.create') }}" class="add_address_btn common_btn"><i
class="far fa-plus"></i>
add new address</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
@endsection
@Mamunsson In which blade file/page exactly should the login link be visible? Your sidebar.blade.php only has links for an authenticated user.
@gych In the nav.blade.php file which lives inside the frontend folder (which is in the views folder)
<!-- ======= Header ======= -->
<header class="header">
<div class="greeting">
<p>Hallo, Amanda!</p>
</div>
<div class="icons">
<i class="bi bi-person-fill"></i> <a href="{{route('login')}}">Anmelden</a>
<span id="cart-count">0</span>
<i class="bi bi-bag"></i> <a href="#">Einkaufswagen</a>
</div>
</header>
<!-- ======= Navigation ======= -->
<nav class="navbar">
<div class="container">
<h1>The Online Shop</h1>
<ul>
<li>
<form role="search" method="get" class="search-form" action="">
<span class="fa fa-search form-control-feedback"></span>
<input type="search" class="search-field" placeholder="Search the Online Shop" />
<button type="submit" class="search-submit">
Finden
</button>
<input type="hidden" value="product" name="post_type" id="post_type">
</form>
</li>
</ul>
</div>
</nav>
<!-- ======= Selection Menu ======= -->
<div class="selection-menu">
<ul>
<li><a href="#">Immobilien</a></li>
<li><a href="#">Möbel</a></li>
<li><a href="#">Kleidung</a></li>
<li><a href="#">Auto</a></li>
<li><a href="#">Elektronik</a></li>
<li><a href="#">Antiquitäten</a></li>
<li><a href="#">Sport</a></li>
<li><a href="#">Sale %</a></li>
</ul>
</div>
This is the AdminController:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function dashboard()
{
return view('admin.layouts.dashboard');
}
public function login(){
return view('auth.login');
}
}
This is the admin route:
<?php
use App\Http\Controllers\AdminController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
/** Admin Login page route */
Route::get('auth.login', [AdminController::class, 'login'])->name('login');
/** Admin Routes */
Route::get('dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
/** Profile Routes */
Route::get('profile', [ProfileController::class, 'index'])->name('profile');
Route::post('profile/update', [ProfileController::class, 'updateProfile'])->name('profile.update');
Route::post('profile/update/password', [ProfileController::class, 'updatePassword'])->name('password.update');
Basically I want one single login form that will work for the user dashboard, the vendor dashboard and the admin dashboard. The login form already lives inside the auth folder created by laravel breeze and looks like this:
@extends('frontend.master')
@section('title', 'Anmelden')
@section('content')
<!-- ======= Page Heading ======= -->
<div class="page-heading">
<h2>Anmelden</h2>
</div>
<!-- ======= Account Login Page ======= -->
<div class="account-login-page">
<form method="POST" action="{{ route('login') }}">
@csrf
<!-- Email -->
<div>
<label>E-Mail-Adresse</label>
<input id="email" class="form-control" name="email" value="{{ old('email') }}" placeholder="E-Mail"
required />
@if ($errors->has('email'))
<code>{{ $errors->first('email') }}</code>
@endif
</div>
<!-- Password -->
<div>
<label>Passwort</label>
<div class="password-container">
<input id="password" type="password" name="password" class="form-control" placeholder="Passwort"
required id="password" />
<i class="bi-eye" id="eye"></i>
@if ($errors->has('password'))
<code>{{ $errors->first('password') }}</code>
@endif
</div>
</div>
<p class=forgot-password>
<a href="{{ url('forgot-password') }}"> Passwort vergessen?</a>
</p>
<div>
<input type="submit" class="btn" id="login-btn" name="login_btn" value="Anmelden" />
</div>
<div>
<p class="neu-anmeldung">
Sie sind neu bei uns und wollen ein neues Kundenkonto anlegen?
<a href="{{ route('registrierung.create') }}"> Hier geht es zur Neuanmeldung</a>
</p>
</div>
</form>
</div>
<!-- ======= Info Banner ======= -->
<div class="highlights-container-two">
<div class="highlights">
<div class="card-three">
<img class="klebestreifen-two" />
<img src="frontend/assets/images/media_64586f31673d1.JPG" />
</div>
<div class="card-one">
<blockquote>
<h2>Sommermode für sie</h2>
<img src="frontend/assets/images/Wellegrünkurz.svg" />
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Laudantium dicta reiciendis dolores molestiae? Quis expedita rem
odit similique rerum, velit consequuntur consectetur atque optio,
numquam corrupti repellendus deserunt natus! Quo? Lorem ipsum
dolor sit amet consectetur, adipisicing elit. Consequuntur labore
rem, consequatur repudiandae esse consectetur. Voluptatem rem,
placeat commodi ipsum deserunt nam laudantium hic eius nulla.
Expedita itaque odio quisquam.
</p>
</blockquote>
</div>
</div>
</div>
@endsection
@gych It is also designed to prevent the user from accessing the vendor or admin dashboard and vice a versa. The same applies to the admin dashboard-it cannot access the vendor or the user dashboard.
This is the RoleMiddleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RoleMiddleware
{
public function handle(Request $request, Closure $next, $role)
{
$user = Auth::user();
if ($role !== $user->role) {
if ($user->role === 'admin') return redirect()->route('admin.dashboard');
if ($user->role === 'vendor') return redirect()->route('vendor.dashboard');
if ($user->role === 'user') return redirect()->route('user.dashboard');
}
return $next($request);
}
}
This is the RouteServiceProvider:
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to your application's "home" route.
*
* Typically, users are redirected here after authentication.
*
* @var string
*/
public const HOME = '/user/dashboard';
/**
* Define your route model bindings, pattern filters, and other route configuration.
*/
public function boot(): void
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->group(base_path('routes/web.php'));
Route::middleware(['web', 'auth', 'role:admin'])
->prefix('admin')
->as('admin.')
->group(base_path('routes/admin.php'));
Route::middleware(['web', 'auth', 'role:vendor'])
->prefix('vendor')
->as('vendor.')
->group(base_path('routes/vendor.php'));
});
}
}
@mamunsson Ok and do other parts of this header display or don't you see any of them?
<header class="header">
<div class="greeting">
<p>Hallo, Amanda!</p>
</div>
<div class="icons">
<i class="bi bi-person-fill"></i> <a href="{{route('login')}}">Anmelden</a>
<span id="cart-count">0</span>
<i class="bi bi-bag"></i> <a href="#">Einkaufswagen</a>
</div>
</header>
```
@gych Currently, when I click on Anmelden i.e. the route login, I get http://127.0.0.1:8000/user/dashboard in the browser's address bar and the page shows 404 | Not Found
@Mamunsson Is it possible that you have multiple routes that use 'login' as route name? Is your application still using the default auth.php route file which is included in web.php with breeze by default ?
Also the route path for this route is not correct, you are using auth.login as route path.
You should use /admin/login or another path that fits your needs but don't use . in the route path. Only use it for route names.
It also seems that the route is inside the admin.php routes file, which makes it not accessable because the user needs to be authorized as admin to access this route.
@gych This is what I currently have, but the problem persists:
<?php
use App\Http\Controllers\UserProfileController;
use App\Http\Controllers\UserAddressController;
use App\Http\Controllers\UserDashboardController;
use Illuminate\Support\Facades\Route;
/** Laravel Breeze routes that update the User's username, email and password */
Route::middleware(['auth', 'verified', 'role:user'])
->prefix('user')
->name('user.')
->group(function () {
Route::get('/login', [UserDashboardController::class, 'showLoginForm'])->name('login');
Route::get('dashboard', [UserDashboardController::class, 'dashboard'])->name('user.dashboard');
Route::get('profile', [UserProfileController::class, 'index'])->name('profile');
Route::put('profile', [UserProfileController::class, 'updateProfile'])->name('profile.update');
Route::post('profile', [UserProfileController::class, 'updatePassword'])->name('profile.update.password');
});
/** User Address route */
Route::get('user/dashboard/create', [UserAddressController::class, 'create'])->name('address.create');
Route::post('user/dashboard/address', [UserAddressController::class, 'store'])->name('address.store');
Route::get('user/dashboard/address/{id}/edit', [UserAddressController::class, 'edit'])->name('address.edit');
Route::put('user/dashboard/address/{id}', [UserAddressController::class, 'update'])->name('address.update');
Route::delete('user/dashboard/address/{id}', [UserAddressController::class, 'destroy'])->name('address.destroy');
UserDashboardController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDashboardController extends Controller
{
public function showLoginForm ()
{
return view('auth.login');
}
}
nav.blade.php
<!-- ======= Header ======= -->
<header class="header">
<div class="greeting">
<p>Hallo, Amanda!</p>
</div>
<div class="icons">
<i class="bi bi-person-fill"></i> <a href="{{ route('login') }}">Anmelden</a>
<span id="cart-count">0</span>
<i class="bi bi-bag"></i> <a href="#">Einkaufswagen</a>
</div>
</header>
<!-- ======= Navigation ======= -->
<nav class="navbar">
<div class="container">
<h1>The Online Shop</h1>
<ul>
<li>
<form role="search" method="get" class="search-form" action="">
<span class="fa fa-search form-control-feedback"></span>
<input type="search" class="search-field" placeholder="Search the Online Shop" />
<button type="submit" class="search-submit">
Finden
</button>
<input type="hidden" value="product" name="post_type" id="post_type">
</form>
</li>
</ul>
</div>
</nav>
<!-- ======= Selection Menu ======= -->
<div class="selection-menu">
<ul>
<li><a href="#">Immobilien</a></li>
<li><a href="#">Möbel</a></li>
<li><a href="#">Kleidung</a></li>
<li><a href="#">Auto</a></li>
<li><a href="#">Elektronik</a></li>
<li><a href="#">Antiquitäten</a></li>
<li><a href="#">Sport</a></li>
<li><a href="#">Sale %</a></li>
</ul>
</div>
UserAddressController:
<?php
namespace App\Http\Controllers;
// This adds the UserAddress Model to the controller
use App\Models\UserAddress;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class UserAddressController extends Controller
{
// This will return the address page
public function address()
{
$addresses = UserAddress::where('user_id', Auth::user()->id)->get();
return view('user.dashboard.address.index', compact('addresses'));
}
// This will return the create page
public function create()
{
return view('user.dashboard.address.create');
}
public function store(Request $request)
{
// This adds validation to the user input fields
$request->validate([
'name' => ['required', 'max:200'],
'email' => ['required', 'max:200', 'email'],
'phone' => ['required', 'max:200'],
'country' => ['required', 'max:200'],
'state' => ['required', 'max:200'],
'city' => ['required', 'max:200'],
'zip' => ['required', 'max:200'],
'address' => ['required', 'max:200'],
]);
// This stores the user input into the database
$address = new UserAddress();
$address->user_id = Auth::user()->id;
$address->name = $request->name;
$address->email = $request->email;
$address->phone = $request->phone;
$address->country = $request->country;
$address->state = $request->state;
$address->city = $request->city;
$address->zip = $request->zip;
$address->address = $request->address;
$address->save();
// This displays the toast message
toastr('Updated Successfully!')->success('Address successfully added!');
return redirect()->route('address');
}
// This will update an existing address
public function edit(string $id)
{
$address = UserAddress::findOrFail($id);
return view('user.dashboard.address.edit', compact('address'));
}
public function update(Request $request, string $id)
{
$request->validate([
'name' => ['required', 'max:200'],
'email' => ['required', 'max:200', 'email'],
'phone' => ['required', 'max:200'],
'country' => ['required', 'max:200'],
'state' => ['required', 'max:200'],
'city' => ['required', 'max:200'],
'zip' => ['required', 'max:200'],
'address' => ['required'],
]);
$address = UserAddress::findOrFail($id);
$address->user_id = Auth::user()->id;
$address->name = $request->name;
$address->email = $request->email;
$address->phone = $request->phone;
$address->country = $request->country;
$address->state = $request->state;
$address->city = $request->city;
$address->zip = $request->zip;
$address->address = $request->address;
$address->save();
toastr('Updated Successfully!', 'success', 'Success')->success('Updated Successfully!');
return redirect()->route('address');
}
// This will delete an address
public function destroy(string $id)
{
$address = UserAddress::findOrFail($id);
$address->delete();
return response(['status' => 'success', 'message' => 'Deleted Successfully!']);
}
}
UserProfileController
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use File;
class UserProfileController extends Controller
{
public function index()
{
return view('user.dashboard.profile');
}
public function updateProfile(Request $request)
{
$request->validate([
'name' => ['required', 'max:100'],
'email' => ['required', 'email', 'unique:users,email,' . Auth::user()->id],
'image' => ['image', 'max:2048'],
]);
$user = Auth::user();
if ($request->hasFile('image')) {
if (File::exists(public_path($user->image))) {
File::delete(public_path($user->image));
}
$image = $request->image;
$imageName = rand() . '_' . $image->getClientOriginalName();
$image->move(public_path('images'), $imageName);
$path = 'images/' . $imageName;
$user->image = $path;
}
$user->name = $request->name;
$user->email = $request->email;
$user->save();
toastr()->success('Profile Updated Successfully!');
return redirect()->back();
}
public function updatePassword(Request $request)
{
$request->validate([
'current_password' => ['required', 'current_password'],
'password' => ['required', 'confirmed', 'min:8']
]);
$request->user()->update([
'password' => bcrypt($request->password)
]);
toastr()->success('Profile Password Updated Successfully');
return redirect()->back();
}
}
@gych It keeps taking me to http://127.0.0.1:8000/user/dashboard when I click on the login link in nav.blade.php
@Mamunsson Hey, When it redirects you to that dashboard it seems like you're already authenticated (logged in)
I also see is that your login route is inside a route group with auth middleware. It should not be inside this group because when the auth middleware is applied the route will only be accessible for authenticated users.
@gych Hi, This is what I've inside the RoleMiddleware.php:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RoleMiddleware
{
public function handle(Request $request, Closure $next, $role)
{
$user = Auth::user();
if ($role !== $user->role) {
if ($user->role === 'admin') return redirect()->route('admin.dashboard');
if ($user->role === 'vendor') return redirect()->route('vendor.dashboard');
if ($user->role === 'user') return redirect()->route('user.dashboard');
}
return $next($request);
}
}
@Mamunsson Are you sure you're not logged in while testing this?
First put the login get route outside of the user route group
Route::get('/user/login', [UserDashboardController::class, 'showLoginForm'])->name('user.login');
Route::middleware(['auth', 'verified', 'role:user'])
->prefix('user')
->name('user.')
->group(function () {
Route::get('dashboard', [UserDashboardController::class, 'dashboard'])->name('user.dashboard');
Route::get('profile', [UserProfileController::class, 'index'])->name('profile');
Route::put('profile', [UserProfileController::class, 'updateProfile'])->name('profile.update');
Route::post('profile', [UserProfileController::class, 'updatePassword'])->name('profile.update.password');
});
@gych You're right, I think I'm logged in but due to the problem I'm unable to log out.
@Mamunsson Do you have a route to logout ? Breeze starter kit has a logout route by default, its located in routes/auth.php
Route::middleware('auth')->group(function () {
// Other routes that come by default Breeze starter kit
Route::get('logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');
});
@gych I don't see one inside the web.php
@Mamunsson It should be inside auth.php if you've not deleted that file.
Otherwise if you're still using the default AuthenticatedSessionController add the route I included in my previous reply to your routes.
@gych I'm sure I didn't delete it, and found an auth.php file inside the config folder:
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expiry time is the number of minutes that each reset token will be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
| The throttle setting is the number of seconds a user must wait before
| generating more password reset tokens. This prevents the user from
| quickly generating a very large amount of password reset tokens.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_reset_tokens',
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
When I added your code inside the web.php the AuthenticatedSessionController: was highlighted in red by VS Code since it appears there is no controller.
@Mamunsson You're getting the error about AuthenticatedSessionController because you've to add this at the top in your route file so it knows the namespace of the controller.
use App\Http\Controllers\Auth\AuthenticatedSessionController;
With auth.php I don't mean the config file, by default Breeze starter kit creates an auth.php file with routes in the routes folder in your application routes/auth.php
@gych Oh, I see. This is what the auth.php file contains:
<?php
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\ConfirmablePasswordController;
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
use App\Http\Controllers\Auth\EmailVerificationPromptController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\PasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\Auth\VerifyEmailController;
use Illuminate\Support\Facades\Route;
Route::middleware('guest')->group(function () {
Route::get('register', [RegisteredUserController::class, 'create'])
->name('register');
Route::post('register', [RegisteredUserController::class, 'store']);
Route::get('login', [AuthenticatedSessionController::class, 'create'])
->name('login');
Route::post('login', [AuthenticatedSessionController::class, 'store']);
Route::get('forgot-password', [PasswordResetLinkController::class, 'create'])
->name('password.request');
Route::post('forgot-password', [PasswordResetLinkController::class, 'store'])
->name('password.email');
Route::get('reset-password/{token}', [NewPasswordController::class, 'create'])
->name('password.reset');
Route::post('reset-password', [NewPasswordController::class, 'store'])
->name('password.store');
});
Route::middleware('auth')->group(function () {
Route::get('verify-email', EmailVerificationPromptController::class)
->name('verification.notice');
Route::get('verify-email/{id}/{hash}', VerifyEmailController::class)
->middleware(['signed', 'throttle:6,1'])
->name('verification.verify');
Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
->middleware('throttle:6,1')
->name('verification.send');
Route::get('confirm-password', [ConfirmablePasswordController::class, 'show'])
->name('password.confirm');
Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']);
Route::put('password', [PasswordController::class, 'update'])->name('password.update');
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
->name('logout');
});
@Mamunsson Yes indeed that's the file, at the bottom you see that there's already a logout route in that file so no need to add it yourself.
Now test if the logout route still works YOUR_APP_URL/logout
@gych I got this message: The GET method is not supported for route logout. Supported methods: POST when I typed: http://127.0.0.1:8000/logout
@Mamunsson Ah yes I see that its because the logout route is a POST route, change it to GET
Route::get('logout', [AuthenticatedSessionController::class, 'destroy'])
->name('logout');
@gych Awesome, that did it, I'm logged out now. Wow, that was tough. Many thanks indeed for your support.
Ok, now I've this inside the user.php route:
Route::get('/auth/login', [UserDashboardController::class, 'showLoginForm'])->name('user.login');
and this inside the UserDashboardController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDashboardController extends Controller
{
public function showLoginForm ()
{
return view('/auth/login');
}
}
My understanding is that this logic should show the login page which lives inside the auth folder created by Breeze. Instead, I'm getting http://127.0.0.1:8000/user/dashboard in the browser address bar and 404 Page not found.
@Mamunsson You are returning the route path in the view method which is not correct, you need to return your blade view.
Use this instead, double check if its the correct view this example should be located in views/auth/login.blade.php
return view('auth.login');
@gych The route:
Route::get('/auth/login', [UserDashboardController::class, 'showLoginForm'])->name('user.login');
The controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDashboardController extends Controller
{
public function showLoginForm ()
{
return view('auth.login');
}
}
I'm still getting the same issue.
@Mamunsson Did you move the login route outside of the route group it was previously in?
First try to run php artisan route:clear in your terminal, maybe your previous routes are cached.
@gych Many thanks indeed, that did it, I'm now able to access the login page by clicking on the login link. I now need to see how I can access the user's dashboard itself as well as the vendor's dashboard and the admin dashboard when I enter the login credentials.
@Mamunsson No problem :) I'm glad it works now!
For redirecting the user to the right dashboard, after logging in you could check the role and redirect to the correct dashboard. If you can't figure it out, create a new thread with what you've already tried.
@gych Hi, Many thanks indeed. I was able to login to all three dashboards. I had to add this to the RouteServiceProvider:
Route::middleware(['web', 'auth', 'role:user'])
->prefix('user')
->as('user.')
->group(base_path('routes/user.php'));
However, I have one issue with the address link for the vendor dashboard and I'm going to start another thread.
Please or to participate in this conversation.