grosv/laravel-passwordless-login

Argument 1 passed to Grosv\LaravelPasswordlessLogin\LoginUrl::__construct() must be an instance of Grosv\LaravelPasswordlessLogin\Models\User, instance of App\User given

mnazmi23 opened this issue · 19 comments

It is possible that I am not doing it right since I am new to laravel. But I have been doing almost the same thing for other packages and it has been fine.

As for this package, it kept on reading the Models/User in the LoginUrl class. I will get the error like the title mentioned above.

But when I changed the LoginUrl to use App\User(which is my user model with a different table name), it works. Url generated nicely.

So I am not sure if this is a bug or an issue or it is meant to be this way.

Thanks!

@mnazmi23 I just merged a pull request from @innoflash that should fix your issue. Can you set the version of this package you're requiring in your composer.json to dev-master and see if it works now? If it does, let me know and I'll close this out. We'll be tagging a new release after we wrap up one more issue.

@edgrosvenor sure will do in a few hours when I got to work. Will update you back asap.

I have tested, it is working fine, no errors anymore, link was generated fine.

But I have another issue I guess(or maybe I missed something), I put the LPL_REDIRECT_ON_LOGIN=/home/dashboard and the URL uses middleware 'auth'. Clicking the URL from the email redirect it fine, but got kicked back to login. Seems like the Auth is not working?

@edgrosvenor

@mnazmi23 Sorry about that. I'm not sure what would cause that. I assume your model is authenticatable (extends Illuminate\Foundation\Auth\User and that it matches with the fully qualified classname you have configured in your model? Those are the only two things I can think of that would cause the authentication to fail. And we're about to remove them from the equation by moving much of this stuff to a trait that you'll be able to use in your model to provide all this stuff.

If the project you're working on is in GitHub we'll be happy to pull it down and make sure that our changes work with your real world implementation.

@edgrosvenor I love the trait idea. I will wait for the next release I guess. Not in a hurry to implement this. Currently already have default auth login and social login as alternative. Would be great to offer passwordless too without doing it from scratch.

My model is authenticable as it is the default auth implementation of laravel. The only things I changed are the home route and the actual user table(Users to Portal_Users).

Closed as the initial issue has been fixed. Will open a new issue if the auth issue persist on the next release. It is a different issue anyway if it is an issue.

@mnazmi23 I've tagged 1.0 with the trait included. Let me know if you have any issues. I'm pretty sure we identified and fixed the problem with the login not working.

@edgrosvenor it is still not working for me unfortunately. Do i need to do anything else after the link was generated? I sent an email with the url. Clicked it and it was going to the login route. I tried changing the redirect path to public ones(no auth middleware), and it worked fine.

Or, which part of my codes do you need to identify it? My auth implementation are default ones except the table name.

@mnazmi23 I've written a test now that uses a model / table combination that doesn't match with Laravel convention and it works fine. I've also added the method ->createPasswordlessLoginLink() to the trait. So you could just do something like User::find($id)->createPasswordlesssLoginLink() if your model uses the trait and that would ensure everything is structured as expected.

The only other thing I'm wondering is when you say you're using the default configurations, are these web or api routes that you're having trouble with?

Always happy to take a look at your actual app if you'd like to see if I can figure out what's going on. I always worry that my documentation isn't quite up to par and I don't want something I skipped causing problems.

@edgrosvenor I am using the web routes. Havent touched the api route yet.

I'll try and show you some of related code snippets from my project. Btw do i need to register the service provider in the config/app.php?

You shouldn't have to. Should be automatically registered.

Weird that my app does not have it. I'll try a bit later. Can you show how it should look like the app config? @edgrosvenor

@mnazmi23 I have found and corrected the real problem you were having. Apologies for the hassle. I'm 99.999% sure that if you update to the latest version 1.1.2 it'll work fine for you.

Still not working for me.

       public function login(Request $request)
        {
            abort_if(!$request->hasValidSignature(), 401);
            //true
            $user_model = $this->passwordlessLoginService->getUserClass($request->user_type);
             //App/User
            $user = $user_model::find($request->uid);
             //found my user instance
            $guard = $user->guard_name ?? config('laravel-passwordless-login.user_guard');
            //web
            $rememberLogin = $user->should_remember_login ?? config('laravel-passwordless-login.remember_login');
             //false
            $redirectUrl = $user->redirect_url ?? ($request->redirect_to ?: config('laravel-passwordless-login.redirect_on_success'));
             //    /home/dashboard  which is the default HomeController
            Auth::guard($guard)->login($user, $rememberLogin);
             //tried Auth::check() right after this gave me true
            abort_if(!Auth::guard($guard)->user(), 401);
             // returns my user instance again

            return $user->guard_name ? $user->onPasswordlessLoginSuccess($request) : redirect($redirectUrl);
        }

I even tried putting the url directly while returning, and it is still not working. Its like its kicking me out even though the Auth::check() returns true. When I disable the middleware the redirect works fine but of course my Auth::check() returns false on the HomeController.

Can you verify you have version 1.1.2 installed? If you look in the package’s routes.php the login route didn’t have the web middleware on it. That meant the session wasn’t being created on login. Annoyingly the tests showed the user persisting after redirect when they shouldn’t have.

Yes I am on 1.1.2. Updated right after you mentioned it.

Btw I think this is unrelated but returning this

$user->onPasswordlessLoginSuccess($request)

directly gave an method not found exception.

@mnazmi23 Yeah. Sorry. That checks out. I had created the pull request with the fix but hadn't merged it when I did the release. My kid was home from daycare all day so I'm working on half a brain. It really should be working now on 1.1.3. I'll check out the onPasswordlessLoginSuccess method too. That's part of our attempt to get this working with api middleware and tokens too.

@edgrosvenor fuhhh finally! Haha awesome! Got it working nicely now.

Haha I can relate. I am working before my daughter wakes up. Almost time though so I am just doing some clean up.

Finally can start implementing the passwordless login as an alternative for my users.

Thanks a lot man. This is a really good package.

Glad you like it. Thanks for sticking with me through this madness.