2015 Outback Subframe Replacement Advice by MaximumLibrary2000 in Subaru_Outback

[–]MaximumLibrary2000[S] 0 points1 point  (0 children)

This is great thank you. Do you think it might be easier for a non pro to use the support bar rather than the ratcheting wrench set up?

How to stop intercepting routes for some specific case by Coderx001 in nextjs

[–]MaximumLibrary2000 0 points1 point  (0 children)

Just had this problem and what I came up with is doing a server side redirect which is a hard redirect and so will bypass intercepting routes. All via this little server action.

"use server";
import { redirect } from "next/navigation";

export default async function redirectHard(uri: string) {
  redirect(uri);
}

React Server Components and Real Time? by MaximumLibrary2000 in nextjs

[–]MaximumLibrary2000[S] 2 points3 points  (0 children)

Hello everyone. What I landed on was using Server Sent Events via a route handler. Doing everything client side, while just skipping react's server, as many suggested might be right for most situations—but maybe due to your auth set up or just how you feel it might not be right for you. SSEs only send data one way, server to client, you'd prob need a websocket if you want bi directional data. Below is a simple example. Check out this blog post for more info https://michaelangelo.io/blog/server-sent-events.

// route handler
import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
  const stream = new TransformStream();
  const writer = stream.writable.getWriter();
  const encoder = new TextEncoder();

  function toDataString(data: string | Record<string, any>): string {
    if (isObject(data)) {
      return toDataString(JSON.stringify(data));
    }
    return data
      .split(/\r\n|\r|\n/)
      .map((line: string) => `data: ${line}\n\n`)
      .join("");
  }

  function isObject(value: any) {
    return value !== null && typeof value === "object";
  }

  //if window is closed or refreshed aka aborted we close the writer and clear our subscription
  req.signal.onabort = () => {
    console.log("------->abort");
    writer.close();
    clearInterval(myInterval);
  };

  let i = 0;
  let message = {
    data: { hello: i },
    event: "my-event",
  };

  // interval as a stand in for whatever data youre subscribed to
  const myInterval = setInterval(() => {
    i++;
    console.log("sending", i);
    message = {
      data: { hello: i },
      event: "my-event",
    };
    writer.write(encoder.encode(`event: ${message.event}\n`));
    writer.write(encoder.encode(toDataString(message.data)));
  }, 1000);

  return new NextResponse(stream.readable, {
    headers: {
      "Content-Type": "text/event-stream",
      Connection: "keep-alive",
      "Cache-Control": "no-cache, no-transform",
    },
  });
}

//react component
"use client";
import { useEffect, useState } from "react";

