Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Explore using a service worker to pass ID tokens #680

Draft
wants to merge 7 commits into
base: v1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}
}
5 changes: 5 additions & 0 deletions example-app-router/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copy this file as ".env.local".
# Update these with your Firebase app's values.
NEXT_PUBLIC_FIREBASE_PUBLIC_API_KEY=MyExampleAppAPIKey123
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=my-example-app.firebaseapp.com
NEXT_PUBLIC_FIREBASE_PROJECT_ID=my-example-app-id
3 changes: 3 additions & 0 deletions example-app-router/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'next/core-web-vitals'
}
38 changes: 38 additions & 0 deletions example-app-router/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# Built service worker
public/service-worker.js
26 changes: 26 additions & 0 deletions example-app-router/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

## Using Service Worker for Auth

This is a proof of concept for using a service worker to send ID tokens to server-rendered pages. See [this issue](https://github.com/gladly-team/next-firebase-auth/issues/287) and [Firebase docs](https://firebase.google.com/docs/auth/web/service-worker-sessions).

### General flow:
1. On the auth page, register the service worker, which initializes Firebase, sets up request interception, and will save the authed user.
2. On future requests, the service worker gets the ID token (refreshing if needed) and adds it as a request header.
3. Middleware on the server receives the ID token, verifies it, and gets the user. We add the user to auth context.

### WIP Notes
To develop at the moment:
1. Run this app to register the service worker on localhost
2. Auth in another app on localhost, since auth is not yet set up here
3. Return to this app
4. Inspect requests to see SW-added headers


### To Do
1. Add middlware to get the user from the ID token server-side
2. Pass user to auth provider (server component)
3. Clean up service worker: remove unneeded logic, maybe use Workbox, make sure types aren't erroring
4. Add auth to this example
5. Move code into NFA under unstable APIs
6. Clean up this example: get parent ESLint config working
7. Later: see if the middleware can also support existing cookies (see [issue](https://github.com/gladly-team/next-firebase-auth/issues/418#issuecomment-1203655282)). This way, developers with apps on the edge could use either cookies (no ID token) or integrate a service worker.
8 changes: 8 additions & 0 deletions example-app-router/app/api/thing/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NextResponse } from "next/server"

export async function GET(request: Request) {
console.log('GET /api/thing')
return new Response(JSON.stringify({ foo: 'bar' }), {
status: 200,
})
}
Binary file added example-app-router/app/favicon.ico
Binary file not shown.
107 changes: 107 additions & 0 deletions example-app-router/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
:root {
--max-width: 1100px;
--border-radius: 12px;
--font-mono: ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono',
'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro',
'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace;

--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;

--primary-glow: conic-gradient(
from 180deg at 50% 50%,
#16abff33 0deg,
#0885ff33 55deg,
#54d6ff33 120deg,
#0071ff33 160deg,
transparent 360deg
);
--secondary-glow: radial-gradient(
rgba(255, 255, 255, 1),
rgba(255, 255, 255, 0)
);

--tile-start-rgb: 239, 245, 249;
--tile-end-rgb: 228, 232, 233;
--tile-border: conic-gradient(
#00000080,
#00000040,
#00000030,
#00000020,
#00000010,
#00000010,
#00000080
);

--callout-rgb: 238, 240, 241;
--callout-border-rgb: 172, 175, 176;
--card-rgb: 180, 185, 188;
--card-border-rgb: 131, 134, 135;
}

@media (prefers-color-scheme: dark) {
:root {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;

--primary-glow: radial-gradient(rgba(1, 65, 255, 0.4), rgba(1, 65, 255, 0));
--secondary-glow: linear-gradient(
to bottom right,
rgba(1, 65, 255, 0),
rgba(1, 65, 255, 0),
rgba(1, 65, 255, 0.3)
);

--tile-start-rgb: 2, 13, 46;
--tile-end-rgb: 2, 5, 19;
--tile-border: conic-gradient(
#ffffff80,
#ffffff40,
#ffffff30,
#ffffff20,
#ffffff10,
#ffffff10,
#ffffff80
);

--callout-rgb: 20, 20, 20;
--callout-border-rgb: 108, 108, 108;
--card-rgb: 100, 100, 100;
--card-border-rgb: 200, 200, 200;
}
}

* {
box-sizing: border-box;
padding: 0;
margin: 0;
}

html,
body {
max-width: 100vw;
overflow-x: hidden;
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
}

a {
color: inherit;
text-decoration: none;
}

@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
}
22 changes: 22 additions & 0 deletions example-app-router/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import './globals.css'
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
Loading