Skip to content

Commit

Permalink
Update README and fix prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
zaiste committed Jun 7, 2023
1 parent c6d04d6 commit 8e13b71
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pnpm-lock.yaml
coverage/**
dist/
66 changes: 39 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,56 +141,68 @@ const response = await signIn({
Setup `_app.tsx` as described above. In your login component trigger the external auth flow using the following code:

```tsx
import { Inter } from 'next/font/google'

import { useSaleorAuthContext, useSaleorExternalAuth } from '@saleor/auth-sdk/react'
import { ExternalProvider } from '@saleor/auth-sdk';
import Link from 'next/link';
import { gql, useQuery } from '@apollo/client';

const inter = Inter({ subsets: ['latin'] })
import { useSaleorAuthContext, useSaleorExternalAuth } from "@saleor/auth-sdk/react";
import { ExternalProvider } from "@saleor/auth-sdk";
import Link from "next/link";
import { gql, useQuery } from "@apollo/client";

export default function Home() {
const { loading: isLoading, error, data } = useQuery(gql`query CurrentUser { me { id email firstName lastName } }`);

const { authURL, loading } = useSaleorExternalAuth({
saleorURL: '<your Saleor instance>',
const {
loading: isLoadingCurrentUser,
error,
data,
} = useQuery(
gql`
query CurrentUser {
me {
id
email
firstName
lastName
}
}
`,
);
const { authURL, loading: isLoadingExternalAuth } = useSaleorExternalAuth({
saleorURL: "<your Saleor instance>",
provider: ExternalProvider.OpenIDConnect,
redirectURL: 'http://localhost:5375/api/auth/callback',
redirectURL: "<your Next.js app>/api/auth/callback",
});

const { signOut } = useSaleorAuthContext();

if (loading || isLoading) {
if (isLoadingExternalAuth || isLoadingCurrentUser) {
return <div>Loading...</div>;
} else if (data && data.me) {
}
if (data?.me) {
return (
<div>
{JSON.stringify(data)}
<button onClick={() => signOut()}>Logout</button>
</div>
)
} else if (authURL) {
return (
);
}
if (authURL) {
return (
<div>
<Link href={authURL}>Login</Link>
<Link href={authURL}>Login</Link>
</div>
)
} else {
return (
<div>Something went wrong</div>
)
);
}
return <div>Something went wrong</div>;
}
```

You also need to define the auth callback. In `pages/api/auth` create the `callback.ts` with the following content:

```ts
import { ExternalProvider, SaleorExternalAuth } from '@saleor/auth-sdk';
import { createSaleorExternalAuthHandler } from '@saleor/auth-sdk/next'
import { ExternalProvider, SaleorExternalAuth } from "@saleor/auth-sdk";
import { createSaleorExternalAuthHandler } from "@saleor/auth-sdk/next";

const externalAuth = new SaleorExternalAuth('<your Saleor instance URL>', ExternalProvider.OpenIDConnect)
const externalAuth = new SaleorExternalAuth(
"<your Saleor instance URL>",
ExternalProvider.OpenIDConnect,
);

export default createSaleorExternalAuthHandler(externalAuth);
```
Expand Down
2 changes: 1 addition & 1 deletion src/SaleorExternalAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type ExternalObtainAccessTokenResponse =
| { errors: any[] };

export class SaleorExternalAuth {
constructor(private saleorURL: string, private provider: ExternalProvider) { }
constructor(private saleorURL: string, private provider: ExternalProvider) {}

async makePOSTRequest(query: ReturnType<typeof gql>, variables: object) {
const response = await fetch(this.saleorURL, getRequestData(query, variables));
Expand Down

0 comments on commit 8e13b71

Please sign in to comment.