jeudi 21 février 2019

Laravel JWT "undefined variable: token" on api/login

I have been attempting to creating a website where a user can make an account and login (among other things), and I can register a user, but when I try to log in, I get a 500 error. So I checked the network tab in the inspector and it's telling me "undefined variable": token. I am questioning whether it's something in my controller or maybe i'm missing a header. I've since created a jwt-secret, added the csrf token to my headers, and checked my verifycsrftoken.php to see if there were any exceptions that I accidentally added. If there is something that I have not done or something that I have missed, please help me out.

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Facades\JWTFactory;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Contract\JWTSubject;
use Tymon\JWTAuth\PayloadFactory;
use Tymon\JWTAuth\JWTManager as JWT;

class UserController extends Controller
{

public function register(Request $request)
{
    $validator = Validator::make($request->json()->all() , [
        'name'=> 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6',    
    ]);

    if($validator->fails()){
        return response()->json($validator->errors()->toJson(), 400);
    }

    $user = User::create([
        'name' => $request->json()->get('name'),
        'email' => $request->json()->get('email'),
        'password' => Hash::make($request->json()->get('password')),
    ]);

    $token = JWTAuth::fromUser($user);

    return response()->json(compact('user', 'token'), 201);
}


public function login(Request $request)
{
    $credentials = $request->json()->all();

    try {
        if (! $token == JWTAuth::attempt($credentials))
        {
            return response()->json(['error' => 'invalid_credentials'], 400);
        }
    }catch(JWTException $e){
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    return response()->json(compact('token'));
}


public function getAuthenticatedUser()
{
    try {
        if(!user == JWTAuth::parseToken()->authenticated()){
            return response()->json(['user_not_found'], 404);
        }
    }catch(Tymon\JWTAuth|Exceptions\TokenExpiredException $e){
        return response()->json(['token_expired'], $e->getStatusCode());
    }catch(Tymon\JWTAuth|Exceptions\TokenInvalidException $e){
        return response()->json(['token_invalid'], $e->getStatusCode());
    }catch(Tymon\JWTAuth|Exceptions\JWTException $e){
        return response()->json(['token_absent'], $e->getStatusCode());
    }

    return response()->json(compact('user'));
}


}

And this is my function for hitting that route via my react frontend

export const login = user => {
    return axios
        .post('/api/v1/login',
            {
                email: user.email,
                password: user.password,
                'X-CSRF-TOKEN': csrf_token
            },
            {
                headers: { 'Content-Type': 'application/json' },
            })
        .then(res => {
            localStorage.setItem('usertoken', res.data.token)
            console.log(res)
        })
        .catch(err => {
            console.log(err)
        })
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire