inserting user in database from regitration form laravel 8 - php

i am working on a e-commerce website using laravel 8 for the first time i created a login and registration views, when i try registering first it flashed iccorrect email adresse in session error that is displayed in register.blade.php after fixing that it redirected me to an error page with this text :
The GET method is not supported for this route. Supported methods: POST.
http://localhost:3000/create?_token=7vVwL33yaBmkmsJIKHl78XfzZNmk4vJDBG9qumhf&email=theoghmir.kodia%40laposte.net&name=HELLh&password=2223541
my route is acctually set to POST did i forget some thing ? here is my web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\authController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/auth', [authController::class, 'authpage']);
Route::get('/auth/check', [authController::class, 'check'])->name('auth.check');
Route::get('/register', [authController::class, 'register']);
Route::post('/create', [authController::class, 'create'])->name('auth.create');
Route::get('/logout', [authController::class, 'logout']);
here the register.blade.php
<form action="{{ route('auth.create')}}" methode="POST">
#if(Session::get('fail'))
<div class="alert alert-danger">{{Session::get('fail')}}</div>
#endif
#if(Session::get('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
#endif
#csrf
<label>name :</label>
<input type="text" class="form-control" name="name" placeholder=" name..." >
<label>Email :</label>
<input type="text" class="form-control" name="email" placeholder=" email..." value="{{old('email')}}">
<span class="text-danger">
#error ('email') {{$message}} #enderror</span><br>
<label>Password :</label>
<input type="password" class="form-control" name="password" placeholder="Mot de passe...">
<span class="text-danger">
#error ('password') {{$message}} #enderror</span><br>
<button type="submit">S'inscrir</button>
</form>
here is the auth controller with the methode create() :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Controllers\hash;
class authController extends Controller
{
public function authpage()
{
return view('login');
}
public function register()
{
return view('register');
}
public function create(Request $request)
{
$request->validate([
'email'=>'required|email|unique:users',
'password'=>'required|min:5|max:12',
]);
$user = new User ;
$user -> name = $request->name;
$user -> email = $request->email;
$user -> password = $request->password;
$query = $user->save();
if($query) {
return back()->with('success',' vous êtes inscris avec succées ! ' );
}else{
return back()->with('fail',' il y as quelque chose qui cloche ! ' );
}
}

You have an syntax error, it is not methode= but method="POST"
<form action="{{ route('auth.create')}}" method="POST">

Related

Having a problem in MethodNotAllowedHttpException

I'm Trying to solve this error but not getting any proper solution for this as i'm beginner in the laravel, Please help with the code..
error--> 1
Thank You
This is my Web.php file
Route::get('/', function() {
return view('welcome');
});
Route::get('user',function(){
return view('user');
});
Route::get('user/register',['uses'=> 'usercontroller#create']);
Route::post('/user',['uses'=> 'usercontroller#store']);
Register.blade.php
<form method="POST" method="/user">
{{ csrf_field() }}
name<input type="text" name="name">
email<input type="email" name="email">
pass<input type="password" name="password">
<input type="submit" value="register">
</form>
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class usercontroller extends Controller
{
public function create()
{
return view('register');
}
public function store(Request $request)
{
User::create($request->all());
return 'Sucess';
return $request->all();
}
}
You have a mistake in your form tag (the second method should be action):
Change
<form method="POST" method="/user">
to
<form method="POST" action="/user">

The page has expired due to inactivity.Please refresh and try again in Login

I have a problem with the login when I try to log in it says the page has expired due to inactivity.I am using middleware to check role based on user login and it seems it's not working. When ever try to login the page has expired message popups.
Route:
Route::get('/', function () {
return view('login');
});
Route::get('/dashboard/{user_id}', ['as' => 'dashboard', function ($user_id) {
return view('theme.index')->with(['id'=>$user_id]);
}]);
Route::post('login', 'AuthController#postSignIn')->name('login');
AuthController:
public function postSignIn(Request $request)
{
if (Auth::attempt(['username' => $request['username'], 'password' => $request['password']])) {
$user=DB::table('users')->where([['username', '=', $request['username']],['status','=','0']])->first();
$user_id=$user->user_id;
return redirect()->route('dashboard',$user_id)->with('message', 'State saved correctly!!!');
} else {
return redirect()->back();
}
}
Middleware:
public function handle($request, Closure $next)
{
if ($request->user() === null) {
// return response("Insufficient permissions", 401);
return response(view('error'),401);
}
$actions = $request->route()->getAction();
$roles = isset($actions['roles']) ? $actions['roles'] : null;
if ($request->user()->hasAnyRole($roles) || !$roles) {
return $next($request);
}
// return response("Insufficient permissions", 401);
return response(view('error'),401);
}
}
Index:
<form class="form-horizontal" action="{{ route('login') }}" method="post">
{{ csrf_token() }}
<div class="form-group m-b-20 row">
<div class="col-12">
<label for="emailaddress">Username</label>
<input class="form-control" type="text" id="username" required="" placeholder="Enter Username">
</div>
</div>
<div class="form-group row m-b-20">
<div class="col-12">
<label for="password">Password</label>
<input class="form-control" type="password" required="" id="password" placeholder="Enter your password">
</div>
</div>
<div class="form-group row text-center m-t-10">
<div class="col-12">
<button class="btn btn-md btn-block btn-primary waves-effect waves-light" type="submit">Login</button>
</div>
</div>
</form>
You can change the session lifetime in Laravel config inside config/session.php by modifying following value
lifetime
also you will need to run
php artisan config:cache
for Laravel to pick new configurations.
I had figured it out i did it from scratch it was the problem of Auth function
The import Auth before that i did run two commands to clear my cache
php artisan cache:clear
php artisan config:cache
and import Auth
Thank you for the help guys appreciate it
add your route in $except array of VerifyCsrfToken.php middleware like this $except = [ "/login" ]; .
open VerifyCsrfToken.php middleware and put in except your url like :
protected $except = [
'http://localhost:8000/login'
];
and can see laravel docs for more information about csrf
https://laravel.com/docs/5.6/csrf#csrf-excluding-uris

