I am trying to learn Laravel and my goal is to create a RESTful API (without using views or clicks, only JSON results. Later, the AngularJS web application and the Cordova hybrid mobile app will use this api.
After some research, I tend to choose the JWT-Auth library for full stateless benefits. My problem: I have 2 main types of users: clients and moderators . Customers are not required to have a password. I need to be able to generate a token for access only with the email provided. If this letter exists in the database and belongs to the client, it will generate and return a token. If it exists and belongs to the moderator, it will return false so that the interface can request a password. If the letter does not exist, it throws an invalid parameter error.
I read the docs here and it says you can use custom claims. But the documents do not explain what the claims are, and what this means is that the array is passed as user requirements. I would like information on how to achieve what I explain above.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use JWTAuth; use Tymon\JWTAuth\Exceptions\JWTException; class AuthenticateController extends Controller { public function authenticate(Request $request) { $credentials = $request->only('email', 'password'); try {
Thanks.
Update
Reward code
public function authenticate(Request $request) { $email = $request->input('email'); $user = User::where('email', '=', $email)->first(); try { // verify the credentials and create a token for the user if (! $token = JWTAuth::fromUser($user)) { return response()->json(['error' => 'invalid_credentials'], 401); } } catch (JWTException $e) { // something went wrong return response()->json(['error' => 'could_not_create_token'], 500); } // if no errors are encountered we can return a JWT return response()->json(compact('token')); }
angularjs authentication php jwt
Marco aurรฉlio deleu
source share