after I fixed the error Route [Login] not defined, I can't log in even though I use the correct email and password - laravel-8

this is web.php
Route::get('/login', [LoginController::class, 'index'])->name('login')->middleware('guest');
Route::post('/login', [LoginController::class, 'authenticate']);
Route::post('/logout', [LoginController::class, 'logout']);
this is my LoginController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
public function index()
{
return view('login.index', [
'title' => 'Login'
]);
}
public function authenticate(Request $request)
{
$credentials = $request->validate([
'email' => 'required|email:dns',
'password' => 'required'
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('/admin');
}
return back()->with('loginError', 'Login anda gagal !');
}
public function logout(Request $request)
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/login');
}
}
this is my login view
<main class="form-signin">
#if (session()->has('Sukses'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('Sukses') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
#if (session()->has('loginError'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ session('loginError') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
<form action="/login" method="post">
#csrf
<img class="mb-4" src="/img/discord.png" alt="" width="100" height="99">
<h1 class="h3 mb-3 fw-normal">Please Login</h1>
<div class="form-floating">
<input type="email" name="email" class="form-control #error('email') is-invalid #enderror" id="email" placeholder="name#example.com" autofocus required value="{{ old('email') }}">
<label for="email">Masukkan Email</label>
#error('email')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
<div class="form-floating">
<input type="password" name="password" class="form-control" id="password" placeholder="Password" required>
<label for="password">Password</label>
</div>
<button class="w-100 btn btn-lg btn-primary" type="submit">Login</button>
</form>
<small class="d-block text-center mt-3">Not have any account? Register here</small>
<p class="mt-5 mb-3 text-muted">© 2022</p>
</main>

Related

How to display validation summary below the input controls?

I have the following form:
<form asp-controller="Manage" asp-action="RemoveDetails" method="post" class="form-horizontal">
#if (Model.RequirePassword)
{
<div id="password-container">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" id="password" autocomplete="current-password" aria-required="true" />
<small>
<span asp-validation-for="Password" class="text-danger"></span>
</small>
</div>
</div>
</div>
}
<div class="col-6">
<hr class="mt-2 mb-3">
<button type="submit" class="btn btn-style-1 btn-danger">Confirm</button>
</div>
<div class="row">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
</div>
</form>
If I leave the password empty, a validation message (the model uses DataAnnonations) is displayed right below the control. This is fine.
If I enter the wrong password, the Post action validates it and adds an error to the ModelState. This error is displayed below the form.
Is it possible to display such model errors below the relevant controls?
Try this code in Controller: ModelState.AddModelError("Password", "validation summary error."); The error message won't display in <div asp-validation-summary="ModelOnly"></div>, but it can display error message in #Html.ValidationSummary(false, "", new { #class = "text-danger" }).
<form asp-controller="Home" asp-action="RemoveDetails" method="post" class="form-horizontal">
<div id="password-container">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" id="password" autocomplete="current-password" aria-required="true" />
<small>
#*<span asp-validation-for="Password" class="text-danger"></span>*#
#Html.ValidationSummary(false, "", new { #class = "text-danger" })
</small>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="Description"></label>
<input asp-for="Description" class="form-control" id="description" autocomplete="current-password" aria-required="true" />
<small>
<span asp-validation-for="Description" class="text-danger"></span>
</small>
</div>
</div>
</div>
<div class="col-6">
<hr class="mt-2 mb-3">
<button type="submit" class="btn btn-style-1 btn-danger">Confirm</button>
</div>
<div class="row">
<div asp-validation-summary="ModelOnly"></div>
</div>
</form>
[HttpPost]
public IActionResult RemoveDetails(RemoveDetail rdl)
{
if(ModelState.IsValid)
{
var pwd = rdl.Password;
//do something here and find the password is wrong
//code below won't display error message in <div asp-validation-summary="ModelOnly" class="text-danger">
//but can display error message in #Html.ValidationSummary(false, "", new { #class = "text-danger" })
ModelState.AddModelError("Password", "validation summary error.");
return View(rdl);
}
return View(rdl);
}

Preventing developer message to user frontend

