-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add background worker, update repo and apps (#3)
* wip(extension) Testing background scripts * Updated web app and extension, added background worker to extension
- Loading branch information
Showing
16 changed files
with
7,975 additions
and
7,046 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { __unstable__createClerkClient as createClerkClient } from '@clerk/chrome-extension/background'; | ||
|
||
console.log('Background Script w/ Clerk createClerkClient() demo loaded') | ||
|
||
const publishableKey = process.env.PLASMO_PUBLIC_CLERK_PUBLISHABLE_KEY | ||
if (!publishableKey) { | ||
throw new Error('Please add the PLASMO_PUBLIC_CLERK_PUBLISHABLE_KEY to the .env.development file') | ||
} | ||
|
||
async function getToken() { | ||
const clerk = await createClerkClient({ | ||
publishableKey, | ||
syncHost: process.env.PLASMO_PUBLIC_CLERK_SYNC_HOST | ||
}); | ||
return await clerk.session?.getToken(); | ||
} | ||
|
||
// create a listener to listen for messages from content scripts | ||
// NOTE: A runtime listener cannot be async. | ||
// It must return true, in order to keep the connection open and send a response later. | ||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { | ||
console.log('Handling request for the user\s current token') | ||
getToken().then((token) => sendResponse({ token })); | ||
return true; | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const SDKFeatures = () => { | ||
return ( | ||
<> | ||
<p>Background Worker</p> | ||
<button | ||
onClick={() => { | ||
chrome.tabs.create({ | ||
url: "./tabs/background-worker-demo.html" | ||
}) | ||
}}> | ||
open tab page | ||
</button> | ||
|
||
</> | ||
) | ||
} |
12 changes: 12 additions & 0 deletions
12
apps/chrome-extension/src/tabs/background-worker-demo.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Clerk Background Worker demo</title> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
|
||
<body></body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as React from 'react'; | ||
|
||
export default function NewTab() { | ||
const [token, setToken] = React.useState<string | null>(null); | ||
|
||
const getToken = async (e: React.MouseEvent<HTMLButtonElement>) => { | ||
e.preventDefault(); | ||
|
||
// send a message to the background service worker | ||
chrome.runtime.sendMessage({ greeting: 'get-token' }, response => { | ||
console.log('MESSAGE RESPONSE', JSON.stringify(response)); | ||
setToken(response.token); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<p>Clerk Background Worker demo</p> | ||
<div className="App"> | ||
<p>This new tab simluates a content page where you might want to access user information, or make a request to your backend server and include a user token in the request.</p> | ||
<p>Make sure that you are signed into the extension. You can have the popup closed.</p> | ||
<button | ||
type='button' | ||
onClick={getToken} | ||
className='button invert' | ||
> | ||
Get Token (Service Worker) | ||
</button> | ||
{token && <p>Token: {token}</p>} | ||
</div> | ||
|
||
</div> | ||
) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"./**/*.tsx" | ||
], | ||
"compilerOptions": { | ||
"jsx": "react-jsx", | ||
"paths": { | ||
"~*": [ | ||
"./src/*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Link, Outlet, useNavigate } from 'react-router-dom' | ||
import { ClerkProvider, SignedIn, SignedOut, UserButton } from '@clerk/clerk-react' | ||
|
||
const PUBLISHABLE_KEY = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY | ||
|
||
if (!PUBLISHABLE_KEY) { | ||
throw new Error('Missing Publishable Key') | ||
} | ||
|
||
export default function RootLayout() { | ||
const navigate = useNavigate() | ||
|
||
return ( | ||
<ClerkProvider | ||
routerPush={(to) => navigate(to)} | ||
routerReplace={(to) => navigate(to, { replace: true })} | ||
publishableKey={PUBLISHABLE_KEY} | ||
> | ||
<header className="header"> | ||
<div> | ||
<div> | ||
<p>Clerk + React + React Router App</p> | ||
</div> | ||
<SignedIn> | ||
<UserButton /> | ||
</SignedIn> | ||
<SignedOut> | ||
<Link to="/sign-in">Sign In</Link> | ||
</SignedOut> | ||
</div> | ||
</header> | ||
<main> | ||
<Outlet /> | ||
</main> | ||
</ClerkProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import App from './App.tsx'; | ||
import './index.css'; | ||
import { ClerkProvider } from '@clerk/clerk-react'; | ||
import React from 'react' | ||
import ReactDOM from 'react-dom/client' | ||
import './index.css' | ||
import { RouterProvider, createBrowserRouter } from 'react-router-dom' | ||
|
||
const PUBLISHABLE_KEY = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY; | ||
// Import the layouts | ||
import RootLayout from './layouts/root-layout' | ||
|
||
if (!PUBLISHABLE_KEY) { | ||
throw new Error('Missing Publishable Key'); | ||
} | ||
// Import the components | ||
import IndexPage from './routes' | ||
import SignInPage from './routes/sign-in' | ||
import SignUpPage from './routes/sign-up' | ||
|
||
const router = createBrowserRouter([ | ||
{ | ||
element: <RootLayout />, | ||
children: [ | ||
{ path: '/', element: <IndexPage /> }, | ||
{ path: '/sign-in/*', element: <SignInPage /> }, | ||
{ path: '/sign-up/*', element: <SignUpPage /> }, | ||
], | ||
}, | ||
]) | ||
|
||
ReactDOM.createRoot(document.getElementById('root')!).render( | ||
<React.StrictMode> | ||
<ClerkProvider publishableKey={PUBLISHABLE_KEY} afterSignOutUrl="/"> | ||
<App /> | ||
</ClerkProvider> | ||
</React.StrictMode> | ||
); | ||
<RouterProvider router={router} /> | ||
</React.StrictMode>, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Link } from 'react-router-dom' | ||
|
||
export default function IndexPage() { | ||
return ( | ||
<div> | ||
<h1>This is the index page</h1> | ||
<div> | ||
<ul> | ||
<li> | ||
<Link to="/sign-up">Sign Up</Link> | ||
</li> | ||
<li> | ||
<Link to="/sign-in">Sign In</Link> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { SignIn } from '@clerk/clerk-react' | ||
|
||
export default function SignInPage() { | ||
return <SignIn path="/sign-in" /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { SignUp } from '@clerk/clerk-react' | ||
|
||
export default function SignUpPage() { | ||
return <SignUp path="/sign-up" /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.