tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.27k stars 1.55k forks source link

Post 419 (unknown status) error #1313

Open packytagliaferro opened 6 years ago

packytagliaferro commented 6 years ago

So I have removed all laravel 5.5 oringinal auth stuff so my Auth\LoginController now looks like this:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
     public function __construct()
     {
         $this->middleware('guest')->except('logout');
     }

    public function authenticate(Request $request)
    {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');

        return response()->json( $credentials );

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));
    }

}

Route in Web.php Route::post('/authenticate', 'Auth\LoginController@authenticate');

In my login.vue file:


 login(){

                axios.post('/authenticate',{
                    email: this.email,
                    password: this.password
                })
                .then(response => {

                    console.log(response);

                })
                .catch(error => {

                });

            }

If I try to login I get this error:

419 (unknown status)

{message: "", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}
exception
:
"Symfony\Component\HttpKernel\Exception\HttpException"
file
:
"/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php"
line
:
203

I am new to Token Auth so not sure whats going on. The error seems pretty vague

packytagliaferro commented 6 years ago

For anyone facing this I had to put the route in API like

Route::post('/authenticate', 'Auth\LoginController@authenticate');

then post to

/api/authenticate

mrjnamei commented 6 years ago

need csrf_tokens .