mardi 11 octobre 2016

JWT custom authentication in Laravel 5

i have made a custom model class for authentication in Laravel 5. I already changed the auth.php and jwt.phpattributes that represents User class.

The JWT is working, but, when i try to login it returns the invalid credentials error, even when i have the register in my database.

I've been thinking it should be something about encryptation, so i tried to encrypt my password and i have updated with the hash de password field in DB (it wasn't encrypted yet). But it stills bring me the credentials error.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\AuthRequest;
use App\Http\Requests;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthController extends Controller
{
    public function login(AuthRequest $request) {
        $credentials = [
            'email'    => $request->input('email'),
            'password' => bcrypt($request->input('password'))
        ];

        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);
        }

        return response()->json(['token' => $token]);
    }
}

Someone knows how i can solve this problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire