Chrome extension integration guide #8045
Replies: 3 comments 3 replies
-
SignoutAs a follow up to the discussion I'll try to explain how to make In the Chrome ExtensionI copied what next-auth does here import api from '@base/lib/api'
async function getCsrfToken() {
return api.get({ path: 'auth/csrf' })
}
async function fetchLogout() {
const { csrfToken } = await getCsrfToken()
const callbackUrl = window.location.href
await api.post({
path: 'auth/signout',
headers: { contentType: 'urlencoded' },
// @ts-expect-error
data: new URLSearchParams({
csrfToken,
callbackUrl,
json: true,
})
})
window.location.reload()
} My ApiClientYou can use Axios or pure The This is how initialize the // `core` is where is the gist I posted
import { ApiClient } from 'core'
const api = new ApiClient({
clientHostOrigin: window.location.origin,
apiHost: env.VITE_API_HOST // This is the domain of your NextJS API Ex.: https://localhost:3001
})
export default api |
Beta Was this translation helpful? Give feedback.
-
Thanks @andresgutgon. This guide is very inspiring to me. It would be better if you could give a complete sample code. |
Beta Was this translation helpful? Give feedback.
-
I played with this in this repo. Is, as all my weekend projects, abandoned 😂 but I managed to do a chrome extension that log ins with Google. Is a mono repo with a NextJS app and a Vite app for the chrome extension. https://github.com/andresgutgon/aibooknotes Enjoy. Don't ask me much I barely remember what I did 😅 |
Beta Was this translation helpful? Give feedback.
-
What?
Hi, Andres here 👋 I've being using this lib only a few weeks for a side project but I think it's awesome. Coming from Rails world where we have Devise this looks super similar in the sense that is a very well though working solution but for nextjs.
My use case in my current side project is to make a nextjs backend with an API that will be consumed from a Chrome extension in a content script. Chrome extensions have different ways of showing content to user and doing actions.
tab
,background
orcontent_script
. I used content script because it allows me to inject a UI in the web page so the user can interact with my API.As you can imagine interacting with a nextjs auth protected api requires users being logged in. And I want my users being logged in from the site where the Chrome extension is used. I haven't seen a lot of documentation on internet about this topic so I think it would be great to have a guide about it in this project so people with a similar use case to mine have a nice reference.
There's already some issue open about this
So what are the problems with Chrome extensions?
Chrome extension V3 (2023) are more extrict when it comes to security. Before it was possible to inject thrird party JS like the Google client Auth and open a login screen. But now that's not an option. Also anyway I want to being able to make calls to my API from the Chrome extensions with the user authenticated with a cookie session.
I wrote a document for myself to remember what I did. I'll post here so you can review and tell me if you think is the best approach.
Why we need SSL in development?
So Chrome extensions content scripts require your server two security measures in
order to be able to make request to your server from other web pages from your
content script
About
sameSite=None
So secure means your server needs to use
https
. And here we're setting SSL onour local development nextjs app to write the next-auth session cookie as
sameSite=None
and be able to read that cookie from the Chrome extension.CORS
I followed NextJS CORS docs and this video
My
middleware.ts
looks like this:And
@/lib/api/trustedDomains
looks like this:From chrome extension fetch
You have to send
Origin
header from Chrome extension to tell the server the user is calling it from authorized domain. A basic fetch api call:sameSite=None
This is needed to be able to do cross-site request. I have this in my nextjs auth config:
How to setup SSL in development?
The answer is using mkcert + local-ssl-proxy
mkcert
: This is tool for generating a local SSL certificate and tell yourmachine to trust it. It makes things super easy. If something is not clear after
reading this README read the readme on their repo.
local-ssl-proxy
: I could setup a traefik/nginx in a docker compose as reverseproxy for the nextjs app but I want to be able to debug and I feel that would
complicate debugging node server. Specially from the editor.
SSL in Development with mkcert
This is here to explain how to enable SSL while developing in your machine.
For generating the SSL certificate locally use mkcert (It can be installed with Homebrew in Mac OSX).
Run this:
package.json
You have to open 2 terminals. NextJS non secure
http://localhost:3000
and SSL secure proxyhttps://localhost:3001
You have to tell NextJS auth now your domain is the secure. Change your
.env
file like this:Now you have to start your NextJS dev server normally:
And on other terminal the https proxy:
All my SSL stuff is under a
certificates
folder like this:So we're telling NextJS auth that our app domain is the domain of the SSL local
proxy. Not the real NextJS domain
http://localhost:3000
. You can see thisconfig in
./ssl/config.json
I think that's all 🎉. Now I see from the page where my Chrome extension is used the cookie for session
![image](https://private-user-images.githubusercontent.com/49499/253734618-860db35d-20b9-4b07-b615-42d5a01d28db.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NTgxODgsIm5iZiI6MTczODg1Nzg4OCwicGF0aCI6Ii80OTQ5OS8yNTM3MzQ2MTgtODYwZGIzNWQtMjBiOS00YjA3LWI2MTUtNDJkNWEwMWQyOGRiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA2VDE2MDQ0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE2MGQzMzIzNmFhNTQyNmMyZDE2MDkwYTExNzEyMDA4MTIzMmQ3NjBlYmVmNzAxYjQyMjA1NzRiZTE5NGY3ZDMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.2IPVvv4zohEMpG8Y3Cz6WBWuThgoAFSFfOneZG_ZZHs)
Now how the other cookies I didn't set to
None
are not accesible (are in yellow)Note about security
NextAuth docs
recommends not to override the cookie configuration. But I don't see other way
of achiving what I want. I don't know how this config can be insecure if the
goal of my app is to allow cross-site sessions between domains. Specially having
cors correctly configured and only allowing some specific sites to read cookies
session.
I would change this warning for a more informative note on how to securely
configure a site for use
sameSite=None
. Even I would add an option to passthis setting and do not need to override all the cookie.
Articles
About cookies and host permissions
Chrome extensions documentation is not the best. This article make the topics of
host permissions and cookies a bit clear for me:
https://www.gmass.co/blog/send-cookie-cross-origin-xmlhttprequest-chrome-extension/
that host. Maybe don't needed for the API because we're going to do this via
AJAX. No permission needed to update my own server lol.
manifest.json
is not for passing cookies via AJAX.Is for interacting with the cookies of a site via Chrome's API
chrome.cookies
.Beta Was this translation helpful? Give feedback.
All reactions