can't get access to admin dasheboard

in my laravel project, I specified 2 guards, 1 for users and 1 for admins.
everything was working properly and today I find out that I couldn't log in to the admin page.
I don't know what happened! I did not change anything!!
when I try to log in as an admin, the login page gets reloads again!
btw, for the case of users, everything works fine!
this is the code
Adminlogincontroller
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Admin;
class AdminLoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/admin';
public function __construct()
{
$this->middleware(array('guest:admin','guest:web'))->except('logout');
}
public function login(Request $request)
{
// Validate the form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
// Attempt to log the user in
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
// if successful, then redirect to their intended location
return redirect()->intended(route('dashboard'));
}
// if unsuccessful, then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
public function logout(Request $request)
{
$this->guard('admin')->logout();
$request->session()->invalidate();
return redirect()->route('AdminLogin');
}
public function showLoginForm()
{
return view('Admin.admin-login');
}
protected function guard()
{
return Auth::guard('admin');
}
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
}
}
blade page
<form action="/admin/login" method="post">
{{csrf_field()}}
<div class="input-field col s12">
<i class="material-icons prefix">email
</i>
<input id="icon_prefix" name="email" type="text" class="validate">
<label for="icon_prefix" style="color:#fff;">E-Mail</label>
</div>
<div class="input-field col s12">
<i class="material-icons prefix">lock</i>
<input id="icon_prefix" name="password" type="password"
class="validate">
<label for="icon_prefix" style="color:#fff;">Mot de passe</label>
</div>
<p>
<input type="checkbox" class="filled-in" id="filled-in-box" />
<label for="filled-in-box" >Se rappeler de moi?</label>
Mot de passe oublié
</p>
<div class="row">
<div class="col s12">
<input type="submit" class="btn hoverable purple white-text" style="margin-top:30px;" value="connexion">
</div></div>
</form>
routes:php
Route::post('/admin/login', 'Admin\AdminLoginController#login');
Route::get('/admin/logout', 'Admin\AdminLoginController#logout');
Route::get('/admin', 'Admin\AdminController#show')->name('dashboard')

