Created
January 8, 2019 16:39
-
-
Save OlabodeAbesin/b9cf778db499e8f531c7e36d199ced08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers\Api\Admin\Auth; | |
| use App\Http\Controllers\Controller; | |
| use Illuminate\Http\Request; | |
| use Validator; | |
| use JWTFactory; | |
| use JWTAuth; | |
| use JWTAuthException; | |
| use App\Admin; | |
| use Illuminate\Support\Facades\Auth; | |
| class LoginController extends Controller | |
| { | |
| public function __construct() | |
| { | |
| $this->admin = new Admin; | |
| } | |
| public function login(Request $request) | |
| { | |
| $validator = Validator::make($request->all(), [ | |
| 'email' => 'required|string|email|max:255', | |
| 'password'=> 'required' | |
| ]); | |
| if ($validator->fails()) { | |
| return response()->json($validator->errors()); | |
| } | |
| config()->set( 'auth.defaults.guard', 'admin' ); | |
| \Config::set('jwt.user', 'App\Admin'); | |
| \Config::set('auth.providers.users.model', \App\Admin::class); | |
| $credentials = $request->only('email', 'password'); | |
| $token = null; | |
| try { | |
| if (! $token = JWTAuth::attempt($credentials)) { | |
| return response()->json(['error' => 'invalid_credentials'], 401); | |
| } | |
| } catch (JWTException $e) { | |
| return response()->json(['error' => 'could_not_create_token'], 500); | |
| } | |
| // user = JWTAuth::toUser($token); | |
| // $user = Auth::user(); | |
| return response()->json(compact('token')); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to authenticate 2 different databases using jwt. I can connect 2 databases but in JWTAuth::attempt($credentials) always use one db.
could you please help me? Please refer my code
`
public function authenticate(Request $request, $cid)
{
$validator = Validator::make($request->all(), [
'username' => 'required',
'password' => 'required',
]);
if ($validator->fails()) {
return response()->json(['success' => false, 'message' => 'Data Not Found', "error" => $validator->errors()], 422);
}
`