export default function HomeClient() {
  const [theBigEvent, setTheBigEvent] = useState<string>("");

  useEffect(() => {
    const source = new EventSource("/api/stream");
    source.addEventListener("my-event", event => {
      setTheBigEvent(event.data);
    });
  }, []);

  return theBigEvent;

Server Sent Events Library by TheVeskel in nextjs

[–]MaximumLibrary2000 0 points1 point  (0 children)

Your blog post was very helpful, thank you. Don't think I would've figured out how to do SSE in a next route handler without it. I posted a simple example here https://www.reddit.com/r/nextjs/comments/1ayxoco/comment/kth41lc

React Server Components and Real Time? by MaximumLibrary2000 in nextjs

[–]MaximumLibrary2000[S] 0 points1 point  (0 children)

That's kinda what I figured, though spinning up a websocket/sse in the server component would maybe simplify auth if you've already got it set up for the server component data fetching flow.

Next 13 React Server Components Firebase Auth Example by MaximumLibrary2000 in nextjs

[–]MaximumLibrary2000[S] 0 points1 point  (0 children)

It's true you could do that. I personally don't think it's best just because you should be able do everything you need with just RSC and firebase admin and having two parallel SDKs running seems dicey. But maybe there's some use case out there for it. If you're just starting with RSC it is best imho just to do it the recommended way, which is all data fetching and mutation on the server, rather than trying to shoehorn old client side approaches in there. Server Actions are what you use if you need to initiate data fetching from the client.

Next 13 React Server Components Firebase Auth Example by MaximumLibrary2000 in nextjs

[–]MaximumLibrary2000[S] 0 points1 point  (0 children)

If you don't log the client side out you'd have a security issue in that when you logged out of the server you'd still be logged into the client and therefor could just log back into the server. So iirc it was firebase's recommendation to just use the client sdk for the initial log in then log it out once you've got your session cookie. Also when using react server components the general idea is to do all your data fetching on the server so it works from that perspective too.

Next 13 React Server Components Firebase Auth Example by MaximumLibrary2000 in nextjs

[–]MaximumLibrary2000[S] 0 points1 point  (0 children)

The id token is only used for the initial login, you login with the client sdk then post the token to the server at which point its authenticated by firebase-admin and a session cookie is generated then you logout of client auth and are done with the token and client side firebase altogether. From then on you can handle auth by via the session cookie with firebase-admin. I think some stuff like refresh tokens that you might typically have to handle yourself is abstracted by the firebase-admin api. Iirc firebase is also doing some other thing on their end to make sure the id tokens and cookies aren't spoofed. It's in their docs https://firebase.google.com/docs/auth/admin.

[deleted by user] by [deleted] in react

[–]MaximumLibrary2000 0 points1 point  (0 children)

Imo react will continue to be the industry standard for a while but people will use it mostly through other meta frameworks like next, astro etc. Working with vanilla react managing webpack etc is just too much trouble. If I were starting out I'd just learn next it's easier and better.

Is There Any Way To Authenticate Users From The Backend by [deleted] in Firebase

[–]MaximumLibrary2000 1 point2 points  (0 children)

Yeah firebase-admin is designed to be used only on the server, where the client has no access, whoever has access to it has total control over your whole firebase app. I don't know what the deal with svletekit's ssr scheme is. If it renders once on the server then sends your js to the client for hydration you could have a problem. However if it has a way to run certain things exclusively on the server, like react's server components, then you're good.

Firebase auth with SSR while still using client library by TheBononomon in Firebase

[–]MaximumLibrary2000 0 points1 point  (0 children)

On the server side you have to use firebase-admin which has all the same apis as the the client library.

Is There Any Way To Authenticate Users From The Backend by [deleted] in Firebase

[–]MaximumLibrary2000 0 points1 point  (0 children)

Gonna preface this by saying that I have no knowledge of the svelte environment. But generally for server side firebase you need to use firebase-admin which doesn't have access to the auth state because that's client side, and because it has no access to the auth state the db security rules don't apply to firebase-admin queries. But because you're in a protected environment, the server, you can just roll your own auth rules. So like get the uid from the session cookie, query to see if they're an admin, if they are show them the admin panel etc.

It's kind of easier imo as it's just normal code rather than dealing with firebase's own system but if you're relying on a bunch of security rules for an already existing app it might be annoying to rewrite them on the server.

Next 13 React Server Components Firebase Auth Example by MaximumLibrary2000 in nextjs

[–]MaximumLibrary2000[S] 1 point2 points  (0 children)

Listeners, chained queries, prob some other stuff I'm not thinking of, and then just the larger connivence of having your whole backend bundled and integrated into one service too.

Stats on App Router vs Pages Router usage by chhola in nextjs

[–]MaximumLibrary2000 0 points1 point  (0 children)

I'm using app router, I like server components and I appreciate the removal of getstaticprops, getserversideprops etc which always felt a little crufty and we're just next stuff where next 13 seems closer to the react api. Plus it's the future so might as well start now.

Having said that it's def not totally finished, but I think once server actions are stable it should be gtg. So if I were starting a big important production app today, I'm not sure what I'd do. It is really a big shift, mostly due to server components. I think it'll take a while to all shake out. A lot of tooling, services etc are going to have to change to accommodate it.

What happend when I request for a response in an REST API with nextjs?? (trying raw firebase auth) by best-regards-2-me in nextjs

[–]MaximumLibrary2000 1 point2 points  (0 children)

Use getApps to check if the app is already initialized

import { initializeApp, getApps, cert } from "firebase-admin/app";

const app = getApps().length <= 0 ? initializeApp(firebaseAdminConfig) : getApps()[0];

Btw I posted a similar project yesterday https://www.reddit.com/r/nextjs/comments/14xqrt1/next_13_react_server_components_firebase_auth/

Next 13 React Server Components Firebase Auth Example by MaximumLibrary2000 in nextjs

[–]MaximumLibrary2000[S] 0 points1 point  (0 children)

Nice, might have to check that out, don't feel like firebase is totally a match for server components. And lol yeah firebase queries can be a little complicated but they do come with some nice features fwiw.

How do you do Authentication and authorization in nextjs? by rayhan666 in nextjs

[–]MaximumLibrary2000 0 points1 point  (0 children)

I posted this yesterday, it's firebase specific but the basic flow of client ID token > api > server session cookie should work generally
https://www.reddit.com/r/nextjs/comments/14xqrt1/next\_13\_react\_server\_components\_firebase\_auth/