Login form is not directing to dashboard page after user logs in - laravel

I am trying to redirect logged in users to the main dashboard view, that I've titled as "xwelcome.blade.php" temporarily.
I've attached my routes:
Route::get('xwelcome', [CustomAuthController::class, 'xwelcome']);
Route::get('login', [CustomAuthController::class, 'index'])->name('login');
Route::post('custom-login', [CustomAuthController::class, 'customLogin'])->name('login.custom');
Route::get('registration', [CustomAuthController::class, 'registration'])->name('register-user');
Route::post('custom-registration', [CustomAuthController::class, 'customRegistration'])->name('register.custom');
Route::get('logout', [CustomAuthController::class, 'logOut'])->name('logout');
This is my CustomAuthController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class CustomAuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function customLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('xwelcome')
->withSuccess('Signed in');
}
return redirect("login")->withSuccess('Login details are not valid');
}
public function registration()
{
return view('auth.registration');
}
public function customRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
return redirect("xwelcome")->withSuccess('You have signed-in');
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
public function dashboard()
{
if(Auth::check()){
return view('xwelcome');
}
return redirect("login")->withSuccess('You are not allowed to access');
}
public function logOut() {
Session::flush();
Auth::logout();
return Redirect('login');
}
}
Login button:
<form role="form" method="post" action="{{ route('login.custom') }}">
#csrf
<div class="mt-4">
<div class="form-group">
<label class="form-control-label">Email address</label>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input type="email" class="form-control" id="input-email" placeholder="name#example.com">
</div>
</div>
<div class="form-group mb-4">
<div class="d-flex align-items-center justify-content-between">
<div>
<label class="form-control-label">Password</label>
</div>
<div class="mb-2">
Lost password?
</div>
</div>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
<input type="password" class="form-control" id="input-password" placeholder="Password">
<div class="input-group-append">
<span class="input-group-text">
<a href="#" data-toggle="password-text" data-target="#input-password">
<i class="fas fa-eye"></i>
</a>
</span>
</div>
</div>
</div>
<button type="submit" class="btn btn-sm btn-primary btn-icon rounded-pill">
<span class="btn-inner--icon"><i class="fas fa-long-arrow-alt-right"></i></span>
Sign In
</button></div>
</form>
After I fill in my test account details, and press the button, I see this in my address bar
https://example.com/login?_token=0GddRaD453thhRFL7KWgEQ5DnZ3q7B4rL5gaajuM
Any help would be appreciated. Cheers

I can't see the input fields in your form!.
Have you tried to display the errors after validating the form?.
I think that you are redirected back to :
https://example.com/login?_token=0GddRaD453thhRFL7KWgEQ5DnZ3q7B4rL5gaajuM
because you are missing the input:email and input:password, what your are seeing is only the csrf token that laravel generates.

i Think issue is on button . try this out
<form role="form" method="post" action="{{ route('login.custom') }}">
#csrf
<div class="mt-4">
<button type="submit" class="btn btn-sm btn-primary btn-icon rounded-pill">
<span class="btn-inner--icon"><i class="fas fa-long-arrow-alt-right"></i></span>
Sign In
</button></div>
</form>

you are missing name attribute in your input field. the code should look like
<form role="form" method="post" action="{{ route('login.custom') }}">
#csrf
<div class="mt-4">
<div class="form-group">
<label class="form-control-label">Email address</label>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input name="email" type="email" class="form-control" id="input-email" placeholder="name#example.com">
</div>
</div>
<div class="form-group mb-4">
<div class="d-flex align-items-center justify-content-between">
<div>
<label class="form-control-label">Password</label>
</div>
<div class="mb-2">
Lost password?
</div>
</div>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
<input name="password" type="password" class="form-control" id="input-password" placeholder="Password">
<div class="input-group-append">
<span class="input-group-text">
<a href="#" data-toggle="password-text" data-target="#input-password">
<i class="fas fa-eye"></i>
</a>
</span>
</div>
</div>
</div>
<button type="submit" class="btn btn-sm btn-primary btn-icon rounded-pill">
<span class="btn-inner--icon"><i class="fas fa-long-arrow-alt-right"></i></span>
Sign In
</button></div>
</form>

Related

multiple field validation using livewire ? name.0.required is working but name.*.required is not working. Please provide me solution

