I have a log in and register form that I'd like to set up on the same page.
The log in form works just fine, with dummy data already inputted in to the database that i'm using.
The issue I'm having is that I'm getting a method calling error (assumedly because I have the same post function call to two different functions.
Currently in my routes.php file I have
// route to process the form
Route::post('/', array('uses' => 'HomeController#doLogin'));
Route::post('/', array('uses' => 'HomeController#doRegister'));
And my controller file looks like this (sorry it's a little long, I thought it'd be better to provide everything instead of assuming someone can understand my question from just my explanation alone)
public function doRegister() {
$v = User::validate(Input::all());
if ( $v->passes() ) {
User::create(array(
'name'=> Input::get('name'),
'email'=> Input::get('email'),
'password'=> Hash::make(Input::get('password')),
));
return 'Thanks for registering!';
} else {
return Redirect::to('/')->withErrors($v->getMessages());
}
}
public function doLogin()
{
// validate the info, create rules for the inputs
$rules = array(
'email' => 'required|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('/')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
// create our user data for the authentication
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
// validation successful!
// redirect them to the secure section or whatever
// return Redirect::to('secure');
// for now we'll just echo success (even though echoing in a controller is bad)
echo 'SUCCESS!';
} else {
// validation not successful, send back to form
return Redirect::to('/');
}
}
}
As far as I'm aware this is because I'm not setting which function to use correctly for my registration form.
Hopefully I've done an ok job at explaining my issue, any solution please? (rather new to laravel)
One form would post to login and the other to register
Route::post('login', array('uses' => 'HomeController#doLogin'));
Route::post('register', array('uses' => 'HomeController#doRegister'));
And then you would open the form like:
{{ Form::open(array('url' => 'login')) }}
and
{{ Form::open(array('url' => 'register')) }}
Edit:
And the forms would just be placed inside your home view for example, and then you would just redirect from the login and register methods, and not show a view.
Related
I've a problem with Laravel 5.1 Redirect With Input. I can't seem to get old values from submitted input.
I've been research for any post in stack overflow and still stuck in this problem.
This is my request controller
public function create() {
$this->data["pageDescription"] = "Add new page.";
$this->data["pageSubtitle"] = $this->data["pageTitle"] . " Add New";
$this->data["formAction"] = adminRoute("page");
return view("admin.page.form")->with("data", $this->data)->withInput(Input::old());
}
and this is my submitted controller
public function store(Request $request) {
Input::flash();
$validator = Validator::make($request->all(), [
"title" => "required|max:100",
"url_title" => "required|max:100",
"meta_description" => "required|max:50",
"content_title" => "required|max:100",
"content_description_id" => "required",
"status" => "required|in:pending,published",
"add_more_content" => "required|in:true,false"
]);
if($validator->fails()) {
return redirect()->back()
->with("data", $this->data)
->withErrors($validator)
->withInput();
} else {
return redirect()->back()->withInput();
}
}
It's kinda weird because when I dump Session::all() before I redirect()->back() the _old_input is exists
But when I dump Session::all() on my function create() the _old_input is empty.
Even when I skip the validation, and use
redirect()->back()->withInput();
I still got the same problem, the old values input empty
This is my form view
Anybody know what I am doing it wrong?
Thank youu
Did you get validation error? I'm not 100% sure, but I think redirect()->back() calls to the GET method of your form.
to make it a bit short. I just made a registration form fully working with a controller, the routes and the view. Now I know it's common sense to use a Model for it and in the controller only call the method in the model. So i thought okay lets fix that. Now when I register an account I get a blank page. I bet the redirect is going wrong but I can't fix it maybe you can?
RegisterController.php
public function doRegister(){
$user = new User();
$user->doRegister();
}
User.php (model)
public function doRegister()
{
// process the form here
// create the validation rules ------------------------
$rules = array(
'username' => 'required|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|min:5',
'serial_key' => 'required|exists:serial_keys,serial_key|unique:users'
);
// create custom validation messages ------------------
$messages = array(
'required' => 'The :attribute is important.',
'email' => 'The :attribute is not a legit e-mail.',
'unique' => 'The :attribute is already taken!'
);
// do the validation ----------------------------------
// validate against the inputs from our form
$validator = Validator::make(Input::all(), $rules);
// check if the validator failed -----------------------
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('/register')
->withErrors($validator)
->withInput(Input::except('password', 'password_confirm'));
} else {
// validation successful ---------------------------
// our duck has passed all tests!
// let him enter the database
// create the data for our duck
$duck = new User;
$duck->username = Input::get('username');
$duck->email = Input::get('email');
$duck->password = Hash::make(Input::get('password'));
$duck->serial_key = Input::get('serial_key');
// save our user
$duck->save();
// redirect with username ----------------------------------------
return Redirect::to('/registered')->withInput(Input::old('username'));
}
}
you need to make $user->doRegister(); a return statement
in your RegisterController you have to do
public function doRegister(){
$user = new User();
return $user->doRegister();
}
try this
return Redirect::to('/registered')
->with('bill_no', Input::get('username'));
in the '/registered' controller,..
use this
$username = Session::get("username");
above worked for me,...
Working with Laravel 5 I'm facing an issue to where it routes to auth/login by default. When you login, it redirects to login causing an error. When I'm able to actually use http://localhost/login it actually routes to home like it should. Anything new that would be causing it behave like this?
HomeController shown below:
<?php namespace app\Http\Controllers;
class HomeController extends Controller {
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard to the user.
*
* #return Response
*/
public function index()
{
return view('home');
}
public function showLogin()
{
// show the form
return view('login');
}
public function doLogin()
{
// validate the info, create rules for the inputs
$rules = array(
'email' => 'required|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
// create our user data for the authentication
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
// validation successful!
// redirect them to the secure section or whatever
// return Redirect::to('secure');
// for now we'll just echo success (even though echoing in a controller is bad)
echo 'SUCCESS!';
} else {
// validation not successful, send back to form
return Redirect::to('login');
}
}
}
public function doLogout()
{
Auth::logout(); // log the user out of our application
return Redirect::to('login'); // redirect the user to the login screen
}
}
I figured it out to be that constructor.
public function __construct()
{
$this->middleware('auth');
}
I removed that and changed the view to 'auth/login' and it works like a charm.
So I have this code:
public function postLogin() {
// validate the info, create rules for the inputs
$rules = array(
'username' => 'required', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::route('login')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
$remember = (Input::has('remember')) ? true : false;
// create our user data for the authentication
$userdata = (array(
'username' => Input::get('username'),
'password' => Input::get('password')
), $remember);
// attempt to do the login
if (Auth::attempt($userdata)) {
// validation successful!
// redirect them to the secure section or whatever
// return Redirect::to('secure');
// for now we'll just echo success (even though echoing in a controller is bad)
echo 'SUCCESS!';
} else {
// validation not successful, send back to form
return Redirect::route('login')
->with('global', 'Incorrect username or password. Please try again.');
}
}
}
When it runs I get: syntax error, unexpected ','. Basically it isn't expecting the $remember to be passed there. Where is it meant to be? I've tried putting it here: Auth::attempt($userdate), $remember) { } but that didn't work either. It had the same error. Not sure what's going on. Any help would be appreciated.
You can use Auth::viaRemember() in your Authcontroller, to check if a user was already logged:
if (Auth::check() || Auth::viaRemember()) {...
And change your login-check as follows:
//Assuming, the remember-input is a checkbox and its value is 'on'
if (Auth::attempt($userData, (Input::get('remember') == 'on') ? true : false)) {...
Greetings,
I am setting up a pretty standard registration form with password field.
The problem is, after a failed submission (due to empty field, incorrect format etc), the controller reloads the registration page, but with the password field containing the hashed value of the previously entered password. How do I make it empty after each failed submission?
View:
echo $form->password('Vendor.password', array('class' => 'text-input'));
Controller:
Security::setHash('sha1');
$this->Auth->sessionKey = 'Member';
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
Help is very much appreciated, thanks!
You may run into another problem down the road with cakePHP password validation.
The problem is that cake hashes passwords first, then does validation, which can cause the input to fail even if it is valid according to your rules. This is why the password is returned to the input field hashed instead of normal.
to fix this, instead of using the special field name 'password', use a different name like 'tmp_pass'. This way, cakePHP Auth won't automatically hash the field.
Here's a sample form
echo $form->create('Vendor', array('action' => 'register'));
echo $form->input('email');
echo $form->input( 'tmp_pass', array( 'label' => 'Password','type'=>'password' ));
echo $form->end('Register');
In your Vendor model, don't assign validation rules to 'password' instead assign these rules to 'tmp_pass', for example
var $validate = array('email' => 'email', 'password' => ... password rules... );
becomes
var $validate = array('email' => 'email', 'tmp_pass' => ... password rules... );
Finally, in your Vendor model, implement beforeSave().
First, see if the data validates ('tmp_pass' will be validated against your rules).
If successful, manually hash tmp_pass and put it in $this->data['Vendor']['password'] then return true. If unsuccessful, return false.
function beforeSave() {
if($this->validates()){
$this->data['Vendor']['password'] = sha1(Configure::read('Security.salt') . $this->data['User']['tmp_pass']);
return true;
}
else
return false;
}
this?
password('Vendor.password', array('class' => 'text-input','value'=>''))
In your controller:
function beforeRender() {
parent::beforeRender();
$this->data['Vendor']['password'] = '';
}