-
Notifications
You must be signed in to change notification settings - Fork 1
Authentication #22
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
Merged
Merged
Authentication #22
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| # Need access to the database? Message us in the #phlask-data channel on Slack. | ||
| VITE_DB_URL=https://wantycfbnzzocsbthqzs.supabase.co | ||
| VITE_DB_API_KEY="Enter the API key provided by the team here" | ||
| SUPABASE_URL=https://wantycfbnzzocsbthqzs.supabase.co | ||
| SUPABASE_PUBLISHABLE_KEY= |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { | ||
| createServerClient, | ||
| type GetAllCookies, | ||
| parseCookieHeader, | ||
| type SetAllCookies, | ||
| serializeCookieHeader, | ||
| } from "@supabase/ssr"; | ||
| import { data } from "react-router"; | ||
| import { databaseApiKey, databaseUrl } from "~/constants/db.server"; | ||
|
|
||
| export const getDatabaseClient = (request: Request) => { | ||
| const headers = new Headers(); | ||
|
|
||
| if (!databaseUrl || !databaseApiKey) { | ||
| const message = import.meta.env.DEV | ||
| ? "Database credentials are missing! Make sure that `SUPABASE_URL` and `SUPABASE_PUBLISHABLE_KEY` are defined in a `.env` file" | ||
| : "An unexpected error have happened. Please try again later."; | ||
| throw data(new Error(message), { status: 500 }); | ||
| } | ||
|
|
||
| const getAll: GetAllCookies = async () => { | ||
| return parseCookieHeader(request.headers.get("Cookie") ?? "").map( | ||
| (cookie) => ({ name: cookie.name, value: cookie.value ?? "" }), | ||
| ); | ||
| }; | ||
|
|
||
| const setAll: SetAllCookies = (cookiesToSet) => { | ||
| cookiesToSet.forEach(({ name, value }) => { | ||
| headers.append("Set-Cookie", serializeCookieHeader(name, value, {})); | ||
| }); | ||
| }; | ||
|
|
||
| const supabase = createServerClient(databaseUrl, databaseApiKey, { | ||
| cookies: { getAll, setAll }, | ||
| }); | ||
|
|
||
| return { client: supabase, headers }; | ||
| }; |
This file was deleted.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const databaseUrl = process.env.SUPABASE_URL; | ||
| export const databaseApiKey = process.env.SUPABASE_PUBLISHABLE_KEY; |
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import type { User } from "@supabase/supabase-js"; | ||
| import { createContext } from "react-router"; | ||
|
|
||
| export const userContext = createContext<User | null>(null); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { type MiddlewareFunction, redirect } from "react-router"; | ||
| import { getDatabaseClient } from "~/api/client.server"; | ||
| import { userContext } from "~/context/user"; | ||
|
|
||
| export const authMiddleware: MiddlewareFunction = async ( | ||
| { request, context }, | ||
| next, | ||
| ) => { | ||
| const { client } = getDatabaseClient(request); | ||
|
|
||
| const response = await client.auth.getUser(); | ||
| if (response.error) { | ||
| return redirect("/auth"); | ||
| } | ||
|
|
||
| context.set(userContext, response.data.user); | ||
|
|
||
| return next(); | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| import { index, layout, type RouteConfig } from "@react-router/dev/routes"; | ||
| import { | ||
| index, | ||
| layout, | ||
| type RouteConfig, | ||
| route, | ||
| } from "@react-router/dev/routes"; | ||
|
|
||
| export default [ | ||
| layout("routes/_layout.tsx", [index("routes/dashboard.tsx")]), | ||
| layout("routes/authenticated/_layout.tsx", [ | ||
| index("routes/authenticated/dashboard.tsx"), | ||
| route("logout", "routes/authenticated/logout.tsx"), | ||
| ]), | ||
| route("auth", "routes/unauthenticated/_layout.tsx", [ | ||
| index("routes/unauthenticated/login.tsx"), | ||
| ]), | ||
| ] satisfies RouteConfig; |
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.
The only change here is injecting the client here, so that we can use it on the server