I'm writing a simple authentication form with laravel.
After trying two different tutorials, it's still returning false.
Register is working fine, but my login isn't.
Here is the LoginController:
public function getLogin() {
$this->layout->content = View::make('user.login');
}
public function postSignin() {
if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password'))))
{
return Redirect::to('user/dashboard')->with('message', 'You are now logged in!');
}
else
{
return Redirect::to('user/login')
->with('message', 'Your username/password combination was incorrect')
->withInput();
}
}
public function getDashboard() {
$this->layout->content = View::make('user.dashboard');
}
The route:
Route::controller('user', 'UserController');
and the view:
{{ Form::open(array('url'=>'user/signin', 'class'=>'form-signin')) }}
<h2 class="form-signin-heading">Please Login</h2>
{{ Form::text('username', null, array('class'=>'input-block-level', 'placeholder'=>'Username')) }}
{{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Password')) }}
{{ Form::submit('Login', array('class'=>'btn btn-large btn-primary btn-block'))}}
{{ Form::close() }}
In your form you have an input for username but in your controller you have Input::get('email').
You have to edit your form like that...
{{ Form::text('email', null, array('class'=>'input-block-level', 'placeholder'=>'Username')) }}
...or your controller like that
Auth::attempt(array('email'=>Input::get('username'), 'password'=>Input::get('password')))
I found out that I hadn't set a key with
php artisan key:generate
and my password field was only 32 characters long instead of 60.
Finally got it to work!
Related
I try to delete with confirm like, using a controller method delete:
function delete($id) {
$list = Todolist::find($id);
return view('lists.delete')->with('list',$list);
}
and corresponding delete.blade.php:
{!! Form::open(array('route' => array('lists.destroy', $list->id), 'method' => 'delete', 'class' => 'form')) !!}
<button type="submit" class="btn btn-sucess">Delete</button>
<button type="submit" onClick="history.back()">Cancel</button>
{!! Form::close() !!}
then also a controller destroy-method
function destroy($id) {
$list = new Todolist;
//$list->delete($id);
echo
return view('lists.confirmdelete')\Redirect::route('lists.index')
->with('message', 'Task deleted!');
////how to aply 5 second sleep for showing message 'Task deleted!'???
}
and confirmdelete.blade.php
<h1>{{ $list->name }}</h1>
<p>{{ $list->description }}</p>
<p><b>{{ $message }}</b></p>
How to do, that it display "Task deleted!" messsage e.g 5 seconds and then two steps back to an index action?
In your destroy method you have to do a little tweek & also have to use a little bit of js
Change destroy method this to
function destroy($id) {
$list = new Todolist;
$data = [
'name' => $list->name,
'description' => $list->description,
];
$list->delete($id);
$data['message'] = 'Task deleted!';
$data['redirectRoute'] = route('lists.index');
return view('lists.confirmdelete', $data);
}
and in confirmdelete.blade.php
<h1>{{ $name }}</h1>
<p>{{ $description }}</p>
<p><b>{{ $message }}</b></p>
<script>setTimeout(function(){ window.location.href = '{{ $redirectRoute }}' }, 5000);</script>
Easier with:
<button class="btn btn-link"
onclick="return confirm('Are you sure you want to delete the record {{ $user->id }} ?')">
DELETE
</button>
In this case, you open a popup with a confirmation if you want to delete a record before clicking. Obiously you need to put
$user = User::find($id);
in the delete method if you want an id in the message.
session variable best for given problem
try in controller method
function destroy($id) {
//AFTER SUCCESS YOUR LOGIC
Session::flash('message', 'Status Changed');
Session::flash('alert-class', 'alert-success');
return redirect('/index');
}
in blade file you can check like this demo
#if(Session::has('message'))
<div class="alert {{ Session::get('alert-class') }} fade-in" id="alert">
×
{{ Session::get('message') }}
</div>
#endif
Session flash is best to use here it will poof once it print. and you can use JS for after 5 second fadeout.
<script>
$("#alert").fadeTo(2000, 500).slideUp(500, function(){
$("#alert").slideUp(500);
});
</script>
If i try to login i just get this url:
http://xx.xx.xx.xx/game/public/?_token=4kXP1sYlLIizUUMyMWluK6jSEOacDikXafUSVrIF&username=dieter&password=azerty
Im probably doing something stupid.. Here is my code:
controller:
public function postSignin() {
if (Auth::attempt(array('username'=>Input::get('username'), 'password'=>Input::get('password')))) {
return Redirect::to('users/dashboard')
->with('message', 'You are logged in');
} else {
return Redirect::to('index')
->with('message', 'Your username/password combination was incorrent.')
->withInput();
}
}
Login form:
{{Form::open(array('url'=>'users/signin')) }}
{{ Form::text('username', null, array('class' => 'input-block-level', 'placeholder' => 'Username')) }}
{{ Form::password('password', null, array('class' => 'input-block-level', 'placeholder' => 'Password')) }}
{{ Form::submit('Login', array('class' => 'btn btn-default')) }}
{{ Form::close() }}
Routes:
<?php
Route::controller('users', 'UsersController');
Route::controller('/', 'HomeController');
It's like it is ignoring the 'url' in the login form. If i change it to something else it just responds the same way.
I've bene googling all day for this, so please help. ^^
Thanks
You've got a form without action attribute, and your submit button sends you to the same page. Try to change the Form declaration as this:
{{ Form::open(array('route' => 'users/signin') ) }}
I hope this works fine.
I am attempting to create a single page like application with Laravel 4. When the user arrives at the site, they should be prompted to log in. Once the user logs in, the view (not the URL) will switch and the user will be able to see information as if they are authenticated.
My HTML (if authroized should show "Auth" in h1, if not, it shows login form)
<div class="container">
#if(Auth::check())
<h1>Auth</h1>
#else
{{ Form::open(array('url'=>'login', 'method'=>'post')) }}
<div class="row">
<div class="col-xs-12">
<div class="form-group">
{{ Form::label('email', 'Email Address') }}
{{ Form::text('email', Input::old('email'), array('class'=>'form-control', 'placeholder'=>'example#test.com')) }}
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array('class'=>'form-control')) }}
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
{{ Form::submit('Log In', array('class'=>'btn btn-primary pull-right')) }}
</div>
</div>
{{ Form::close() }}
#endif
</div>
Controller
class SiteController extends BaseController {
public function getIndex()
{
return View::make('index');
}
public function postLogin() {
$email = Input::get('email');
$password = Input::get('password');
if (Auth::attempt(array('email'=>$email, 'password'=>$password)))
{
return Redirect::route('index');
}
}
}
My user model is the default that ships with Laravel 4. As of now, I am passing the Auth::attempt and getting the return Redirect::route('index');, but the #if(Auth::check()) doesn't seem to be firing. Instead it continues to show me the log in form. Am I doing something wrong here?
I don't see anything wrong here, but you need to be sure what's happening, it looks like your authenticated session is not sticking, but to be sure you could:
<?php
class SiteController extends BaseController {
public function getIndex()
{
Log::info('index - authed: '. Auth::check() ? 'yes' : 'no');
return View::make('index');
}
public function postLogin() {
$email = Input::get('email');
$password = Input::get('password');
if (Auth::attempt(array('email'=>$email, 'password'=>$password)))
{
Log::info('postLogin - attempt successful');
return Redirect::route('index');
}
Log::info('postLogin - error on attempt');
}
}
And then check your logs:
php artisan tail
Earlier today I had the exact same problem with Auth::attempt always retuning false. I realized that Auth checks for a hashed password, so by doing so I was able to get it to return true, but now it always does. Even if I type asdadfasdfaf in my form, the if statement loads the page. Any suggestions?
Controller:
class userController extends \BaseController
{
public function login()
{
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($user))
{
return Redirect::route('home');
}
else
{
return View::make('login');
}
}
}
Form
{{ Form::open(array('url' => 'home' )) }}
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
The Routes file:
Route::post('home', 'userController#login');
No matter what I enter it always directs me to my "home" page?
The action url should be login
{{ Form::open(array('url' => 'login' )) }}
^^^^ as you used Auth::attempt to this URL
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
I'm facing a weird issue while developing a very basic application using Laravel 4. I have created the functionality following the documentation of laravel 4's user authenticating docs where users can sign up to ask different questions and only the registered users can access to the secret/admin page. Everything works well so far except that I'm getting this weird issue where even after logging out users can still access to the precious page or in other word the admin page. I'm not sure it has something to do with Laravel but still I couldn't figure out what's the issue and how to prevent or force the browser to reload or something so that they can't see the admin page even if they click on the back arrow in browser.
Although it may not be relavent but I'm still attaching the codes I have. In routes.php I have this
<?php
Route::get('/', array('as'=>'home', 'uses'=>'QuestionController#getindex'));
Route::get('register', array('as'=>'register', 'uses'=>'UserController#getnew'));
Route::get('login', array('as'=>'login', 'uses'=>'UserController#getlogin'));
Route::get('logout', array('as'=>'logout', 'uses'=>'UserController#getlogout'));
Route::post('register', array('before'=>'csrf', 'uses'=>'UserController#postcreate'));
Route::post('login', array('before'=>'csrf', 'uses'=>'UserController#postlogin'));
And in userController I have this
<?php
class UserController extends BaseController {
public function getNew() {
return View::make('users.new')
->with('title', 'Snappy Q&A - Register');
}
public function postCreate() {
$validator = Member::validate(Input::all());
if ( $validator->passes() ) {
$user = User::create( array (
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'))
));
Auth::login($user);
return Redirect::route('home')->with('message', 'Thanks for registering!');
} else {
return Redirect::route('register')->withErrors($validator)->withInput();
}
}
public function getLogin() {
return View::make('users.login')
->with('title', 'Snappy Q&A - Login');
}
public function postLogin() {
$user_creds = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if( Auth::attempt($user_creds) ) {
return Redirect::route('home')
->with('message', 'Yep, you are now logged in');
} else {
return Redirect::route('login')
->with('message', 'Shit man! The creds are not authorised!')
->withInput();
}
}
public function getLogout() {
if( Auth::check() ) {
Auth::logout();
return Redirect::route('login')
->with('message', 'You are now logged out!');
} else {
return Redirect::route('home');
}
}
}
This new.blade.php is responsible for creating a new user
#extends('master.master')
#section('content')
#if( $errors->has() )
<p>The following erros has occured: </p>
<ul class="form-errors">
{{ $errors->first('username', '<li>:message</li>') }}
{{ $errors->first('password', '<li>:message</li>') }}
{{ $errors->first('password_confirmation', '<li>:message</li>') }}
</ul>
#endif
{{ Form::open( array('route'=>'register', 'method'=>'post')) }}
{{ Form::token() }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username')) }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{ Form::label('password_confirmation', 'Confirm Password') }}
{{ Form::password('password_confirmation') }}
{{ Form::submit('Register', array('class'=>'btn btn-success')) }}
{{ Form::close() }}
#stop
This one is for logging in a user:
#extends('master.master')
#section('content')
{{ Form::open( array('route'=>'login', 'method'=>'post') ) }}
{{ Form::token() }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username')) }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{ Form::submit('Login', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
#stop
TO make it clear, I don't have anything special yet for the admin page except the condition in the navigation with the method Auth::check(); to make sure that only logged in user can see the logout navigation. I will create the functionality later after I'm done with this problem. The admin page view will be in a folder called questions. I hope this makes sense now.
How do I take care of that? Please ask if you still need any other instance of my code. And I believe many of the newbies in Laravel world face more or less the same issue while developing functionality like that. I'm hoping this is a good question and will help others as well as me.
Try to use filters in routes
Route::get('/', array('before' => 'auth', function()
{
}));
More:
http://laravel.com/docs/routing#route-filters