laravel 5.3 + Tymon\JWTAuth = How to create token with own authentication - php

Im building a Restful api, except that I DONT want to use Laravel's User authentication system (instead I use my own).
So im on stuck here:
$this->jwt_token = JWTAuth::attempt(['email'=>$email, 'pass'=>$pass])
which gives me the following error:
Type error: Argument 1 passed to
Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an
instance of Illuminate\Contracts\Auth\Authenticatable, instance of
App\User given, called in
**\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 387
So how do I solve this? any idea?
Thanks

It looks like your User model isn't extending the correct Eloquent class.
Pull up your user model, and make sure it extends Illuminate\Foundation\Auth\User.
In the default Laravel install, this is imported via use Illuminate\Foundation\Auth\User as Authenticatable, then the model extends Authenticatable.
If you don't want to use the User model for authenticating, you can change which model should be used in config/jwt.php, but whatever you choose will need to extend the Auth\User class as above.

Related

where does laravel can function for user object define?

i am working on a laravel project .almost i see in each controller these line format :
!auth()->user()->can('something (differ from each controller to other one)')
but in my php editor it says method {can} not found for this object. so i try to found method can and not found it .even i edit __Call magic method to see if when can method is calling does magic function run but i know it never run for can function.so how it is possible to link function to object when it is not defined in class and its all mother class.and where does can function locate?i search and i see laravel has some policy for authorize users but yet i don not know how can function link to user object without
define in classes and even magic method does not run .and how can i develope these type of function (for authorizing in laravel and change policy)
can method is a part of Authorizable trait in Illuminate\Foundation\Auth\Access
Look at your User model, you will found that model is extended from Illuminate\Foundation\Auth\User as Authenticatable. and Authenticatable uses the above trait.
it's part of the core of laravel, you can read the documentation from official docs

Where is Laravel Slack Webhook URL defined

Continuing with my Laravel 5.5 project, I'm confused as to where you define the slack webhook URL in the Laravel app.
https://laravel.com/docs/5.5/notifications#routing-slack-notifications shows defining a routeNotificationForSlack method on the User (or whatever notifiable model) that returns $this->slack_webhook_url-- but that var is never populated:
class User extends Authenticatable
{
use Notifiable;
public function routeNotificationForSlack()
{
return $this->slack_webhook_url;
}
Should the slack_webhook_url be hardcoded in this model, or is it passed in when you instantiate the notification?
it's up to you how to implement routeNotificationForSlack(), Laravel will only call it and use the return value to determine what url to ping.
If all your users share same webhook endpoint, you can store it in .env, if they are different for each user, return a database column like the example. If it depends on group/privilege, you should implement it accordingly

custom method in laravel 4.1 model not working

I am trying to use custom method in User model with class name User in laravel4.1. i changed the $table attribute to my table name and added a custom method names 'public function abc' in user model. Then in my user controller i tried like this :-
$u= new User;
$u->abc();
but its not working and giving following error :-
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::abc()
and i dnt know why this happening everything seems fine,help me out in this guys.
UPDATE :SOLVED ,Done Nothing
I DO NOT KNOW WHAT IS THE PROBLEM WITH LARAVEL
$u= new user;
$u->abc();
i just changed User to user and its started working and i dnt even know why ,anyone know reason??
Every method on a Model get passed on to a new QueryBuilder
User::where()
User::find()
User::{relationship}()
If you instantiate a model like this
$user = User::find()->method();
it will work. Don't try to make your Eloquent models too fat.
Just create a Repository to make your Controllers as thin as possible and your Eloquent just as intelligent as it can be by using the tools given by Eloquent (relationships, hidden attributes, accessors & mutators, $this->appends, ...)
Everything else belongs in your Repositories.
Try running
composer dumpautoload
If this doesn't work, be sure that there is only one class called User in your whole project. There may be a package, migration or something with the same name. Try putting your custom User class in a namespace.

Getting "ErrorException" while implementing Laravel 4 class Auth::login()

I'm building a web application where users can signup and post questions. Everything went alright until I was trying developing a function where users will be automatically logged in upon registration. I used Auth::login() class just like I used to do with Laravel 3 but I'm getting the following error:
"ErrorException Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Auth\UserInterface, instance of Member given"
while I implemented the class with registered user's creds. I guess Laravel 4 has changed the way the class would work in it's previous version but I can't figure it out in their documentation as well as searching here in StackExchange before I post this question.
Here is the codes in my UserController
public function postCreate() {
$validator = Member::validate(Input::all());
if ($validator->passes()) {
$user = Member::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();
}
}
I'm a bit confused about the default User.php model provided with Laravel 4. I usually would create a User model based on the users table in my database while I would work in laravel 3. But this time in laravel 4 I had to change my user table name to members so that I can use my own model Member.php. Is it alright if I remove the codes in default User model or should I place my own functions at the top keeping the existing codes provided by Laravel?
P.S. I'm relativeley new with the MVC concept and started learning with tutorials on Laravel 3 and that's why I'm finding it hard to get myself used to with Laravel 4.
You must implement UserInterface to fix the error. In addition, if you need password reminder functionality, you have to implement RemindableInterface as well.
In your Member class add the following lines:
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Member extends Eloquent implements UserInterface, RemindableInterface {
As the Exception says, your Member-Model needs to implement UserInterface in order to use the Auth helper:
class Member extends Eloquent implements UserInterface
Here a quote from Code Bright by Dayle Rees:
Our User model implements both UserInterface and RemindableInterface. What are these for?
Well the UserInterface let’s Laravel know that the model contains all methods needed for it to
authenticate with Laravel’s own authentication system. The RemindableInterface allows Laravel
to retrieve the users email address or other contact information to allow for the functionality of
sending password reminder emails.

Referring to an Eloquent model from package

I am trying to use an Eloquent model inside the Sentry package (this could've been any package).
I've refered to it using use App\Models\User; in the top, and User:: when I need to call it.
However, I am getting this error: Class 'App\Models\User' not found
What am I doing wrong?
The current code is here: http://paste.laravel.com/H19
I assume thath your models are in the App part of the application.
In laravel, the App namespace is the top level namespace (the default one).
So to call your models, you have to call \User (because \ refer to the top level namespace)
Alternatively you can do:
use \User as User
And then you can call it User instead of \User

Categories