Laravel passport 404, Trying to connect a front end client to Laravel back-end api

Viewed 45

Please help explain me a bit. I'm very new to this back-end, OAuth 2.0. I'm trying to make my Laravel project to become an api with Passport authorization system. But after following the passport installation guide from official documents and watching youtube tutorials, I still can't connect Laravel to my front end. Can you please point out what I'm missing ? Sorry if this question is super vague, I don't really understand much what's going on.

How and where do I create a new client for my passport back end api ? Should this code be in my front end (or in laravel?) ?

const data = {
    name: 'Client Name',
    redirect: 'http://example.com/callback'
};
 
axios.post('/oauth/clients', data)
    .then(response => {
        console.log(response.data);
    })
    .catch (response => {
        // List errors on response...
    });

In the official documents which talks about issuing access tokens with JSON api, there's this explanation.

However, you will need to pair Passport's JSON API with your own frontend to provide a dashboard for your users to manage their clients. Below, we'll review all of the API endpoints for managing clients. For convenience, we'll use Axios to demonstrate making HTTP requests to the endpoints.

The JSON API is guarded by the web and auth middleware; therefore, it may only be called from your own application. It is not able to be called from an external source.

It didn't explain how I could pair my frontend to this Laravel app. I tried adding this to my front end javascript.

const BASE_URL = 'http://127.0.0.1:8000/api';
**
 * Trying Passport JSON api to get all clients for a given user. 
 */
const getClients = async () => {
    try {
        const response = await axios.get(`${BASE_URL}/oauth/clients`);
        const clients = response.data;

        console.log('Here are your clients', clients);
    } catch (errors) {
        console.error(errors);
    }
}


/**
 * Trying to create a new client. Not sure if this is equivalent to linking this external app to the backend api or not
 */
const addClient = async() => {
    const data = {
        name: 'front end jajaja',
        redirect: 'http://www.google.com'
    };

    const response = await axios.post(`${BASE_URL}/oauth/clients`, data);
    console.log(response.data);
}
/**
 * main function to test it all
 */
const main = () => {
    addClient();
    // getClients();
    
}
main();

but after I ran npm run dev and the main() method is called from the front end, console displayed an error saying

POST http://127.0.0.1:8000/api/oauth/clients 404 (Not Found)

I haven't edit routes/api.php.

<?php

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

And from a tutorial by Andrew, he said he used Laravel breeze to make a mock client, but can I use my super simple front end JavaScript instead ? Can you please explain me, how can I connect my front to the back ? How can I get the authorization code, client secret ??

0 Answers
Related