Send different content based on authentication data

12 Views Asked by At

I am new to firebase authentication, and I can't find any info about this feature that I want to implement:

  1. A user types this in a search bar: mywebsite.com/admin
  2. If the user is logged in --> send him /admin page
  3. If the user is not logged in --> send him different page

Can I implement this on the server-side? Or I still need first to load one page (/admin), and only then redirect the user to different page?

I use next.js + express + AWS hosting + React, in case in may be useful.

1

There are 1 best solutions below

0
On BEST ANSWER

On the server-side in the code for your admin page you will need to get the ID token of the user to detect if they're logged in, and authorized, to access the page. If not, you'll redirect them back to the main page.

This process is best described in the Firebase documentation on verifying ID tokens, but boils down to:

  1. Get the ID token on the client
  2. Pass it in the call on the server
  3. On the server decode and verify the ID token with the Admin SDK
  4. Ensure the user is authorized, or send back a redirect if they're not.