I want to validate all field. But It is validating only first field. Append field is not validating
multiple fields validation using livewire? name.0.required is working but name.*.required is not working.
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class Test extends Component
{
public $name;
public $inputs = [];
public $i = 1;
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
}
public function remove($i)
{
unset($this->inputs[$i]);
}
public function store()
{
$validatedDate = $this->validate([
'name.0' => 'required',
'name.*' => 'required',
],
[
'name.0.required' => 'name field is required',
'name.*.required' => 'name field is required',
]
);
session()->flash('message', 'Name Has Been Created Successfully.');
}
public function render()
{
return view('livewire.test');
}
}
<div>
<form class="offset-md-3">
#if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
<div class="add-input">
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" wire:model="name.0">
#error('name.0') <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
<div class="col-md-2">
<button class="btn text-white btn-info btn-sm" wire:click.prevent="add({{$i}})">Add</button>
</div>
</div>
</div>
#foreach($inputs as $key => $value)
<div class=" add-input">
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" wire:model="name.{{ $value }}">
#error('name.'.$value) <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
<div class="col-md-2">
<button class="btn btn-danger btn-sm" wire:click.prevent="remove({{$key}})">remove</button>
</div>
</div>
</div>
#endforeach
<div class="row">
<div class="col-md-12">
<button type="button" wire:click.prevent="store()" class="btn btn-success btn-sm">Save</button>
</div>
</div>
</form>
</div>
This is my full code use to create multi field using livewire. I am not able to validate appended field so I need help to solve this problem. This validate first field name.0 other append field name.* does not validate.
You should use $key instead $value.
Like this:
wire:model="name.{{ $key }}"

Laravel store doesn't save anything

I am making an application in Laravel. I've created a form to save a model (Repair). But when I save the form, you see it redirecting, but nothing saves. No error or success message is shown?
The store function in the RepairController.php:
public function store(Request $request)
{
$this->validate($request, [
'customer_id' => 'required',
'defect' => 'required',
'stats_id' => 'required'
]);
Repair::create([
'customer_id' => $request->customer_id,
'serial_number' => $request->serial_number,
'model' => $request->model,
'defect' => $request->defect,
'diagnostics' => $request->diagnostics,
'comments' => $request->comments,
'status_id' => $request->status_id
]);
return redirect()->route('repair.index')->with('success',$msg);
}
Repair.php has a fillable:
protected $fillable = ['customer_id', 'serial_number', 'model', 'defect', 'diagnostics', 'comments', 'status_id'];
The create modal:
<div id="newModal" class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog modal- modal-dialog-centered modal-" role="document">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" id="modal-title-default">Type your modal title</h6>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{ route('repair.store') }}" method="POST" id="editForm">
#csrf
<input type="text" name="customer_id" class="form-control" placeholder="Customer ID"><br>
<input type="text" name="serial_number" class="form-control" placeholder="Serial number"><br>
<input type="text" name="model" class="form-control" placeholder="Model"><br>
<input type="text" name="defect" class="form-control" placeholder="Defect"><br>
<div class="form-group">
<label for="Diagnostics">Diagnostics</label>
<textarea name="diagnostics" class="form-control" id="Diagnostics" rows="3"></textarea>
</div>
<div class="form-group">
<label for="Comments">Comments</label>
<textarea name="comments" class="form-control" id="Comments" rows="3"></textarea>
</div>
<input type="text" name="status_id" class="form-control" placeholder="Status ID"><br>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Create repair</button>
<button type="button" class="btn btn-link ml-auto" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
The route in web.php:
Route::resource('repair', 'RepairController');
There might be some validation fails in your data. Can you add the below code above your form and see what it prints.
#if($errors->any())
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
#endif

Laravel - Session flash did not display the content of the error message

I am using session flash in my Laravel-5.8 project.
Controller
<?php
namespace App\Http\Controllers\Appraisal;
use App\Http\Controllers\Controller;
use App\Models\Appraisal\AppraisalSkill;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Exception;
use Illuminate\Support\Facades\Validator;
use Session;
class AppraisalSkillsController extends Controller
{
public function create()
{
abort_unless(\Gate::allows('skill_create'), 403);
return view('appraisal.skills.create');
}
public function store(Request $request)
{
abort_unless(\Gate::allows('skill_create'), 403);
$this->validate($request, [
'skill_name' => 'required|unique:appraisal_skills,company_id',
]);
$skill = AppraisalSkill::create([
'skill_name' => $request->skill_name,
'description' => $request->description,
'company_id' => Auth::user()->company_id,
'created_by' => Auth::user()->id,
'created_at' => date("Y-m-d H:i:s"),
'is_active' => 1,
]);
Session::flash('success', 'Appraisal Skill is created successfully');
return redirect()->route('appraisal.skills.index');
}
}
view/partials/_messages.blade.php
#if (count($errors) > 0)
<div class="alert alert-danger alert-block" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Errors: </strong>
<ul>
#foreach ($errors as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Session::has('success'))
<div class="alert alert-success" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Success: </strong>{{Session::get('success')}}
</div>
#endif
view
<div class="container-fluid">
<div class="panel-heading clearfix">
<div class="float-right">
<div class="btn-group btn-group-sm" role="group">
<a href="{{ route("appraisal.skills.index") }}" class="btn bg-navy margin" title=" Back">
<span> Back to List</span>
</a>
</div>
</div>
</div>
<br>
#include('partials._messages')
<br>
<div class="card">
<div class="card-header">
Create Skill
</div>
<div class="card-body">
<form action="{{route('appraisal.skills.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Skill Name<span style="color:red;">*</span></label>
<div class="col-md-9 controls">
<input type="text" name="skill_name" placeholder="Enter skill name here" class="form-control" value="{{old('skill_name')}}">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Description</label>
<div class="col-md-9">
<textarea rows="2" name="description" class="form-control" placeholder="Enter Description here" value="{{old('description')}}"></textarea>
</div>
</div>
</div>
</div>
</div>
<div>
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.skills.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
</div>
</div>
</div>
When I click on save submit button, I expect that if there is any error it should display the detail of the error. But, rather it only display Error: without the details.
The success message is working, but the error message is not working as expected
How do I get this resolved?
Thank you.
There is a little mistake when looping through the errors. Change line #foreach ($errors as $error) to #foreach ($errors->all() as $error) . It should work now!