Laravel 5.1 Trying to post data to controller but geting MethodNotAllowedHttpException error

Im trying to POST data to my controller but I'm getting a
MethodNotAllowedHttpException in RouteCollection.php line 219:
error message, here are my files.
my route file
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController#index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController#index');
Route::post('profile/update', 'ProfileController#updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
& my ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
not sure what the issue is. Thanks for all the help everyone
A couple of issues that we managed to address here:
Over-use of HTTP Verbs
At your view, you have: <form method="POST" but also <input name="_method" type="hidden" value="PATCH"> which may conflict between a POST and a PATCH. Since your routes.php only declares POST, let's remove the patch definition.
Routing Mistake
Still at your view, your action points to action="/profile/update/" while your route is defined as Route::post('profile/update'), notice the extra / at the end in your form. That slash should not be there.
Controllers Request
You have a here: use App\Http\Requests; is probably incorrect because that's a folder within Laravel, not a class. Let's remove that and keep use Illuminate\Http\Request; for now. In the near future, you'll be learning how to create your own Form Requests and you'll probably want a UpdateProfileRequest.php.

I am working on authentication in laravel when i try to login it show error method not allowed http exception

I have followed a tutorial to add authentication to my application. I have a login route which has the method post and the form is also submitting as a post method. But whenever I click on the login button. Laravel throws an error of MethodNotAllowed. I assume that it is getting the method as a get request but the route is a post. Error is in thecompiled.php.
Route
Route::any('signup',['as' => 'signup' , 'uses' => 'Auth\AuthController#getRegister']);
Route::any('loginForm',['as' => 'loginForm' , 'uses' => 'Auth\AuthController#showLoginForm']);
Route::post('login',['as' => 'login' , 'uses' => 'Auth\AuthController#login']);
Route::any('postRegister',['as' => 'postRegister' , 'uses' =>'Auth\AuthController#postRegister']);
View
<form id="login" method="post" action="{{ route('login') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<input type="email" placeholder="User Email" id="email" name="email" class="form-control" value="{{old('email')}}"/>
</div>
<div class="form-group">
<input type="password" placeholder="Your Password" id="password" name="password" class="form-control" {{--value="{{old('email')}}"/--}}>
</div>
<div class="row">
<div class="mj_checkbox">
<input type="checkbox" value="1" id="check2" name="checkbox">
<label for="check2"></label>
</div>
<span> remember me</span>
</div>
</div>
<div class="form-group pull-right">
<span>forget password ?</span>
</div>
</div>
</div>
</div>
<input type="submit">
</div>
</form>
{authentication code is here}
<?php namespace App\Http\Controllers\Auth;
//use \App\Http\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests\LoginRequest;
use App\Http\Models\Company;
use Hash;
class AuthController extends Controller {
/**
* the model instance
public function __construct(Guard $auth, \App\User $user)
{
$this->user = $user;
$this->auth = $auth;
$this->middleware('guest', ['except' => ['getLogout']]);
}
public function getRegister()
{
return view('public-pages.Home.signup');
public function postRegister(Request $request)
{
$company = new Company;
$company-> companyName = $request -> companyName;
$company-> email = $request -> email;
$company-> password = Hash::make($request-> password);
$company-> address = $request -> address;
$company-> employeeName = $request -> employeeName;
$company-> phone_no = $request -> phone_no;
$company-> country = $request -> country;
$company-> city = $request -> city;
$company -> save();
return redirect('loginForm');
}
public function showLoginForm()
{
return view('public-pages.Home.login');
}
public function login(LoginRequest $request)
{
if ($this->auth->attempt($request->only('email', 'password')))
{
return redirect('Private-pages.Company.cmp-home');
} else {
return redirect('/login')->withErrors([
'email' => 'The credentials you entered did not match our records. Try again?',
]);
}
public function getLogout()
{
$this->auth->logout();
return redirect('/');
}
}

Categories