PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Using API endpoints from within wave app after signin via blade templates

Solved
graham

Sep 6th, 2023 03:40 PM

Hello, I'm looking for direction of what to do here.

I have set up an api endpoint and added it within a middlewhere('auth:api') group.

My users would have already signed into the app using the existing laravel blade pages for /signin and are already on the dashboard.

From the dashboard, I am wondering, how do I authenticate the API endpoints? I can't generate a JWT token flow, beacause I already have a logged in user.

Basically, I am wondering what the best strategy is for using APIs from within the signed in dashboard page to load in resources without full page loads?

bobbyiliev

Sep 7th, 2023 02:27 AM

Hi there,

As you mentioned that the main reason for this would be to load in resources without full page loads, what you could do is to use Laravel Livewire.

This will simplify your implementation a lot. Here is a quick Livewire introduction tutorial that might be helpful:

https://devdojo.com/episode/laravel-livewire-introduction

That way you will not have to implement any additional authentication and will be able to use the standard web authentication but still benefit from the reactivity that Livewire offers.

Would this work for you?

graham

Sep 7th, 2023 09:36 AM

Thanks Bobby, That would work for most of my usecases. Where it would not is for streaming results back to the user via server side events. I don't know how to handle streaming content via livewire.

Specifically, I'm using an OpenAI php library (https://github.com/openai-php/client) to stream GPT4 chat completions back to the user as Open AI provides me with the response.

I don't mind having an API auth and web auth working together if that's the only solution, I just don't know how to get token information to use for the authenticated API requests without fully changing how the Wave APP works.

Any help would be greatly appreciated 🙏

bobbyiliev

Sep 8th, 2023 01:23 AM

Best Answer

Hey there!

Ah yes, that makes sense! I remember seeing a similar implementation with Livewire and OpenAI but I can't find the blog post. I will look into this and see if I can create a little example app and write a post for this!

But in the mean time SSE is indeed a good implementation! Here is a very nice example on how to do this:

https://ahmadrosid.com/blog/laravel-openai-streaming-response

In this example, the author is using just a standard route rather than an API route, and what you could do is just add the auth logic within the controller or add the route to the auth middleware. There will be no need for this SSE route to be under the API route group.

Let me know how it goes and I will keep you posted about the Livewire implementation and if I can come up with a nice way of handling this.

Best,

Bobby

Report
1
graham

Sep 8th, 2023 04:11 PM

Yup! that worked like a charm. Thank you very much!

bobbyiliev

Sep 9th, 2023 01:52 AM

That is great! Happy to hear that it is all working!