How to configure JWT-Auth using Laravel 5

Pretty new to laravel, and I'm creating a backend API for an Angularjs application with it, and now I'm stuck in the authentication part.

I decided to go https://github.com/tymondesigns/jwt-auth to handle authentication and tokens.

The problem is that the JWT-Auth wiki doesn't tell me anything about how to set up a user database. It only tells me that I can feel that way.

$token = JWTAuth::attempt($credentials) 

But of course, this will not work before he has a place to search for these credentials.

How can I do it?

Thanks!

+5
source share
1 answer

Laravel 5 already comes with a user model, if you transferred the User table to your database (set the database credentials in the .env file in the root of your project) using php artisan migrate , then it's just a matter of creating a user.

Migration files for the user model must also be included.

You can then create a user by creating a database seed, or simply do:

 User::create( [ 'name' => 'you', 'email' => ' you@you.com ', 'password' => Hash::make('secret') ] ); 

Then you can log in using JWTAuth with credentials in an array of type

 ['email' => ' you@you.com ', 'password' => 'secret'] 

Depending on where you call User::create() , you may need to enable

 use App\User; 

at the top of your controller, where the App is your application namespace. App is the default namespace, but you can easily change it to whatever you want, it is completely optional, and you do not need to do this. Just add this if you changed it.

Edit: Perhaps this external tutorial on scotch.io can help you more in the long run.

+4
source

All Articles