I am developing a website with different pages, and among those pages,
I have a contact form that send message to the backend, the email id is unique, I need to display an error message to the front end that if a user enter the email that exist to then it has to display a message the email is taken. How can I go to achieve this. Here is my code
Controller
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Contact;
use Illuminate\Support\Carbon;
class FrontendPages extends Controller
{
public function About(){
return view('frontend.pages.about');
}//end method
public function Service(){
return view('frontend.pages.services');
}//end method
public function Blog(){
return view('frontend.pages.blog');
}//end method
public function Contact(){
return view('frontend.pages.contact');
}//end method
public function StoreMessage(Request $request){
Contact::insert([
'name' => $request->name,
'email' => $request->email,
'subject' => $request->subject,
'message' => $request->message,
'created_at' =>Carbon::now(),
]);
if ($request->email == 'email') {
return response()->json(['email exist']);
}
$notification = array(
'message' => 'Thank for being in touch we will get back to you soon',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
}
my form
#extends('frontend.main_master')
#section('main')
#php
$footersetup = App\Models\Footer::find(1)->first();
#endphp
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
<div class="container mt-3">
<div class="d-flex justify-content-between align-items-center">
<h2>Contact</h2>
<ol>
<li>Home</li>
<li>Contact</li>
</ol>
</div>
</div>
</section>
<!-- End Breadcrumbs -->
<!-- ======= Contact Section ======= -->
<section id="contact" class="contact">
<div class="container">
<div class="row mt-5">
<div class="col-lg-4">
<div class="info">
<h4>Location:</h4>
<div class="address">
<i class="bi bi-geo-alt"></i>
{!! $footersetup->address !!}<br /><br />
</div>
<div class="email">
<i class="bi bi-envelope"></i>
<h4>Email:</h4>
<p>
{{$footersetup->email}}<br />{{$footersetup->email1}}
</p>
</div>
<div class="phone">
<i class="bi bi-phone"></i>
<h4>Call:</h4>
<p>{{$footersetup->phone}}</p>
</div>
</div>
</div>
<div class="col-lg-8 mt-5 mt-lg-0">
<form
action="{{route('store.message')}}"
method="post"
role="form"
>
#csrf
<div class="row">
<div class="col-md-6 form-group">
<input
type="text"
name="name"
class="form-control"
id="name"
placeholder="Your Name"
required
/>
</div>
<div class="col-md-6 form-group mt-3 mt-md-0">
<input
type="email"
class="form-control"
name="email"
id="email"
placeholder="Your Email"
required
/>
</div>
</div>
<div class="form-group mt-3">
<input
type="text"
class="form-control"
name="subject"
id="subject"
placeholder="Subject"
required
/>
</div>
<div class="form-group mt-3">
<textarea
class="form-control"
name="message"
rows="5"
placeholder="Message"
required
></textarea>
</div>
<div class="my-3">
</div>
<div class="text-center">
<button type="submit" class="btn btn-success">Send Message</button>
</div>
</form>
</div>
</div>
</div>
</section>
<!-- End Contact Section -->
#endsection

Button for update function not working laravel 8 (CRUD)

I'm new to laravel 8. I try to make an update function controller when user already edit their data and store it in database. I try to click submit button and it's not working without any reason when i try to inspect at chrome. So i try another way which is comment the $request->validate and it's only change for name data while organisation_id leave blank and return to NULL. I wonder do we need to always validate the data in update function ?
Here is my Controller
public function edit($id)
{
$record = User::find($id);
$org = Organisation::all();
$t = Organisation::pluck("name");
return view ('user.edit',compact('record','org','t'));
}
public function edit_store(Request $request, $id)
{
$request->validate([
'name' => 'required',
'organisation_id' => 'required',
]
);
$record= User::find($id);
$record->name = $request->name;
$record->organisation_id = $request->organisation_id;
$record->save();
return redirect ('user')->with('success', 'Thank you');
}
Here is my edit blade
<div class="card">
<div class="card-body">
<form action="{{route('user_edit_store1', $record->id)}}" method="POST">
#csrf
<input type="hidden" name="id" value="{{ $record->id }}">
<div class="form-group row">
<label for="name"
class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text"
class="form-control #error('name') is-invalid #enderror" name="name"
value="{{ old('name') ? old('name') : $record->name }}"
</div>
</div>
<div class="form-group row">
<label for="organisation_id"
class="col-md-4 col-form-label text-md-right">{{ __('Organisation') }}</label>
<div class="col-md-6">
{!! Form::open(['route' => 'user_store']) !!}
{!! Form::select('id', $t, 'id', ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary" >
{{ __('Save') }}
</button>
</div>
</div>
</form>
</div>
</div>
Routes
Route::get('user_edit/{id}',[UserController::class,'edit'])->name('user_edit');
Route::post('user_edit_store/{user}',[UserController::class,'edit_store'])->name('user_edit_store1');
Model user.php
protected $fillable = [
'name',
'organisation_id',
];
It appears that the <select> that you're creating with the Form::select form helper will have name of 'id' instead of 'organisation_id' which will result in a validation fail.

Laravel 6 - login with Phone or Email

I'm using Laravel 6 and want user can alternatively login by email or mobile.
My attempts so far:
Migration is working fine.
Registration is working fine
Login works fine one at a time either email or mobile with below changes in loginController.php.
public function username()
{
return 'mobile'; // or email
}
and also
login.blade.php for HTML forms from email to mobile.
I tried to validate both email and phone by #error directive, like #error('email') || #error('mobile') is-invalid #enderror but no action after submit. below is the example
<input id="user_login" type="text" class="form-control #error('email') || #error('mobile') is-invalid #enderror" name="user_login" value="{{ old('user_login') }}" required autocomplete="user_login" autofocus>
#error('user_login')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
Your help will be appreciated.
change User migration:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique()->nullable();
$table->string('mobile_number')->unique()->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
change User model:
protected $fillable = [
'name', 'email', 'password', 'mobile_number'
];
in app\Http\Controllers\Auth\RegisterController.php change:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'mobile_number' => $data['mobile_number'],
'password' => Hash::make($data['password']),
]);
}
then change your register and login views and add a field to get mobile_number
The provided blade template contains incorrect blade syntax that gets transformed to invalid php(the || is converted to plain text instead of the OR operation).
Checkout the resulting php below:
This can be fixed by using the #error and enderror twice:
<form action="{{ route('test.store') }}" method="post">
<input id="user_login" type="text" class="form-control #error('email') is-invalid #enderror #error('mobile') is-invalid #enderror" name="user_login" value="{{ old('user_login') }}" required autocomplete="user_login" autofocus>
#error('user_login')
To add an additional field for authenticating the following can be used:
resources/auth/login.blade.php
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('Phone') }}</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control #error('phone') is-invalid #enderror" name="email" value="{{ old('phone') }}" required autocomplete="email" autofocus>
#error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<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') }}" autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
app/Http/Auth/LoginController.php
Overriding the username() method to use phone if provided and email as fallback.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
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 = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function username()
{
return request()->has('phone') ? 'phone' : 'email';
}
}
or with a single field:
<div class="form-group row">
<label for="user_identifier" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address/Phone') }}</label>
<div class="col-md-6">
<input id="user_identifier" type="text" class="form-control #error('user_identifier') is-invalid #enderror" name="user_identifier" value="{{ old('user_identifier') }}" autofocus>
#error('user_identifier')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
and the associated LoginController method:
public function username()
{
$identifier = request()->get('user_identifier');
if(filter_var($identifier, FILTER_VALIDATE_EMAIL)){
return 'email';
}
return 'phone';
}