Add [title] to fillable property to allow mass assignment on [App\Profile]

I am trying to create edit profile, but when I click on edit profile button I'm getting below error:
Illuminate \ Database \ Eloquent \ MassAssignmentException Add [title]
to fillable property to allow mass assignment on [App\Profile]
show.blade.php :
<#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-4">
<img src="https://scontent-cdt1-1.cdninstagram.com/vp/dcca3b442819fc8b9b63f09b2ebde320/5DA9E3CB/t51.2885-19/s150x150/40101184_290824334847414_1758201800999043072_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com" class="rounded-circle">
</div>
<div class="col-8">
<div class="d-flex align-items-baseline">
<div class="h4 mr-3 pt-2">{{ $user->username }}</div>
<button class="btn btn-primary">S'abonner</button>
</div>
<div class="d-flex">
<div class="mr-3">{{ $user->posts->count() }} article(s) en vente
</div>
Modifier Profile
<div class="mt-3">
<div class="font-weight-bold">
{{ $user->profile->title }}
</div>
<div class="font-weight-bold">
{{ $user->profile->description }}
</div>
</div>
</div>
</div>
<div class="row mt-5">
#foreach ($user->posts as $post)
<div class="col-4">
<img src="{{ asset('storage') . '/' . $post->image }}" class="w-100">
</div>
#endforeach
</div>
</div>
#endsection
ProfileController :
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function show(User $user)
{
return view('profile.show', compact('user'));
}
public function edit(User $user)
{
return view('profile.edit', compact('user'));
}
public function update(User $user)
{
$data = request()->validate([
'title' => 'required',
'description' => 'required'
]);
$user->profile->update($data);
return redirect()->route('profile.show', ['user' => $user]);
}
}
edit.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">Modifier profile</div>
<div class="card-body">
<form method="POST" action="{{ route('profile.update', ['user' => $user]) }}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group">
<label for="title">Titre</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') ?? $user->profile->title }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<div class="col-md-6">
<textarea id="description" type="text" class="form-control #error('description') is-invalid #enderror" name="description" autocomplete="description" autofocus>{{ old('description') ?? $user->profile->description }}</textarea>
#error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input #error('image') is-invalid #enderror" id="validatedCustomFile" >
<label class="custom-file-label" for="validatedCustomFile">Choisir une image</label>
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Modifier profile
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $guarder = [];
public function user()
{
return $this->belongsTo('App\User');
}
}
What am I doing wrong here and how can I get rid of this error?
You have a spelling error, instead of $guarder add this in your model:
protected $guarded = [];
I won't advise using empty guarded but use $fillable instead.
In the same controller model, free access to database fields with
protected $guarded = [];
or
protected $fillable = [];

I got unauthorized page after submit change password form and call logout function in laravel 5

I have overridden postReset method in PassowrdController:
public function postReset(Request $request)
{
$data = Input::all();
$rules = array(
'token' => 'required',
'current_password' => 'required|currentpasscheck',
'password' => 'required|confirmed',
);
$messages = array(
'currentpasscheck' => 'Your old password was incorrect',
);
Validator::extend('currentpasscheck', function ($attribute, $value, $parameters)
{
return Hash::check($value, Auth::user()->getAuthPassword());
});
$validation = Validator::make($data, $rules, $messages);
if ($validation->fails())
{
// Validation has failed.
return Redirect::to('password-reset')
->withErrors($validation)
->withInput();
}
$user = Auth::user();
$user->password = bcrypt($data['password']);
$user->save();
$this->auth->logout();
return Redirect::to('/');
}
route:
Route::post('/password/reset', 'Auth\PasswordController#postReset');//this can be accessed by auth user
reset.blade.php
#extends('app')
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Password</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('/password/reset') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group">
<!-- <label class="col-md-4 control-label">E-Mail Address</label>-->
<label class="col-md-4 control-label">Current Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="current_password" value="{{ old('current_password') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
</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
but after submit change password form I got unauthorized page.

Resources