Route [/admin/update-pwd] not defined - laravel

I am trying to change the admin password but it says: Route [/admin/update-pwd] not defined. How do I change it?
controller:
public function changepassword()
{
return view('admin.changepassword');
}
public function chkPassword(Request $request){
$data = $request->all();
$adminCount = Admin::where(['username' => Session::get('adminSession'),'password'=>md5($data['current_pwd'])])->count();
if ($adminCount == 1) {
echo "true"; die;
} else {
echo "false"; die;
}
}
public function updatePassword(Request $request){
if($request->isMethod('post')){
$data = $request->all();
$adminCount = Admin::where(['username' => Session::get('adminSession'),'password'=>md5($data['current_pwd'])])->count();
if ($adminCount == 1) {
$password = md5($data['new_pwd']);
Admin::where('username',Session::get('adminSession'))->update(['password'=>$password]);
return redirect('/admin/settings')->with('flash_message_success', 'Password updated successfully.');
}else{
return redirect('/admin/settings')->with('flash_message_error', 'Current Password entered is incorrect.');
}
}
}
blade file:
<form method="POST" action="{{ route('/admin/update-pwd') }}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<strong class="text-muted d-block mb-2">Enter Current Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="current_pwd" class="form-control #error('password') is-invalid #enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
<strong class="text-muted d-block mb-2">Enter New Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="new_pwd" class="form-control #error('password') is-invalid #enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
<strong class="text-muted d-block mb-2">Confirm New Password</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="password" name="confirm_pwd" class="form-control #error('password') is-invalid #enderror" id="form2-password" placeholder="Password">
<span class="input-group-append">
<span class="input-group-text">
<i class="material-icons">lock</i>
</span>
</span>
</div>
</div>
#error('password')
<div class="small text-danger">{{ $message }}</div>
#enderror
<button type="submit" class="mb-2 btn btn-primary mr-2">Update Password</button>
</form>
Routes:
Route::get('/admin/changepassword', 'ChangepasswordController#changepassword');
Route::get('/admin/check-pwd','AdminController#chkPassword');
Route::match(['get', 'post'],'/admin/update-pwd','AdminController#updatePassword');

When you use the route helper it expects that you provide a route name not a URI, so add this:
Route::match(['get', 'post'],'/admin/update-pwd','AdminController#updatePassword')
->name('admin.update_password');
Then in your view you can use:
route('admin.update_password');
Or use the url helper method instead:
// instead of this
{{ route('/admin/update-pwd') }}
//use
{{ url('/admin/update-pwd') }}

You are trying to use Laravel named route inside your form method. Use Laravel Named Routed, and define it inside your form.
//inside web.php
Route::post('/admin/update-pwd','AdminController#updatePassword')->name('admin/update-pwd');
Then you will be able to use inside form
<form method="POST" action="{{ route('/admin/update-pwd') }}" enctype="multipart/form-data">

Related

Laravel 8 How to restrict user login if email is not verified

I am using Bootstrap Starter template package in Laravel 8 that replaces Tailwind CSS framework with Bootstrap CSS framework.
https://packagist.org/packages/shahvirag/laravel-ui-bootstrap
The problem I'm having is that user can sign in after registration without needing to verify account through email. How can I restrict his access until the email is not verified?
I have followed the steps and tried using routes middleware, but there is no such file in App\Http\Controllers\PagesController;
Route::get('/',[PagesController::class, 'index'])->name('users')->middleware('verified'); // this does not work
Any help?
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;
Route::get('/', function () {
return view('main');
});
Route::middleware(['auth'])->group(function() {
Route::get('/home', function() {
return view('home');
})->name('home');
Route::get('/user/profile', function() {
return view('profile');
})->name('profile');
});
Route::get('/',[PagesController::class, 'index'])->name('users')->middleware('verified');
resources/views/auth/login.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
#if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
#endif
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
To use verified middleware, your user model should implement MustVerifyEmail
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
Try this instead
Route::get('/',[PagesController::class, 'index'])->name('users')->middleware(['auth', 'verified']);
You can use the method included with the User model:
$user->hasVerifiedEmail()
Or in the routes middleware(['verified'])

How do I create date in register form