IFormFile is null, Razor

I'm trying to bind an image uploading but facing the problem with sending this image to the controller.
I'm using Razor and .net core 2.2.
My Controller
[HttpPost(nameof(MealController))]
[Authorize(Roles = UserRoles.Moderator)]
public IActionResult Update(MealModel meal, IFormFile pic)
{
if (ModelState.IsValid)
{
try
{
var changedMeal = _mapper.Map<MealModel, MealDTO>(meal);
_mealService.Update(changedMeal, pic);
}
catch (Exception e)
{
return NotFound();
}
return RedirectToAction("Index");
}
return View(meal);
}
My form
<form class="form" method="post" asp-controller="Meal" asp-action="Update">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Продукт</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="product">
<div class="product-img">
<div class="selector">
<img src="#Model.ImagePath" class="img-thumbnail" id="selectedImg" alt="Product Image"/>
<div class="form-group">
<input type="file" class="custom-file-input" id="pic-input" name="pic"
onchange="readURL(this, 'selectedImg')">
#* <label class="custom-file-label" for="pic-input"></label> *#
</div>
</div>
</div>
<div class="product-info">
<div class="form-group name">
<label for="name">Назва продукту</label>
<input class="form-control" type="text" placeholder="Назва" id="name" name="name" asp-for="Name"
value="#Model.Name">
</div>
<div class="form-group">
<label for="name">Ціна</label>
<input class="form-control" placeholder="Price" id="price" type="number" asp-for="Price"
value="#Model.Price">
</div>
<div class="form-group">
<label for="name">Вага</label>
<input class="form-control" id="weight" type="number" asp-for="Weight"
value="#Model.Weight">
</div>
<div class="comments">
<textarea class="form-control" rows="3" cols="5" placeholder="Склад" asp-for="Description" value="#Model.Description"></textarea>
</div>
<input type="hidden" asp-for="MealGroupId" value="#Model.MealGroupId"/>
<input type="hidden" asp-for="Id" value="#Model.Id"/>
#* <input type="hidden" asp-for="ImagePath" value="#Model.ImagePath"/> *#
</div>
</div>
</div>
<div class="modal-footer">
<div class="action-button">
<button type="submit"
class="btn btn-success ok-button">
<span>Додати</span>
</button>
<button type="reset"
class="btn btn-danger remove-button">
<span>Відмінити</span>
</button>
</div>
</div>
</div>
</form>
When I debug, IFormFile is always null. I tried using [FromForm] but it didn't help. I googled and found out that my name in form and parameter name was different but it had effect. Could you, please give me a little hint on how I can solve it. Thank you and have a good day!
You have to specify <form>'s enctype attribute set to multipart/form-data to be able to send files to server
<form class="form" method="post" asp-controller="Meal" asp-action="Update" enctype="multipart/form-data">