Laravel, vue.js, jwt

0

Zacząłem zabawę z JWT, stworzyłem komponent logowania Login


export default {
      data(){
        return{
           credential: {
               email: '',
               password: '',
           }
        }
      },
       methods:{
           login(){
             this.$http.post('http://127.0.0.1:8000/api/user/login', this.credential)
               .then(respone => respone.json())
               .then(result => {
                 console.log(result);
                 localStorage.setItem('token', result.token);
               });
           }
       },
       created:function () {
 
       }
   }

w Laravelu kontroler UserController


class UserController extends Controller
{
    public function login(Request $request){
        $userData = $request -> only('email', 'password');
        if (!$token = JWTAuth::attempt($userData)){
            return response()->json([
                'error' => 'Invalid login or password',
            ],401);
        }
        return response()->json([
            'token' => $token,
        ],200);
    }
}

próbując się zalogować i uzyskać token w konsoli otrzymuję błąd

POST http://127.0.0.1:8000/api/user/login 404 (Not Found)
login:1 Failed to load http://127.0.0.1:8000/api/user/login: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. The response had HTTP status code 404.
login:1 Uncaught (in promise) Response {url: "http://127.0.0.1:8000/api/user/login", ok: false, status: 0, statusText: "", headers: Headers, …}

nie mam pojęcia dlaczego API nie jest ładowane :/

0

używam CORSa, mam dodanego

No nie masz - gdybyś miał, nie byłoby błędu ;-)

Opowiedz jak konfigurowałeś CORS.

0

1. composer require barryvdh/laravel-cors
2. \Barryvdh\Cors\HandleCors::class,
3, php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"
wcześniej chciałem wyświetlać artykuły, bez CORS też nie działało, doinstalowałem, teraz wyświetlanie działa ok jednak tutaj...

0

W takim razie to wygląda dobrze - dlaczego Twój request dostaje zwrotkę 404?

0

Dodałeś nagłówki CORS w .htaccess ?

1 użytkowników online, w tym zalogowanych: 0, gości: 1