I am using the code below to create a user in Laravel. When I log in with this user, it does not appear to be "authenticated" (even though the ID, password, and tenanted information has been entered correctly). Authenticated users go to a "home" page. This just goes back to the login page.
I noticed that when the user was created the "remember_token" of the "user" table was not filled out.
How can I fill out this field? How can I fix this so that users created using PHP are authenticated?
TIA
$user = User::create([
'name' => $contractor->getFirstName() . ' ' . $contractor->getLastName(),
'email' => $contractor->getAsgnLogonID(),
'password' => bcrypt($contractor->getAsgnPassword()),
'tenantid' => $TENANTREFNO,
'wavemakerid'=> $contractor->getKeyID(),
]);
Here is the web.php file:
Route::get('/', function () {
return redirect()->route('login.showform');
});
Route::post('/login/custom', [
'uses' => 'Auth\LoginController#login',
'as' => 'login.custom'
]);
Route::get('/login/showform', [
'uses' => 'Auth\LoginController#showLoginForm',
'as' => 'login.showform'
]);
Route::get('/home', 'HomeController#index');
Route::get('/logout', 'Auth\LoginController#logout');
Route::get('/dashboard', 'DashboardController#index');
Auth::routes();
I made the changes below and was able to have the user logged in:
if ( $password == $local_password )
{
Auth::login($user);
return redirect('/home');
}
Related
I tried adding a position to my user and setting it on users_positions table.
I created a new query alongside with the auth user create. When re-routing it to the root URL, it always comes back as
InvalidArgumentException
Route [/] not defined.
Controller
protected function create(array $data)
{
$id = User::create([
'firstname' => $data['firstname'],
'middlename' => $data['middlename'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'username' => $data['username'],
'password' => Hash::make($data['password']),
]);
DB::table('users_positions')->insert([
[
'user_id' => $id->id,
'position_desc' => $data['position'],
'primary' => "1",
]
]);
return redirect()->route('/')
->with('success', 'Successfully added a user account');
}
Route
Auth::routes();
Route::get('/home', 'HomeController#index')->name('name');
Route::get('/', function () {
return view('incident-reports.index');
})->middleware('auth');
The query is successful but the route still not defined.
When I try manually going to the root URL http://127.0.0.1:8000. It will work.
My plan is after adding user and the user_position it will come back at root URL and prompting an alert if it is success or error.
if you want to redirect with the path, send it as parameter to redirect() method
return redirect('/');
the route() method is to redirect to a route by Alias
route
Route::get('/', function () {
return view('incident-reports.index');
})->middleware('auth')->alias('root');
redirect
return redirect()->route('root');
Change this line:
return redirect()->route('/')
->with('success', 'Successfully added a user account');
to this
return redirect()->to('/')
->with('success', 'Successfully added a user account');
route() expects a route name as argument. '/' is a url
I have middleware that redirects the user if it it not logged in. The user is not prevented from filling in the form, only when he submits the data my middleware comes in to check if he is authenticated. It seems like it is not passing throught the middleware at all when the submit button is clicked.
My route
Route::group(['middleware' => 'allow.access'], function(){
Route::post('houses', [ //I wonder if I defined this
'as' => 'houses.store', //route correctly because
'uses' => 'HousesController#store' //it seems Laravel is ignoring it
]);
Route::get('houses/{id}/edit', [
'as' => 'houses.edit',
'uses' => 'HousesController#edit'
]);
});
My middleware works if I use this route inside the group:
Route::get('houses/create/{zip}', [
'as' => 'houses.create',
'uses' => 'HousesController#create'
]);
my middleware
public function handle($request, Closure $next)
{
if(!$request->session()->has('loginInfo'))
{
return redirect()->route('register.login');
}
return $next($request);
}
Here is the code.If sign in it goes to /dashboard route. but after I go to other route user session is not persisting(by dd I found this).thanks in advance if you solve, I spent hours on this.
Route::group(['middleware' => 'web'],function(){
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::get('/dashboard' , [
'uses' => 'UserController#GetDashboard',
'as' => 'dashboard'
]);
Route::post('/signin' , [
'uses' => 'UserController#postSignin',
'as' => 'signin'
]);
});
in my login controller
public function postSignin(Request $request)
{
if(Auth::attempt(['email' => $request['email'],'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
$request is an object, not an array. Try using $request->get('email').
I have a controller UserController.php which contains the following methods:
getIndex(), getAll(), getCreate(), postStore(), getShow(), getEdit(), putUpdate(), getDestroy(), getLogin(), getDashboard(), and getLogout().
I have included the following codes into the routes.php
Route::get('/', function()
{
return View::make('hello');
});
Route::get('users/all/', [ 'as' => 'users.index', 'uses' => 'UserController#getAll']);
Route::get('users/create/', [ 'as' => 'users.getCreate', 'uses' => 'UserController#getCreate']);
Route::get('users/{all}/edit', 'UserController#getEdit');
Route::put('users/update/{id}', [ 'as' => 'users.putUpdate', 'uses' => 'UserController#putUpdate']);
Route::controller('users', 'UserController');
I can access the pages like
http://localhost/testlaravell/users/
or
http://localhost/testlaravell/users/add
etc.
Now, I want that only logged in users can access the pages, other wise s/he will be redirect to the login page http://localhost/testlaravell/login
The methods for login under UserController.php as follows:
public function postSignin() {
$rules = array(
'username' => 'required', // make sure the username is an actual username
'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('users/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(
'username' => Input::get('username'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
return Redirect::to('users/dashboard')->with('message', 'Welcome User');
} else {
// validation not successful, send back to form
return Redirect::to('users/login')->with('message', 'Sorry!! Username/ Password seems wrong.');
}
}
}
public function getLogin() {
return View::make('users.login');
}
You'll want to make use of the Auth Filter in Laravel 4
You can wrap all your routes in a group and specify that filter, in your case the code would be something like this.
Route::group(array('before' => 'auth'), function()
{
Route::get('users/all/', [ 'as' => 'users.index', 'uses' => 'UserController#getAll']);
Route::get('users/create/', [ 'as' => 'users.getCreate', 'uses' => 'UserController#getCreate']);
Route::get('users/{all}/edit', 'UserController#getEdit');
Route::put('users/update/{id}', [ 'as' => 'users.putUpdate', 'uses' => 'UserController#putUpdate']);
Route::controller('users', 'UserController');
});
You can checkout the documentation on Route Groups here
I have a registration form and it was working until it just didn't. I am sure I did something with the code because I am new to laravel 4 so I am unable to identify my wrong doing. Now I am getting
This webpage has a redirect loop.
This is the route file:
Route::get('/','MainController#index');
Route::get('/login', 'MembersController#login');
Route::get('/signup', 'MembersController#signup');
/*handled by controller to register the user that signed up*/
Route::get('/register', 'MembersController#register');
/* Handle authenticating a user when loggin in*/
Route::post('register', array(
'uses' => 'MembersController#register',
'as' => 'members.register'
));
and this is the form opening:
#section('content')
{{ Form::open(array('route' => 'members.register')) }}
......
{{ Form::close() }}
#stop
and this is the validation where if there is an error, it used to redirect to the sign-up page again and show them (and it did until it broke)
public function register()
{
$rules = array(
# place-holder for validation rules
'firstname' => 'Required|Min:3|Max:40|Alpha',
'lastname' => 'Required|Min:3|Max:40|Alpha',
'email' => 'Required|Between:3,64|Email|Unique:users',
'country' => 'Required',
'password' =>'Required|AlphaNum|Between:7,15|Confirmed',
'password_confirmation'=>'Required|AlphaNum|Between:7,15'
);
/*Create new user if no user with entered email exists. Use validator to ensure all fields are completed*/
$user = new User;
$validator = $this->validate(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::to('register')->withErrors($validator);
}else{
echo "Success";
}
}
Thanks for the help :)
Change the following line
return Redirect::to('register')->withErrors($validator);
with this
return Redirect::back()->withInput()->withErrors($validator);
You are calling the route register infinite times.
Remove this route as well. You only need post route.
/*handled by controller to register the user that signed up*/
Route::get('/register', 'MembersController#register');