-
Notifications
You must be signed in to change notification settings - Fork 794
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
Remix example #443
Closed
Closed
Remix example #443
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
c7804c6
feat(remix): add initial files for example
IgnisDa e5384fb
ci(remix): add deployment configs
IgnisDa 936d88b
feat(remix): add basic login example
IgnisDa ee4dc4b
feat(remix-eg): add redirect after auth
IgnisDa 931a872
feat(remix-example): add basic todo page
IgnisDa a26de9e
docs(remix-eg): add comment explaining func
IgnisDa 2fb22cb
feat(remix-eg): allow deleting and updating todos
IgnisDa e3c9d2a
refactor(remix-eg): use remix-utils methods
IgnisDa 7d82abc
style(remix-eg): remove useless braces
IgnisDa fab4452
fix(remix-eg): set correct key for todo list
IgnisDa 64d7ddd
ci: run built app in docker container
IgnisDa d162580
fix(example/remix): use correct env vars
IgnisDa 0bbe644
Merge branch 'main' of https://github.com/teamhanko/hanko into remix
IgnisDa e89ae54
Merge branch 'main' into remix
559c595
build: update remix versions
IgnisDa 413e7b9
style(remix-example): fix eslint error
IgnisDa 73cea86
Merge branch 'main' into remix
IgnisDa 6e4d8bb
build(remix-example): update elements version
IgnisDa 8ba200b
Merge branch 'main' into remix
like-a-bause 7c1561f
Merge branch 'main' into remix
IgnisDa d086400
docs(examples): add correct instructions for running server.
IgnisDa b552ce5
Workaround for facebook/react/issues/24430
cayblood b54f399
Merge branch 'main' into remix
IgnisDa aae5471
Workaround for React 18 hydration issues
cayblood c3efbbd
Merge branch 'IgnisDa:remix' into remix
cayblood 279de09
build: revert packages to a version that works
IgnisDa f4e7054
build(examples/remix): revert frontend sdk to latest version
IgnisDa dd26cae
Merge branch 'main' into remix
IgnisDa File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"cSpell.words": [ | ||
"hanko", | ||
"teamhanko", | ||
"Todos" | ||
] | ||
} |
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 @@ | ||
services: | ||
todo-backend: | ||
build: ../../examples/express | ||
environment: | ||
- HANKO_API_URL=http://hanko:8000 | ||
networks: | ||
- intranet | ||
|
||
todo-frontend: | ||
build: ../../examples/remix | ||
environment: | ||
- PORT=8888 | ||
- REMIX_APP_HANKO_API=http://localhost:8000 | ||
- REMIX_APP_TODO_API=http://todo-backend:8002 | ||
ports: | ||
- "8888:8888" | ||
- "8008:8008" | ||
networks: | ||
- intranet |
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,2 @@ | ||
REMIX_APP_HANKO_API=http://localhost:8000 | ||
REMIX_APP_TODO_API=http://localhost:8002 |
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,4 @@ | ||
/** @type {import('eslint').Linter.Config} */ | ||
module.exports = { | ||
extends: ["@remix-run/eslint-config", "@remix-run/eslint-config/node"], | ||
}; |
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 @@ | ||
node_modules | ||
|
||
/.cache | ||
/build | ||
/public/build |
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,21 @@ | ||
# pull official base image | ||
FROM node:16-alpine | ||
|
||
# set working directory | ||
WORKDIR /app | ||
|
||
# add `/app/node_modules/.bin` to $PATH | ||
ENV PATH /app/node_modules/.bin:$PATH | ||
|
||
# install app dependencies | ||
COPY package.json ./ | ||
COPY package-lock.json ./ | ||
RUN npm install | ||
|
||
# add app | ||
COPY . ./ | ||
|
||
RUN npm run build | ||
|
||
# start app | ||
CMD ["npm", "run", "start"] |
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 @@ | ||
# Hanko Remix example | ||
|
||
This is a [Remix](https://remix.run) project bootstrapped with [Create Remix | ||
App](https://www.npmjs.com/package/create-remix). | ||
|
||
## Starting the app | ||
|
||
### Prerequisites | ||
|
||
- a running Hanko API (see the instructions on how to run the API [in | ||
Docker](../backend/README.md#Docker) or [from Source](../backend/README.md#from-source)) | ||
- a running express backend (see the [README](../express) for the express backend) | ||
|
||
### Set up environment variables | ||
|
||
In the `.env` file set up the correct environment variables: | ||
|
||
- `REMIX_APP_HANKO_API`: this is the URL of the Hanko API (default: | ||
`http://localhost:8000`, can be customized using the `server.public.address` option in | ||
the [configuration file](../../backend/docs/Config.md)) | ||
- `REMIX_APP_TODO_API`: this is the URL of the [express](../express) backend (default: | ||
`http://localhost:8002`) | ||
|
||
### Run development server | ||
|
||
The entire process is automated with docker. You can find the instructions in the | ||
[examples/README.md](../README.md#remix) file. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,22 @@ | ||
import { RemixBrowser } from "@remix-run/react"; | ||
import { startTransition, StrictMode } from "react"; | ||
import { hydrateRoot } from "react-dom/client"; | ||
|
||
function hydrate() { | ||
startTransition(() => { | ||
hydrateRoot( | ||
document.getElementById('root')!, | ||
<StrictMode> | ||
<RemixBrowser /> | ||
</StrictMode> | ||
); | ||
}); | ||
} | ||
|
||
if (window.requestIdleCallback) { | ||
window.requestIdleCallback(hydrate); | ||
} else { | ||
// Safari doesn't support requestIdleCallback | ||
// https://caniuse.com/requestidlecallback | ||
window.setTimeout(hydrate, 1); | ||
} |
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,118 @@ | ||
import { PassThrough } from "stream"; | ||
import type { EntryContext } from "@remix-run/node"; | ||
import { Response } from "@remix-run/node"; | ||
import { RemixServer } from "@remix-run/react"; | ||
import isbot from "isbot"; | ||
import { renderToPipeableStream } from "react-dom/server"; | ||
import { renderHeadToString } from 'remix-island'; | ||
import { Head } from './root'; | ||
|
||
const ABORT_DELAY = 5000; | ||
|
||
export default function handleRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext | ||
) { | ||
return isbot(request.headers.get("user-agent")) | ||
? handleBotRequest( | ||
request, | ||
responseStatusCode, | ||
responseHeaders, | ||
remixContext | ||
) | ||
: handleBrowserRequest( | ||
request, | ||
responseStatusCode, | ||
responseHeaders, | ||
remixContext | ||
); | ||
} | ||
|
||
function handleBotRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext | ||
) { | ||
return new Promise((resolve, reject) => { | ||
let didError = false; | ||
|
||
const { pipe, abort } = renderToPipeableStream( | ||
<RemixServer context={remixContext} url={request.url} />, | ||
{ | ||
onAllReady() { | ||
const head = renderHeadToString({request, remixContext, Head}); | ||
const body = new PassThrough(); | ||
|
||
responseHeaders.set("Content-Type", "text/html"); | ||
|
||
resolve( | ||
new Response(body, { | ||
headers: responseHeaders, | ||
status: didError ? 500 : responseStatusCode, | ||
}) | ||
); | ||
body.write(`<!DOCTYPE html><html><head>${head}</head><body><div id="root">`); | ||
pipe(body); | ||
body.write(`</div></body></html>`); | ||
}, | ||
onShellError(error: unknown) { | ||
reject(error); | ||
}, | ||
onError(error: unknown) { | ||
didError = true; | ||
|
||
console.error(error); | ||
}, | ||
} | ||
); | ||
|
||
setTimeout(abort, ABORT_DELAY); | ||
}); | ||
} | ||
|
||
function handleBrowserRequest( | ||
request: Request, | ||
responseStatusCode: number, | ||
responseHeaders: Headers, | ||
remixContext: EntryContext | ||
) { | ||
return new Promise((resolve, reject) => { | ||
let didError = false; | ||
|
||
const { pipe, abort } = renderToPipeableStream( | ||
<RemixServer context={remixContext} url={request.url} />, | ||
{ | ||
onShellReady() { | ||
const head = renderHeadToString({ request, remixContext, Head }); | ||
const body = new PassThrough(); | ||
|
||
responseHeaders.set("Content-Type", "text/html"); | ||
|
||
resolve( | ||
new Response(body, { | ||
headers: responseHeaders, | ||
status: didError ? 500 : responseStatusCode, | ||
}) | ||
); | ||
|
||
body.write(`<!DOCTYPE html><html><head>${head}</head><body><div id="root">`); | ||
pipe(body); | ||
body.write(`</div></body></html>`); | ||
}, | ||
onShellError(err: unknown) { | ||
reject(err); | ||
}, | ||
onError(error: unknown) { | ||
didError = true; | ||
|
||
console.error(error); | ||
}, | ||
} | ||
); | ||
|
||
setTimeout(abort, ABORT_DELAY); | ||
}); | ||
} |
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,20 @@ | ||
import { redirect } from '@remix-run/server-runtime'; | ||
import { parse } from 'cookie'; | ||
import type { JwtPayload } from 'jsonwebtoken'; | ||
import { decode } from 'jsonwebtoken'; | ||
|
||
export const extractHankoCookie = (request: Request) => { | ||
const cookies = parse(request.headers.get('Cookie') || ''); | ||
return cookies.hanko; | ||
}; | ||
|
||
// ensures the user has a hanko cookie but does not check if it is valid | ||
export async function requireValidJwt(request: Request) { | ||
const hankoCookie = extractHankoCookie(request); | ||
const decoded = decode(hankoCookie) as JwtPayload; | ||
const hankoId = decoded?.sub; | ||
const exp = (decoded?.exp || 0) * 1000; | ||
if (!hankoId || exp < Date.now()) | ||
throw redirect(`/`); | ||
return decoded | ||
} |
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,6 @@ | ||
// `.client.ts` files are only supposed to run on the client. I could not find this | ||
// documented on the remix docs but someone on the remix discord told me this is how it is | ||
// supposed to work. | ||
import { register } from '@teamhanko/hanko-elements'; | ||
|
||
export { register as registerHankoAuth }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this, the remix build command currently results in: "ERROR: Could not resolve "@teamhanko/hanko-elements"
Reverting to
@teamhanko/hanko-elements@0.0.17-alpha
requires the "old" import path for the register function, i.e.import { register } from '@teamhanko/hanko-elements/hanko-auth'
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@IgnisDa I just submitted a PR for this fix plus another compatibility issue I found with the ClientOnly component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@IgnisDa @lfleischmann replied below before my fix was merged into this PR. I think their concerns would go away and we could finally get this merged with that fix. That said, we should probably update to 0.5.0, now that it has been released.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added you as a collaborator in my repository in case you need to make any changes.