Problems with authentication with Laravel 5 - mysql

I am trying to login a user with laravel 5.
Here is my controller
public function postLogin(LoginRequest $request){
$remember = ($request->has('remember'))? true : false;
$auth = Auth::attempt([
'email'=>$request->email,
'password'=>$request->password,
'active'=>true
],$remember);
if($auth){
return redirect()->intended('/home');
}
else{
return redirect()->route('login')->with('fail','user not identified');
}
}
When i enter wrong credentials, everything works fine, but when i enter the right one, i got this error message:
ErrorException in EloquentUserProvider.php line 110:
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Models\User given, called in C:\xampp\htdocs\Projects\Pedagogia\Admin.pedagogia\vendor\laravel\framework\src\Illuminate\Auth\Guard.php on line 390 and defined
I don't see where i did wrong

Argument 1 passed
to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be
an instance of Illuminate\Contracts\Auth\Authenticatable, instance of
App\Models\User given.
The validateCredentials() method of the Illuminate\Auth\EloquentUserProvider class expects an instance of Illuminate\Contracts\Auth\Authenticable, but you are passing it an instance of App\Models\User. To put it simply, your user model needs to implement the Illuminate\Contracts\Auth\Authenticable interface to work with Laravels authentication scaffolding.
Your App\Models\User model should look like this:
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticable as AuthenticableTrait;
class User extends \Eloquent implements Authenticatable
{
}

#Gaetan you are getting this not found error, because you are using the
use Illuminate\Contracts\Auth\Authenticable instead using use Illuminate\Contracts\Auth\Authenticatable;
you user model should be like that:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class User extends Model implements Authenticatable
{
// your code here
}
change Authenticable with Authenticatable.

Related

1 argument required when initalizing another class in my class

