Last active
January 25, 2022 15:38
-
-
Save p6l-richard/4e9ae57cd1647c274a2f3ab309a3911e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
chrome.runtime.onInstalled.addListener(() => { | |
await syncUser() | |
const {tokens, isSignedIn} = chrome.storage.sync.get('authentication') | |
// initiate signup flow on my web app if user isn't signed in yet | |
if (!isSignedIn && !tokens) { | |
chrome.tabs.create({url : "proprietary.web.app/signup"}) | |
} | |
}) | |
chrome.runtime.onMessage.addListener((message) => { | |
// Gmail page oened, sync `user` | |
if (message === 'open-extension') { | |
await syncUser(); | |
} | |
}) | |
chrome.runtime.onMessage.addListener(async (message, data) => { | |
if (message === 'create-event') { | |
const { tokens } = data | |
// backend sets httpOnly cookie with token | |
const res = await fetch('proprietary.web.app/api/token', { headers: {Authorization: `Bearer ${tokens.refreshToken}`} }) | |
// use token to make actual request | |
const data = fetch('proprietary.web.app/api/events', {method: "POST", headers: { Authorization: `Bearer ${token}`}}) | |
// invalidate storage | |
chrome.storage.sync.set({ events: data }) | |
return data | |
} | |
}) | |
const syncUser = async () => { | |
// see if we can find a cookie that has been set in our backend | |
const { tokens, isSignedIn } = chrome.cookies.get( | |
{name: "authentication", url: "proprietary.web.app"} | |
) | |
if (isSignedIn && tokens) { | |
// the content page will refer to `storage` to obtain authentication status | |
await chrome.storage.sync.set({authentication: {tokens, isSignedIn}}) | |
console.log('user is signed in'); | |
}); | |
else { | |
await chrome.storage.sync.set({authentication: {isSignedIn: false}}) | |
console.log('user has to sign in') | |
} | |
} |
This file contains hidden or 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
// button shows only if isSignedIn === true | |
// const {tokens, isSignedIn} = chrome.storage.sync.get('authentication') | |
function Button() { | |
return ( | |
<button onClick={() => { | |
const res = chrome.runtime.sendMessage('create-event') | |
initializeSomething(res); | |
}); | |
}}>Create event</button> | |
) | |
} |
This file contains hidden or 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
function SignupPage() { | |
return ( | |
<div> | |
<button onClick={() => fetch('proprietary.web.app/signup', {method: 'POST'})}>Sign in with Google</button> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment