Skip to content
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

fix: remix oauth #354

Merged
merged 3 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-boats-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-remix': patch
---

fix: export types.
51 changes: 32 additions & 19 deletions examples/remix/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ActionFunction,
json,
redirect,
LoaderFunction,
MetaFunction
} from '@remix-run/node';
Expand Down Expand Up @@ -100,6 +101,28 @@ export const action: ActionFunction = async ({
);
}

// GitHub OAuth
if (_action === 'github') {
const { error, data } = await supabaseClient.auth.signInWithOAuth({
provider: 'github',
options: { scopes: 'repo', redirectTo: 'http://localhost:3004' }
});

// in order for the set-cookie header to be set,
// headers must be returned as part of the loader response
if (error)
return json(
{ error },
{
headers: response.headers
}
);

return redirect(data.url, {
headers: response.headers
});
}

if (_action === 'logout') {
let { error } = await supabaseClient.auth.signOut();

Expand Down Expand Up @@ -130,20 +153,14 @@ export default function App() {
<Form method="post">
<div>
<label htmlFor="registerEmail">Email:</label>
<input
type="text"
id="registerEmail"
name="registerEmail"
defaultValue="jon@supabase.com"
/>
<input type="text" id="registerEmail" name="registerEmail" />
</div>
<div>
<label htmlFor="registerPassword">Password:</label>
<input
type="password"
id="registerPassword"
name="registerPassword"
defaultValue="very-secur3-password"
/>
</div>
<button type="submit" name="_action" value="register">
Expand All @@ -154,27 +171,23 @@ export default function App() {
<Form method="post">
<div>
<label htmlFor="loginEmail">Email:</label>
<input
type="text"
id="loginEmail"
name="loginEmail"
defaultValue="jon@supabase.com"
/>
<input type="text" id="loginEmail" name="loginEmail" />
</div>
<div>
<label htmlFor="loginPassword">Password:</label>
<input
type="password"
id="loginPassword"
name="loginPassword"
defaultValue="very-secur3-password"
/>
<input type="password" id="loginPassword" name="loginPassword" />
</div>
<button type="submit" name="_action" value="login">
Login
</button>
</Form>
<hr />
<Form method="post">
<button type="submit" name="_action" value="github">
GitHub Oauth
</button>
</Form>
<hr />
<Form method="post">
<button type="submit" name="_action" value="logout">
Logout
Expand Down
69 changes: 69 additions & 0 deletions examples/remix/app/routes/github-provider-token.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { json, LoaderFunction, redirect } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import { createServerClient, User } from '@supabase/auth-helpers-remix';
import { Database } from '../../db_types';

export const loader: LoaderFunction = async ({
request
}: {
request: Request;
}) => {
const response = new Response();

const supabaseClient = createServerClient<Database>(
process.env.SUPABASE_URL!,
process.env.SUPABASE_ANON_KEY!,
{ request, response }
);

const {
data: { session }
} = await supabaseClient.auth.getSession();

if (!session) {
// there is no session, therefore, we are redirecting
// to the landing page. we still need to return
// response.headers to attach the set-cookie header
return redirect('/', {
headers: response.headers
});
}

// Retrieve provider_token & logged in user's third-party id from metadata
const { provider_token, user } = session;
const userId = user.user_metadata.user_name;

const allRepos = await (
await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
method: 'GET',
headers: {
Authorization: `token ${provider_token}`
}
})
).json();

// in order for the set-cookie header to be set,
// headers must be returned as part of the loader response
return json(
{ user, allRepos },
{
headers: response.headers
}
);
};

export default function ProtectedPage() {
// by fetching the user in the loader, we ensure it is available
// for first SSR render - no flashing of incorrect state
const { user, allRepos } = useLoaderData<{ user: User; allRepos: any }>();

return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
<div>Protected content for {user.email}</div>
<p>Data fetched with provider token:</p>
<pre>{JSON.stringify(allRepos, null, 2)}</pre>
<p>user:</p>
<pre>{JSON.stringify(user, null, 2)}</pre>
</div>
);
}
46 changes: 14 additions & 32 deletions examples/remix/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
import { json, LoaderFunction } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import { createServerClient } from '@supabase/auth-helpers-remix';
import { useEffect, useState } from 'react';
import { createBrowserClient, Session } from '@supabase/auth-helpers-remix';
import { Database } from '../../db_types';

// this route demonstrates a simple server-side authenticated supabase query
export const loader: LoaderFunction = async ({
request
}: {
request: Request;
}) => {
const response = new Response();
const supabaseClient = createServerClient<Database>(
process.env.SUPABASE_URL!,
process.env.SUPABASE_ANON_KEY!,
{ request, response }
);
const { data, error } = await supabaseClient.from('test').select('*');
export default function Index() {
const [session, setSession] = useState<Session | null>(null);

if (error) {
throw error;
}
useEffect(() => {
const supabaseClient = createBrowserClient<Database>(
window.env.SUPABASE_URL,
window.env.SUPABASE_ANON_KEY
);

// in order for the set-cookie header to be set,
// headers must be returned as part of the loader response
return json(
{ data },
{
headers: response.headers
}
);
};

export default function Index() {
const { data } = useLoaderData();
supabaseClient.auth
.getSession()
.then(({ data: { session } }) => setSession(session));
}, []);

return (
<div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
<pre>{JSON.stringify(data, null, 2)}</pre>
<pre>{JSON.stringify({ session }, null, 2)}</pre>
</div>
);
}
4 changes: 2 additions & 2 deletions examples/remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"sideEffects": false,
"scripts": {
"build:example": "remix build",
"dev": "remix dev",
"start": "remix-serve build"
"dev": "PORT=3004 remix dev",
"start": "PORT=3004 remix-serve build"
},
"dependencies": {
"@remix-run/node": "^1.7.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
},
"dependencies": {
"@supabase/auth-helpers-shared": "workspace:*"
},
"peerDependencies": {
"@supabase/supabase-js": "^2.0.4"
}
}
2 changes: 1 addition & 1 deletion packages/remix/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const PKG_NAME = "@supabase/auth-helpers-remix";
export const PKG_VERSION = "0.1.0";
export const PKG_VERSION = "0.1.1";
3 changes: 3 additions & 0 deletions packages/remix/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export {
createServerClient
} from './utils/createSupabaseClient';
export { default as logger } from './utils/log';

// Types
export type { Session, User, SupabaseClient } from '@supabase/supabase-js';