How do I create date of birth or date in register form? I am trying to make Register form. How can I do this?
Output error
In user models:
public function setDateOfBirthAttribute($value)
{
$this->attributes['date_of_birth'] = $value ? Carbon::createFromFormat(config('project.date_format'), $value)->format('Y-m-d') : null;
}
In Blade files:
<div class="form-group bmd-form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="material-icons">event</i>
</span>
</div>
<input name="date_of_birth" type="date" class="form-control" placeholder="{{ trans ('global.date_of_birth') }}..." value="{{ old('date_of_birth' }} " required autocomplete="name" autofocus> config('project.date_format')
</div>
#error('date_of_birth')
<div class="error" for="date_of_birth">
{{ $message }}
</div>
#enderror
</div>

validation error regarding the validation file in laravel

I am calling a validation request but i am getting this error.
Method Illuminate\Validation\Validator::validateFile, does not exist.
My UserRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Http\Controllers\ProfileController;
class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name'=> 'string|required|max:255',
'username'=> 'string|required|max:255|alpha_dash',
'avatar'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',
'email'=> 'email|required|string',
'password'=>'required|min:10|string|confirmed',
'background'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',
'description'=>'string|max:255'
];
}
public function messages()
{
return [
'name.required' => 'A name is required',
'username.required' => 'A username is required',
'email.required' => 'An email is required',
'password.required' => 'A password is required',
'description' => 'A description should be string',
];
}
}
My Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use Illuminate\Http\File ;
use App\User;
use Hash;
use Session;
public function update(User $user,UserRequest $request)
{
$user->update(['name'=>request('name'),'username'=>request('username'),'email'=>request('email'),'password'=> Hash::make(request('password')),'avatar'=>$path,'background'=>$background,'description'=>request('description')]);
Session::flash('success', 'You have successfully updated a post!');
return redirect($user->path())->with(['message'=>'Profile updated']);
}
my blade
#extends('layouts.app')
#section('content')
<h1 class="text-gray-700 text-2xl font-bold text-center">Edit Profile</h1>
<div class="mt-5 mb-6">
<img src="{{$user->avatar()}}" alt=""
class="rounded-full mr-2 bottom-0 transform "
width="150px" style="transform: translateX(16.5rem);" >
</div>
<form method="POST" action="{{$user->path() }}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div id="flip" class="p-5 bg-gray-200">Basic Info</div>
<div id="panel">
<div class="mb-6 mt-6">
<label for="avatar">Select Profile to Upload</label>
<input id="avatar" type="file" class="#error('avatar') is-invalid #enderror" name="avatar">
#error('avatar')
<div class="text-red-800">{{ $message }}</div>
#enderror
</div>
<div class="mb-6">
<label for="name">Name</label>
<input id="name" type="text" class="#error('name') is-invalid #enderror border border-gray-400 p-2 w-full" name="name" value="{{$user->name}}">
#error('name')
<div class="text-red-800">{{ $message }}</div>
#enderror
</div>
<div class="mb-6">
<label for="username">Username</label>
<input id="username" type="text" name="username" class="#error('username') is-invalid #enderror border border-gray-400 p-2 w-full" value="{{$user->username}}">
#error('username')
<div class="text-red-800">{{ $message }}</div>
#enderror
</div>
<div class="mb-6">
<label for="email">Email</label>
<input id="email" type="email" name="email" class="#error('email') is-invalid #enderror border border-gray-400 p-2 w-full" value="{{$user->email}}">
#error('email')
<div class="text-red-800">{{ $message }}</div>
#enderror
</div>
<div class="mb-6">
<label for="password">Password</label>
<input id="password" type="password" name="password" class="#error('password') is-invalid #enderror border border-gray-400 p-2 w-full" >
#error('password')
<div class="text-red-800">{{ $message }}</div>
#enderror
</div>
<div class="mb-6">
<label for="password-confirm">Confirm Password</label>
<input id="password-confirm" type="password" name="password-confirm" class="#error('password-confirm') is-invalid #enderror border border-gray-400 p-2 w-full" >
#error('password-confirm')
<div class="text-red-800">{{ $message }}</div>
#enderror
</div>
</div>
<div id="main" class="p-5 bg-gray-200">Background</div>
<div id="child">
<div class="mb-6 mt-6">
<label for="background">Select Background image to Upload</label>
<input id="background" type="file" class="#error('background') is-invalid #enderror" name="background">
#error('background')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
</div>
<div class="mb-6">
<label for="description">Description</label>
<input id="description" type="text" name="description" class="#error('description') is-invalid #enderror border border-gray-400 p-2 w-full" value="{{$user->description}}">
#error('description')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
</div>
</div>
<div class="mt-6">
<button type="submit" class="bg-blue-500 text-white rounded py-2 px-4 hover:bg-blue-800'">Submit</button>
</div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
$("#main").click(function(){
$("#child").slideToggle("slow");
});
});
</script>
#endsection
I have tried the solutions available on stack overflow but still none of them work
Does anyone know why am i getting this error?
Any help will be appreciated.
Remove comma in these validation lines.
'avatar'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',
'background'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',
Add password.confirmed in messages array.
'password.confirmed' => 'YOUR ERROR MESSAGE',

