Hello Guys i am new to laravel. When the new user register with authentication i am this error message. How to resolve my problem
The Authentication Failure with this error message.
Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\customer given.
My controller is
public function store(Request $request)
{
$user = new customer;
$user->name=Input::get('name');
$user->email=Input::get('email');
$user->password=Input::get('password');
$user->save();
Auth::login($user);
return redirect::home();
}
and my routes
Route::get('register', 'testing#index');
Route::post('store', 'testing#store');
Route::get('login', 'testing#create');
Route::post('logout', 'testing#destroy');
and my register page is
<form action="store" method="post">
<label for="name">Name</label>
<input type="text" name="name" autocomplete="off">
<br>
<label for="email">Email</label>
<input type="text" name="email" autocomplete="off">
<br>
<label for="password">Password</label>
<input type="text" name="password" autocomplete="off">
<br>
<input type="hidden" name="_token" value="{{csrf_token()}}">
<br>
<input type="submit" name="submit" value="Submit">
</form>
Please help me guys how to register,login and logout with complete authentation.
Thanks you and welcome your suggestions.
Without knowing your actual model of customer I try to give you the best matching answer.
If you are very new to laravel the best appraoch ist to use
php artisan make:auth
To create:
Home View
App Layout
Auth Routes
Auth Controller
Password Reset
This will be done for you, using the default, already existing, App\User model. If you DO NOT want to user the default App\User and you want to use your App\Customer for authentication, then you will need to at least make your model extend Authenticable
use Illuminate\Foundation\Auth\User as Authenticatable;
class Customer extends Authenticatable
{
...
}
Without this you will surely receive the given error. However, you will still need to ensure that there are the required fields on your Customer model - like email, password, remember token etc. If you do not want to use these you will need to make further adaptions within your authentication controller.
As mentioned the best approach for beginners is using the generated auth. You can find more here: https://laravel.com/docs/5.2/authentication
Be careful - php artisan make:auth will create a few views and it will overwrite existing ones with the same name.
e.g.
/resources/views/home.blade.php
/resources/views/layout/app.blade.php
/resources/views/auth/login.blade.php
/resources/views/auth/register.blade.php
and a few more within auth
Looking at your code you could simply try replacing
$user = new customer;
with
$user = new User
and ofcourse pull in the user model
use App\User;
Related
I am trying to handle a basic form with laravel and am running in to an issue where my POST route isn't being detected and is resulting in a route not defined error in the blade template. My goal is to resolve this error and post the form to the controller, then access the various form fields with the $request param.
This is the error: Route [become-a-customer] not defined.
I appreciate any suggestions on how to resolve this.
Form
<form action="{{ route('become-a-customer') }}" method="post" class="col-md-8 offset-md-2">
<div class="form-row">
<div class="form-group col-md-6">
<label for="first_name">First Name</label>
<input name="last_name" type="email" class="form-control" id="first_name" placeholder="First Name">
</div>
...
</div>
<input type="hidden" name="_token " value="{{ Session::token() }}"/>
<button type="submit" class="btn">SUBMIT</button>
</form>
web.php
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer');
BecomeACustomerController . php
class BecomeACustomerFormController extends Controller
{
public function postBecomeACustomer(Request $request)
{
$firstName = $request['first_name'];
$lastName = $request['last_name'];
...
...
return redirect()->back();
}
}
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
use this command
php artisan optimize
In Your blade Template, You have used the Named route for the form action but, it is not specified in the route file (Web.php).
Change your route file like this
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
OR, you have to change the form action like this
action="{{ url('become-a-customer') }}"
Using the named route is the best practice for a Laravel project.
you can also define as following where "as" key is for naming your route
Route::post('/become-a-customer', ['uses' => 'BecomeACustomerFormController#postBecomeACustomer', 'as' => 'become-a-customer']);
Check your Apache or Nginx configurations. Sometimes a redirect from https to http will alter the method from POST to GET.
I'd recommend setting up a temporary endpoint for GET by the same Route and placing a dd() statement in it to test the theory.
route() method uses route name which is undefined. You can define it via name() method on route as below
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
for more see doucmentation
For me url('routeName') worked instead of route('routeName')
I am new to Laravel, I am trying to have a simple example, but I am getting a 419 error, I dont know why it shows up but I will expalin what I did,
I created a simple Controller and I called it FormController with the command line :
php artisan make:controller --resource FormController
In my web.php I added this :
Route::resource('form','FormController');
my view has a simple form in it :
<form action="/form" method="POST" >
<input type="text" name="cih">
<input type="submit">
</form>
I open my view with the create method :
public function create()
{
return view('contact');
}
I want that when I submit my form I get my data, so I use my 'store' method :
public function store(Request $request)
{
return $request->all();
}
But instead of getting it, I get 419 message, and my session has expired etc ..
I followed a course and that what the teacher was doing I believe nothing more, so I would appreciate any help, I need it.
Thank you
You need to include the CSRF token while submitting a form since the 'VerifyCsrfToken' middleware is enabled by default for the web routes in App/Http/Kernal.php.
<form action="/form" method="POST" >
#csrf
<input type="text" name="cih">
<input type="submit">
</form>
And, Welcome to Laravel!
I'm working in a web project using Laravel 5.2
My use case is very simple:
Before rendering any page, verify if the user is authenticated. If not, provide a login form with custom authentication (not the Eloquent's default stuff)
After reading about this scenario I have:
// routes.php
Route::get('/', 'HomeController#index');
Then if I want all my pages secured, I require the middleware auth in controller's constructor. All my controllers should follow the same pattern if I want to request a logged in user before serving any page.
// app/Http/Controllers/HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('home');
}
}
So far, so good. If I visit my app at '/' I'm redirected to a /login page.
I created the login page:
// views/auth/login.blade.php
#extends('layouts.app')
#section('content')
<form class="form-signin" method="post" action="{{ url ('/login') }}">
{!! csrf_field() !!}
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputUsername" class="sr-only">Username</label>
<input type="text" id="inputUsername" class="form-control" placeholder="Username" name="username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" name="password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
#endsection
Please notice the action of the form that redirects to /login.
Then I update the routes.php by providing the new following routes:
// Authentication Routes...
Route::get('login', 'Auth\AuthController#showLoginForm');
Route::post('login', 'Auth\AuthController#login');
Route::get('logout', 'Auth\AuthController#logout');
Route::get('/', 'HomeController#index');
With these new routes I'm catching login/logout scenarios and assigning AuthController's methods to handle them.
On the already implemented AuthController, I guess I need to define these methods.
I was not able to make this to work or maybe I'm doing this custom authentication in a wrong way.
I have:
// app/Http/Auth/AuthController.php
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
// ... lot of default stuff ..
protected function login($data)
{
//
// do custom login authentication with $data
// ie: validate thru a web service or something
//
return redirect()->intended('/');
}
}
Any suggestions on how to implement this?
Change your login function to
protected function login(Request $data)
{
//
// do custom login authentication with $data
// ie: validate thru a web service or something
//
return redirect()->intended('/');
}
This overrides the function from Illuminate\Foundation\Auth\AuthenticatesUsers that originally used by Laravel Auth
Thanks to everyone who responded to this question. I ended rolling out a custom integration where users are duplicated on the Eloquent's User model and my external service.
Unfortunately the lack of documentation around Laravel 5.2 and custom authentication methods makes this as a patch solution until something more stable comes out.
Thanks and happy coding!
My getCredentials() method is using login, password and client_id
I moved it into my authController, modified and it works like a charm with one exception. When login fails my login form is only populated with previously used login (password is obviously empty) but client_id is missing.
I KNOW i can add it in postLogin() but I'd like to keep "vendor" part untouched.
What is the best practice here? Should I move also the whole postLogin() to my authController as well and change it there?
One way to go is to use $remember.
Regarding to Laravel documentation, you need to add $remember like:
if (Auth::attempt(['email' => $email, 'password' => $password], $remember))
{
// The user is being remembered...
}
And in your blade add a check box like
<div>
<input id="RememberMe" name="rememberme" type="checkbox"/>
<label for="RememberMe">Remember Me</label>
</div>
I've looked around to see if I can find this specific problem, but have been unsuccessful so far.
The problem is pretty simple. I'm using an artisan-generated UsersController to handle RESTful communication on the /users directory. GET works just fine, but whenever I POST a form to /users, instead of executing the store() method properly like it should, it throws the MethodNotAllowedHttpException error. When I made a new handler postNew(), and POST to users/new, it works just fine. I could just use that, but I would really like to figure out what the problem is so I can use the standard RESTful method.
Additionally, I'm not using Laravel's form generator because I intend to cache every page for speed, and don't want to generate a unique id for every form I send. I saw on another post that this might be causing problems, but couldn't figure out a way to integrate it into a solution.
<form id="signup-form" method="POST" action="users">
<label>First Name:</label>
<input type="text" name="firstName">
<label>Last Name:</label>
<input type="text" name="lastName">
<input type="submit" value="Sign Up Free!">
</form>
That's my code.
controller:make will only create a resource controller, it will not define its route(s).
Since you mention that you created a new controller method postNew(), and it works when you send a POST request to users/new, I will assume that you have created a RESTful controller route and not a resource route.
RESTful controller route (not compatible with controller:make):
Route::controller('users', 'UsersController');
Resource controller route (compatible with controller:make):
Route::resource('users', 'UsersController');
The differences between these two controller types are outlined on the Controllers docs page: RESTfull vs. Resource.