I want to acces a function from another class. A solution would be to initalize the second class in the class I want to access. Like this:
class Calendarsub extends State<Calendar> with SingleTickerProviderStateMixin{
final TableCalendar tableCalendar;
Calendarsub(this.tableCalendar);
When I do this I can acces the functions but the app is not running because the Stateful Widget says: "1 required Argument expected but 0 found."
class Calendar extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return Calendarsub(); // In this bracket must be the argument
// But I don't know which one
}
}
You constructor needs 1 argument - TableCalendar
So, you have to initialize it with this value:
TableCalendar tableCalendar = TableCalendar(); //or somethimg like that
Calendarsub calendar = Calendarsub(tableCalendar);
or make this parameter optional:
class Calendarsub extends State<Calendar> with SingleTickerProviderStateMixin{
TableCalendar tableCalendar;
Calendarsub({this.tableCalendar});
in second case creating will be like:
Calendarsub calendar = Calendarsub(tableCalendar: TableCalendar());
It sounds like you have two classes using the same name
Calendar, a subclass of StatefulWidget, which takes a parameter
Calendar, a State, which doesn't take any parameter
This confuses the compiler. You need to rename so that there's no name conflict anymore.

Laravel 5.2 use custom Controller Trait

I want to use my custom Trait stored in app directory into my controller. However I always get this message:
Trait 'app\MessageTrait' not found
My Controller:
namespace app\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use app\Http\Requests;
use app\User;
use app\MessageTrait;
class login extends Controller{
use MessageTrait;
public function index(Request $request){
return back();
}
}
My MessageTrait is contained within MessageTrait.php, located in app directory. Code looks like this:
My Trait:
trait MessageTrait{
public function success(){
return 'success';
}
public function error($message){
return 'error';
}
}
First I thought it may be a Namespace issue - however, User class could be found using same namespacing as my MessageTrait. Any ideas?
I solved this by adding namespace app to the top line of my Trait file. Everything works as expected now!

Doctrine 2 namespace issue

I'm using Zend Framework 1 with the Bisna library to integrate Doctrine 2. I generated my Entities from my database model with the Doctrine 2 CLI. This is all working fine, except for the setter methods for associated records. The argument they accept must be of a specific namespace (\Category here).
class Article
{
public function setCategory(\Category $category = null) {
$this->category = $category;
return $this;
}
}
However, when I do this:
$article = $this->em->getRepository('\Application\Entity\Article')->find(1);
$category = new \Application\Entity\Category();
$category->SetName('New Category');
$article->setCategory($category);
I get the following fatal error: Argument 1 passed to Application\Entity\CategoryField::setCategory() must be an instance of Category, instance of Application\Entity\Category given.
When I change the setter method to accept \Application\Entity\Category objects, it's working of course. Should I do this for every generated method, or are there other options? This is the first time I'm using namespaces, so it might be something simple.
You can always add this to the top of your class file: use \Application\Entity\Category; and then simply reference it later like so: public function setCategory(Category $category = null)
Check out: http://php.net/manual/en/language.namespaces.importing.php for more info about use
Otherwise you would have to reference the full namespace otherwise your application does not know that \Category is a reference to \Application\Entity\Category

How do I register multiple services with a specific interface in Castle Windsor

I have the following registration:
container.Register(AllTypes.FromAssemblyContaining<ITabViewModel>().BasedOn<ITabViewModel>());
Two classes:
public class StructureDecorationViewModel : NotificationObject, ITabViewModel
{
...
}
public abstract class NotificationObject : INotifyPropertyChanged
{
...
}
And two resolvers:
serviceProvider.ResolveAll<System.ComponentModel.INotifyPropertyChanged>()
serviceProvider.ResolveAll<ITabViewModel>()
Both of these Resolvers gives the StructureDecorationViewModel, how can I filter the registration so that I only register the ITabViewModel and not the INotifyPropertyChange?
To register against just one interface you would normally use FirstInterface:
AllTypes
.FromAssemblyContaining<ITabViewModel>()
.BasedOn<ITabViewModel>()
.WithService
.FirstInterface();
However in this case you would end up with your service registered against INotifyPropertyChanged which is not what you want as it picks the first interface from the base class (Have a look at the ServiceDescriptor class to see what other registrations are available).
What you need is the Select method that allows you to define the type or types you want to register the service against:
AllTypes
.FromAssemblyContaining<ITabViewModel>()
.BasedOn<ITabViewModel>()
.WithService
.Select(typeof(ITabViewModel));
However if you want to keep things more generic someone has written an extension method that looks at the service being registered and picks out the first interface on the derived class (http://www.hightech.ir/SeeSharp/windsor-registration-service-interface):
public static BasedOnDescriptor FirstInterfaceOnClass(this ServiceDescriptor serviceDescriptor)
{
return serviceDescriptor.Select((t, bt) =>
{
var baseInterfaces = t.BaseType.GetInterfaces();
var interfaces = t.GetInterfaces().Except(baseInterfaces);
return interfaces.Count() != 0 ? new[] {interfaces.First()} : null;
});
}
Which allows you to do this:
AllTypes
.FromAssemblyContaining<ITabViewModel>()
.BasedOn<ITabViewModel>()
.WithService
.FirstInterfaceOnClass();

Can I have conditional construction of classes when using IoC.Resolve?

I have a service class which has overloaded constructors. One constructor has 5 parameters and the other has 4.
Before I call,
var service = IoC.Resolve<IService>();
I want to do a test and based on the result of this test, resolve service using a specific constructor. In other words,
bool testPassed = CheckCertainConditions();
if (testPassed)
{
//Resolve service using 5 paramater constructor
}
else
{
//Resolve service using 4 parameter constructor
//If I use 5 parameter constructor under these conditions I will have epic fail.
}
Is there a way I can specify which one I want to use?
In general, you should watch out for ambiguity in constructors when it comes to DI because you are essentially saying to any caller that 'I don't really care if you use one or the other'. This is unlikely to be what you intended.
However, one container-agnostic solution is to wrap the conditional implementation into another class that implements the same interface:
public class ConditionalService : IService
{
private readonly IService service;
public ConditionalService()
{
bool testPassed = CheckCertainConditions();
if (testPassed)
{
// assign this.service using 5 paramater constructor
}
else
{
// assign this.service using 4 parameter constructor
}
}
// assuming that IService has a Foo method:
public IBaz Foo(IBar bar)
{
return this.service.Foo(bar);
}
}
If you can't perform the CheckCertainConditions check in the constructor, you can use lazy evaluation instead.
It would be a good idea to let ConditionalService request all dependencies via Constructor Injection, but I left that out of the example code.
You can register ConditionalService with the DI Container instead of the real implementation.
My underlying problem was that I was trying to resolve my class which had the following signature:
public DatabaseSchemaSynchronisationService(IDatabaseService databaseService, IUserSessionManager userSessionManager)
This was basically useless to me because my usersessionmanager had no active NHibernate.ISession because a connection to my database had not yet been made. What I was trying to do was check if I did have a connection and only then resolve this class which served as a service to run database update scripts.
When changing my whole class to perform the scripts in a different way, all I needed in its constructor's signature was:
public DatabaseSchemaSynchronisationService(ISessionFactory sessionFactory)
This allowed me to open my own session. I did, however have to first check if the connection was ready before attempting to resolve the class, but having IDatabaseSchemaSynchronisationService as a parameter to another class's constructor; this class also gettting resolved somewhere where I could not check the db connection was a bad idea.
Instead in this second class, I took the IDatabaseSchemaSynchronisationService paramater out of the constructor signature and made it a local variable which only gets instantiated (resolved) :
if (connectionIsReady)
Thanks to everyone who answered.