the AJAX ERROR VALIDATION not showing in login page but it showing in developer mode console network in laravel

the problem is when I submit the form the login get success when I do with right creational .But when I login with wrong info the validation message doesn't display error .
<script type="text/javascript">
// Set default CSRF header
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
// Intercept login form
$('#login-form').submit(function(e){
// Prevent normal form submission, we well do in JS instead
e.preventDefault();
// Get form data
var data = {
email: $('[name=email]').val(),
password: $('[name=password]').val(),
remember: $('[name=remember]').val(),
};
// Send the request
$.post($('this').attr('action'), data, function(response) {
if (response.success) {
// If login success, redirect
window.location.replace(response.redirect);
}
});
});
Login Controller from and method here login and registering working very well but the only problem is validation message is not showing .
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
if($request->ajax()){
// If request from AJAX
return [
'success' => true,
'redirect' => $this->redirectPath() ?: route('home'),
];
} else {
// Normal POST do redirect
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
}
html form where id am trying to showing or displaying errors when login
<form id="login-form" class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
You are receiving a validation error in Ajax Format since your request is an Ajax request.
You can handle the errors in the Javascript directly:
I haven't tested it but it should give you some directions.
$.post($('this').attr('action'), data, function(response) {
if (response.success) {
// If login success, redirect
window.location.replace(response.redirect);
} else {
if (response.errors.email)
$('#email-error').text(response.errors.email[0]);
if (response.errors.password)
$('#password-error').text(response.errors.password[0]);
}
});
And modify your blade template as follow:
<form id="login-form" class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required
autofocus>
<span class="help-block" id="email-error"></span>
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
<span class="help-block" id="password-error"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>

Missing required parameters for Route with custom password reset

I'm trying to add new type of users "Client" into existing project.
It already had "Admin" user, which does not have email password reset function.
I duplicated and modified native Laravel out of the box Auth controllers, blade forms and model and now I can login and logout as Client (guard:client).
My problem is in password reset function.
"Forgot Your Password?" button redirects to the right url (http://127.0.0.1:8000/client/password/reset).
"Send Password Reset Link" button generates email with link (http://127.0.0.1:8000/client/password/reset/a7558b0f294af7cdaeafd73617f664e1d76ed27d567d648e8b468eb9edcc9c2d)
But when push that link I get following error:
Missing required parameters for [Route: client.password.reset] [URI:
client/password/reset/{token}]. (View:
C:\wamp64\www\testkz\resources\views\index\client\reset.blade.php)
Where reset.blade.php is a copy of resources\views\auth\passwords\reset.blade.php
My routes
Route::post('/password/email','Client\ClientForgotPasswordController#sendResetLinkEmail')->name('client.password.email');
Route::get('/password/reset','Client\ClientForgotPasswordController#showLinkRequestForm')->name('client.password.request');
Route::post('/password/reset','Client\ClientResetPasswordController#reset');
Route::get('/password/reset/{token}','Client\ClientResetPasswordController#showResetForm')->name('client.password.reset');
ClientResetPasswordController
<?php
namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Password;
use Auth;
class ClientResetPasswordController extends Controller
{
use ResetsPasswords;
protected $redirectTo = '/client';
public function __construct()
{
$this->middleware('guest:client');
}
protected function guard()
{
return Auth::guard('client');
}
protected function broker()
{
return Password::broker('clients');
}
public function showResetForm(Request $request, $token = null)
{
return view('index.client.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
}
blade
#extends('index.layout.layout')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Client Password</div>
<div class="panel-body">
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ route('client.password.reset') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ $email or old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Make sure form method id POST
<form class="form-horizontal" method="POST" action="{{ route('password.request') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
</form>
I upgraded my project from Laravel 5.3 to 5.5, but used old version of reset.blade.php

Resources