ss-saleor-auth-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -0
- package/.eslintrc.cjs +20 -0
- package/.github/workflows/main.yml +32 -0
- package/.nvmrc +1 -0
- package/.prettierignore +3 -0
- package/.prettierrc +5 -0
- package/README.md +411 -0
- package/justfile +9 -0
- package/package.json +124 -0
- package/setup-tests.ts +10 -0
- package/src/SaleorAccessTokenStorageHandler.ts +26 -0
- package/src/SaleorAuthClient.ts +292 -0
- package/src/SaleorExternalAuth.ts +78 -0
- package/src/SaleorRefreshTokenStorageHandler.ts +71 -0
- package/src/graphql.ts +20 -0
- package/src/index.ts +3 -0
- package/src/mutations.ts +104 -0
- package/src/next/handler.ts +14 -0
- package/src/next/index.ts +1 -0
- package/src/next/server.ts +45 -0
- package/src/react/SaleorAuthProvider.tsx +7 -0
- package/src/react/context.ts +20 -0
- package/src/react/index.ts +4 -0
- package/src/react/useAuthChange.ts +41 -0
- package/src/react/useSaleorExternalAuth.ts +45 -0
- package/src/types.ts +98 -0
- package/src/utils.ts +71 -0
- package/test/SaleorAuthClient.test.ts +174 -0
- package/test/SaleorExternalAuth.test.ts +43 -0
- package/tsconfig.json +40 -0
- package/vitest.config.ts +26 -0
package/.eslintignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.eslintrc.cjs
|
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-env node */
|
|
2
|
+
module.exports = {
|
|
3
|
+
extends: [
|
|
4
|
+
"eslint:recommended",
|
|
5
|
+
"plugin:@typescript-eslint/recommended-type-checked",
|
|
6
|
+
"plugin:@typescript-eslint/stylistic-type-checked",
|
|
7
|
+
],
|
|
8
|
+
plugins: ["@typescript-eslint"],
|
|
9
|
+
parser: "@typescript-eslint/parser",
|
|
10
|
+
parserOptions: {
|
|
11
|
+
project: true,
|
|
12
|
+
tsconfigRootDir: __dirname,
|
|
13
|
+
},
|
|
14
|
+
root: true,
|
|
15
|
+
rules: {
|
|
16
|
+
"@typescript-eslint/prefer-nullish-coalescing": "off",
|
|
17
|
+
"@typescript-eslint/require-await": "off",
|
|
18
|
+
"no-empty-pattern": "off",
|
|
19
|
+
},
|
|
20
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: Build, TypeScripts, tests
|
|
2
|
+
on:
|
|
3
|
+
pull_request:
|
|
4
|
+
merge_group:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
concurrency:
|
|
9
|
+
group: tests-${{ github.event.pull_request.number || github.ref }}
|
|
10
|
+
cancel-in-progress: true
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build_and_test:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- name: Install PNPM
|
|
19
|
+
uses: pnpm/action-setup@v2
|
|
20
|
+
with:
|
|
21
|
+
version: 8.10.0
|
|
22
|
+
|
|
23
|
+
- uses: actions/setup-node@v3
|
|
24
|
+
with:
|
|
25
|
+
node-version-file: package.json
|
|
26
|
+
cache: "pnpm"
|
|
27
|
+
|
|
28
|
+
- name: Install dependencies
|
|
29
|
+
run: pnpm --version && pnpm install --frozen-lockfile
|
|
30
|
+
|
|
31
|
+
- name: Run tests
|
|
32
|
+
run: pnpm test
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
18
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img width="150" alt="" src="https://github.com/saleor/auth-sdk/assets/1338731/c90a73d0-5ef1-4d09-9347-c5d02cd7244d">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
|
|
7
|
+
# Saleor Auth SDK
|
|
8
|
+
|
|
9
|
+
Saleor Auth SDK integrates secure and customizable authentication and authorization into storefronts using Saleor.
|
|
10
|
+
|
|
11
|
+
**Below 3kB bundle size (gzipped).**
|
|
12
|
+
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
<div align="center">
|
|
16
|
+
<a href="https://www.npmjs.com/package/@saleor/auth-sdk">npm</a>
|
|
17
|
+
<span> • </span>
|
|
18
|
+
<a href="https://docs.saleor.io/docs/3.x/api-usage/authentication">Docs</a>
|
|
19
|
+
<span> • </span>
|
|
20
|
+
<a href="https://twitter.com/getsaleor">Twitter</a>
|
|
21
|
+
<span> • </span>
|
|
22
|
+
<a href="https://discord.gg/H52JTZAtSH">Discord</a>
|
|
23
|
+
</div>
|
|
24
|
+
|
|
25
|
+
<br/>
|
|
26
|
+
|
|
27
|
+
<div align="center">
|
|
28
|
+
|
|
29
|
+
[](https://discord.gg/H52JTZAtSH)
|
|
30
|
+
|
|
31
|
+
</div>
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Next.js App Router
|
|
36
|
+
|
|
37
|
+
Next.js 13+ App Router is the recommended way to use the Saleor Auth SDK. It is the easiest to set up and provides the best user experience.
|
|
38
|
+
|
|
39
|
+
In order to use Saleor Auth SDK in React Server Components, the client needs to be created in the following way:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createSaleorAuthClient } from "@saleor/auth-sdk";
|
|
43
|
+
import { getNextServerCookiesStorage } from "@saleor/auth-sdk/next/server";
|
|
44
|
+
|
|
45
|
+
const nextServerCookiesStorage = getNextServerCookiesStorage();
|
|
46
|
+
const saleorAuthClient = createSaleorAuthClient({
|
|
47
|
+
saleorApiUrl: "…",
|
|
48
|
+
refreshTokenStorage: nextServerCookiesStorage,
|
|
49
|
+
accessTokenStorage: nextServerCookiesStorage,
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Logging in can be implemented via Server Actions:
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
<form
|
|
57
|
+
className="bg-white shadow-md rounded p-8"
|
|
58
|
+
action={async (formData) => {
|
|
59
|
+
"use server";
|
|
60
|
+
|
|
61
|
+
await saleorAuthClient.signIn(
|
|
62
|
+
{
|
|
63
|
+
email: formData.get("email").toString(),
|
|
64
|
+
password: formData.get("password").toString(),
|
|
65
|
+
},
|
|
66
|
+
{ cache: "no-store" },
|
|
67
|
+
);
|
|
68
|
+
}}
|
|
69
|
+
>
|
|
70
|
+
{/* … rest of the form … */}
|
|
71
|
+
</form>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Then, you can use `saleorAuthClient.fetchWithAuth` directly for any queries and mutations.
|
|
75
|
+
|
|
76
|
+
For a full working example see the [Saleor Auth SDK example](https://github.com/saleor/example-auth-sdk/blob/5babda35969c35f423680b47d1446466b18b2461/app/ssr/page.tsx).
|
|
77
|
+
|
|
78
|
+
### Next.js Pages Router with [Apollo Client](https://www.apollographql.com/docs/react/)
|
|
79
|
+
|
|
80
|
+
<details>
|
|
81
|
+
<summary>Step-by-step video tutorial</summary>
|
|
82
|
+
|
|
83
|
+
Check the following [step-by-step video](https://www.youtube.com/watch?v=XY1t8JiPwk0) guide on how to set this up.
|
|
84
|
+
[](https://www.youtube.com/watch?v=XY1t8JiPwk0)
|
|
85
|
+
|
|
86
|
+
</details>
|
|
87
|
+
|
|
88
|
+
When using Next.js (Pages Router) along with [Apollo Client](https://www.apollographql.com/docs/react/), there are two essential steps to setting up your application. First, you have to surround your application's root with two providers: `<SaleorAuthProvider>` and `<ApolloProvider>`.
|
|
89
|
+
|
|
90
|
+
`<SaleorAuthProvider>` comes from our React.js-auth package, located at `@saleor/auth-sdk/react`, and it needs to be set up with the Saleor auth client instance.
|
|
91
|
+
|
|
92
|
+
The `<ApolloProvider>` comes from `@apollo/client` and it needs the live GraphQL client instance, which is enhanced with the authenticated `fetch` that comes from the Saleor auth client.
|
|
93
|
+
|
|
94
|
+
Lastly, you must run the `useAuthChange` hook. This links the `onSignedOut` and `onSignedIn` events.
|
|
95
|
+
|
|
96
|
+
Let's look at an example:
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { AppProps } from "next/app";
|
|
100
|
+
import { ApolloProvider, ApolloClient, InMemoryCache, createHttpLink } from "@apollo/client";
|
|
101
|
+
import { createSaleorAuthClient } from "@saleor/auth-sdk";
|
|
102
|
+
import { SaleorAuthProvider, useAuthChange } from "@saleor/auth-sdk/react";
|
|
103
|
+
|
|
104
|
+
const saleorApiUrl = "<your Saleor API URL>";
|
|
105
|
+
|
|
106
|
+
// Saleor Client
|
|
107
|
+
const saleorAuthClient = createSaleorAuthClient({ saleorApiUrl });
|
|
108
|
+
|
|
109
|
+
// Apollo Client
|
|
110
|
+
const httpLink = createHttpLink({
|
|
111
|
+
uri: saleorApiUrl,
|
|
112
|
+
fetch: saleorAuthClient.fetchWithAuth,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
export const apolloClient = new ApolloClient({
|
|
116
|
+
link: httpLink,
|
|
117
|
+
cache: new InMemoryCache(),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
export default function App({ Component, pageProps }: AppProps) {
|
|
121
|
+
useAuthChange({
|
|
122
|
+
saleorApiUrl,
|
|
123
|
+
onSignedOut: () => apolloClient.resetStore(),
|
|
124
|
+
onSignedIn: () => {
|
|
125
|
+
apolloClient.refetchQueries({ include: "all" });
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<SaleorAuthProvider client={saleorAuthClient}>
|
|
131
|
+
<ApolloProvider client={apolloClient}>
|
|
132
|
+
<Component {...pageProps} />
|
|
133
|
+
</ApolloProvider>
|
|
134
|
+
</SaleorAuthProvider>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Then, in your register, login and logout forms you can use the auth methods (`signIn`, `signOut`, `isAuthenticating`) provided by the `useSaleorAuthContext()`. For example, `signIn` is usually triggered when submitting the login form credentials.
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
import React, { FormEvent } from "react";
|
|
143
|
+
import { useSaleorAuthContext } from "@saleor/auth-sdk/react";
|
|
144
|
+
import { gql, useQuery } from "@apollo/client";
|
|
145
|
+
|
|
146
|
+
const CurrentUserDocument = gql`
|
|
147
|
+
query CurrentUser {
|
|
148
|
+
me {
|
|
149
|
+
id
|
|
150
|
+
email
|
|
151
|
+
firstName
|
|
152
|
+
lastName
|
|
153
|
+
avatar {
|
|
154
|
+
url
|
|
155
|
+
alt
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
`;
|
|
160
|
+
|
|
161
|
+
export default function LoginPage() {
|
|
162
|
+
const { signIn, signOut } = useSaleorAuthContext();
|
|
163
|
+
|
|
164
|
+
const { data: currentUser, loading } = useQuery(CurrentUserDocument);
|
|
165
|
+
|
|
166
|
+
const submitHandler = async (event: FormEvent<HTMLFormElement>) => {
|
|
167
|
+
event.preventDefault();
|
|
168
|
+
|
|
169
|
+
const result = await signIn({
|
|
170
|
+
email: "admin@example.com",
|
|
171
|
+
password: "admin",
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
if (result.data.tokenCreate.errors) {
|
|
175
|
+
// handle errors
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
if (loading) {
|
|
180
|
+
return <div>Loading...</div>;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<main>
|
|
185
|
+
{currentUser?.me ? (
|
|
186
|
+
<>
|
|
187
|
+
<div>Display user {JSON.stringify(currentUser)}</div>
|
|
188
|
+
<button className="button" onClick={() => signOut()}>
|
|
189
|
+
Log Out
|
|
190
|
+
</button>
|
|
191
|
+
</>
|
|
192
|
+
) : (
|
|
193
|
+
<div>
|
|
194
|
+
<form onSubmit={submitHandler}>
|
|
195
|
+
{/* You must connect your inputs to state or use a form library such as react-hook-form */}
|
|
196
|
+
<input type="email" name="email" placeholder="Email" />
|
|
197
|
+
<input type="password" name="password" placeholder="Password" />
|
|
198
|
+
<button className="button" type="submit">
|
|
199
|
+
Log In
|
|
200
|
+
</button>
|
|
201
|
+
</form>
|
|
202
|
+
</div>
|
|
203
|
+
)}
|
|
204
|
+
</main>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Next.js (Pages Router) with [urql](https://formidable.com/open-source/urql/)
|
|
210
|
+
|
|
211
|
+
When using Next.js (Pages Router) along with [urql](https://formidable.com/open-source/urql/) client, there are two essential steps to setting up your application. First, you have to surround your application's root with two providers: `<SaleorAuthProvider>` and `<Provider>`.
|
|
212
|
+
|
|
213
|
+
`<SaleorAuthProvider>` comes from our React.js-auth package, located at `@saleor/auth-sdk/react`, and it needs to be set up with the Saleor auth client.
|
|
214
|
+
|
|
215
|
+
The `<Provider>` comes from `urql` and it needs the GraphQL client instance, which is enhanced with the authenticated `fetch` that comes from the Saleor auth client.
|
|
216
|
+
|
|
217
|
+
Lastly, you must run the `useAuthChange` hook. This links the `onSignedOut` and `onSignedIn` events and is meant to refresh the GraphQL store and in-flight active GraphQL queries.
|
|
218
|
+
|
|
219
|
+
Let's look at an example:
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
import { AppProps } from "next/app";
|
|
223
|
+
import { Provider, cacheExchange, fetchExchange, ssrExchange } from "urql";
|
|
224
|
+
import { SaleorAuthProvider, useAuthChange } from "@saleor/auth-sdk/react";
|
|
225
|
+
|
|
226
|
+
const saleorApiUrl = "<your Saleor API URL>";
|
|
227
|
+
|
|
228
|
+
const saleorAuthClient = createSaleorAuthClient({ saleorApiUrl });
|
|
229
|
+
|
|
230
|
+
const makeUrqlClient = () =>
|
|
231
|
+
createClient({
|
|
232
|
+
url: saleorApiUrl,
|
|
233
|
+
fetch: saleorAuthClient.fetchWithAuth,
|
|
234
|
+
exchanges: [cacheExchange, fetchExchange],
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
export default function App({ Component, pageProps }: AppProps) {
|
|
238
|
+
// https://github.com/urql-graphql/urql/issues/297#issuecomment-504782794
|
|
239
|
+
const [urqlClient, setUrqlClient] = useState<Client>(makeUrqlClient());
|
|
240
|
+
|
|
241
|
+
useAuthChange({
|
|
242
|
+
saleorApiUrl,
|
|
243
|
+
onSignedOut: () => setUrqlClient(makeUrqlClient()),
|
|
244
|
+
onSignedIn: () => setUrqlClient(makeUrqlClient()),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<SaleorAuthProvider client={saleorAuthClient}>
|
|
249
|
+
<Provider value={urqlClient}>
|
|
250
|
+
<Component {...pageProps} />
|
|
251
|
+
</Provider>
|
|
252
|
+
</SaleorAuthProvider>
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Then, in your register, login and logout forms you can use the auth methods (`signIn`, `signOut`) provided by the `useSaleorAuthContext()`. For example, `signIn` is usually triggered when submitting the login form credentials.
|
|
258
|
+
|
|
259
|
+
```tsx
|
|
260
|
+
import React, { FormEvent } from "react";
|
|
261
|
+
import { useSaleorAuthContext } from "@saleor/auth-sdk/react";
|
|
262
|
+
import { gql, useQuery } from "urql";
|
|
263
|
+
|
|
264
|
+
const CurrentUserDocument = gql`
|
|
265
|
+
query CurrentUser {
|
|
266
|
+
me {
|
|
267
|
+
id
|
|
268
|
+
email
|
|
269
|
+
firstName
|
|
270
|
+
lastName
|
|
271
|
+
avatar {
|
|
272
|
+
url
|
|
273
|
+
alt
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
`;
|
|
278
|
+
|
|
279
|
+
export default function LoginPage() {
|
|
280
|
+
const { signIn, signOut } = useSaleorAuthContext();
|
|
281
|
+
|
|
282
|
+
const [{ data: currentUser, fetching: loading }] = useQuery({
|
|
283
|
+
query: CurrentUserDocument,
|
|
284
|
+
pause: isAuthenticating,
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const submitHandler = async (event: FormEvent<HTMLFormElement>) => {
|
|
288
|
+
event.preventDefault();
|
|
289
|
+
|
|
290
|
+
const result = await signIn({
|
|
291
|
+
email: "admin@example.com",
|
|
292
|
+
password: "admin",
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
if (result.data.tokenCreate.errors) {
|
|
296
|
+
// handle errors
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
if (loading) {
|
|
301
|
+
return <div>Loading...</div>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return (
|
|
305
|
+
<main>
|
|
306
|
+
{currentUser?.me ? (
|
|
307
|
+
<>
|
|
308
|
+
<div>Display user {JSON.stringify(currentUser)}</div>
|
|
309
|
+
<button className="button" onClick={() => signOut()}>
|
|
310
|
+
Log Out
|
|
311
|
+
</button>
|
|
312
|
+
</>
|
|
313
|
+
) : (
|
|
314
|
+
<div>
|
|
315
|
+
<form onSubmit={submitHandler}>
|
|
316
|
+
{/* You must connect your inputs to state or use a form library such as react-hook-form */}
|
|
317
|
+
<input type="email" name="email" placeholder="Email" />
|
|
318
|
+
<input type="password" name="password" placeholder="Password" />
|
|
319
|
+
<button className="button" type="submit">
|
|
320
|
+
Log In
|
|
321
|
+
</button>
|
|
322
|
+
</form>
|
|
323
|
+
</div>
|
|
324
|
+
)}
|
|
325
|
+
</main>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### Next.js (Pages Router) with OpenID Connect
|
|
331
|
+
|
|
332
|
+
Setup `_app.tsx` as described above. In your login component trigger the external auth flow using the following code:
|
|
333
|
+
|
|
334
|
+
```tsx
|
|
335
|
+
import { useSaleorAuthContext, useSaleorExternalAuth } from "@saleor/auth-sdk/react";
|
|
336
|
+
import { ExternalProvider } from "@saleor/auth-sdk";
|
|
337
|
+
import Link from "next/link";
|
|
338
|
+
import { gql, useQuery } from "@apollo/client";
|
|
339
|
+
|
|
340
|
+
export default function Home() {
|
|
341
|
+
const {
|
|
342
|
+
loading: isLoadingCurrentUser,
|
|
343
|
+
error,
|
|
344
|
+
data,
|
|
345
|
+
} = useQuery(gql`
|
|
346
|
+
query CurrentUser {
|
|
347
|
+
me {
|
|
348
|
+
id
|
|
349
|
+
email
|
|
350
|
+
firstName
|
|
351
|
+
lastName
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
`);
|
|
355
|
+
const { authURL, loading: isLoadingExternalAuth } = useSaleorExternalAuth({
|
|
356
|
+
saleorApiUrl,
|
|
357
|
+
provider: ExternalProvider.OpenIDConnect,
|
|
358
|
+
redirectURL: "<your Next.js app>/api/auth/callback",
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
const { signOut } = useSaleorAuthContext();
|
|
362
|
+
|
|
363
|
+
if (isLoadingExternalAuth || isLoadingCurrentUser) {
|
|
364
|
+
return <div>Loading...</div>;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (data?.me) {
|
|
368
|
+
return (
|
|
369
|
+
<div>
|
|
370
|
+
{JSON.stringify(data)}
|
|
371
|
+
<button onClick={() => signOut()}>Logout</button>
|
|
372
|
+
</div>
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
if (authURL) {
|
|
376
|
+
return (
|
|
377
|
+
<div>
|
|
378
|
+
<Link href={authURL}>Login</Link>
|
|
379
|
+
</div>
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
return <div>Something went wrong</div>;
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
You also need to define the auth callback. In `pages/api/auth` create the `callback.ts` with the following content:
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
import { ExternalProvider, SaleorExternalAuth } from "@saleor/auth-sdk";
|
|
390
|
+
import { createSaleorExternalAuthHandler } from "@saleor/auth-sdk/next";
|
|
391
|
+
|
|
392
|
+
const externalAuth = new SaleorExternalAuth("<your Saleor instance URL>", ExternalProvider.OpenIDConnect);
|
|
393
|
+
|
|
394
|
+
export default createSaleorExternalAuthHandler(externalAuth);
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
## FAQ
|
|
398
|
+
|
|
399
|
+
## How do I reset password?
|
|
400
|
+
|
|
401
|
+
The `SaleorAuthClient` class provides you with a reset password method. If the reset password mutation is successful, it will log you in automatically, just like after a regular sign-in. The [`onSignIn` method of `useAuthChange` hook](#how-do-i-tell-my-graphql-client-to-refresh-queries-on-signin--signout) will also be triggered.
|
|
402
|
+
|
|
403
|
+
```javascript
|
|
404
|
+
const { resetPassword } = useSaleorAuthContext();
|
|
405
|
+
|
|
406
|
+
const response = await resetPassword({
|
|
407
|
+
email: "example@mail.com",
|
|
408
|
+
password: "newPassword",
|
|
409
|
+
token: "apiToken",
|
|
410
|
+
});
|
|
411
|
+
```
|
package/justfile
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ss-saleor-auth-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Auth SDK for Saleor",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"module": "index.mjs",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prepublishOnly": "pnpm build",
|
|
11
|
+
"prebuild": "pnpm clean",
|
|
12
|
+
"watch": "tsup-node src/* --format esm,cjs --dts --watch",
|
|
13
|
+
"build": "tsup-node src/* --format esm,cjs --dts && clear-package-json package.json -o dist/package.json --fields publishConfig && pnpm copy-files",
|
|
14
|
+
"clean": "rm -rf ./dist/*",
|
|
15
|
+
"test": "vitest",
|
|
16
|
+
"lint": "prettier --write . && eslint --fix src ",
|
|
17
|
+
"lint:ci": "prettier --check . && eslint src",
|
|
18
|
+
"release": "cd dist && ../node_modules/.bin/release-it",
|
|
19
|
+
"copy-files": "cp README.md dist/README.md"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [],
|
|
22
|
+
"author": "Saleor Team",
|
|
23
|
+
"license": "BSD-3-Clause",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@graphql-typed-document-node/core": "^3.2.0",
|
|
26
|
+
"cookie": "^0.6.0",
|
|
27
|
+
"graphql": "^16.8.1"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"next": "^13.4.4 || ^14.0.0",
|
|
31
|
+
"react": "^18.2.0",
|
|
32
|
+
"react-dom": "^18.2.0",
|
|
33
|
+
"urql": "^4.0.3"
|
|
34
|
+
},
|
|
35
|
+
"peerDependenciesMeta": {
|
|
36
|
+
"react": {
|
|
37
|
+
"optional": true
|
|
38
|
+
},
|
|
39
|
+
"react-dom": {
|
|
40
|
+
"optional": true
|
|
41
|
+
},
|
|
42
|
+
"urql": {
|
|
43
|
+
"optional": true
|
|
44
|
+
},
|
|
45
|
+
"next": {
|
|
46
|
+
"optional": true
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@graphql-typed-document-node/core": "^3.2.0",
|
|
51
|
+
"@types/cookie": "^0.5.4",
|
|
52
|
+
"@types/debug": "^4.1.12",
|
|
53
|
+
"@types/node": "^20.9.0",
|
|
54
|
+
"@types/node-fetch": "^2.6.9",
|
|
55
|
+
"@types/react": "^18.2.37",
|
|
56
|
+
"@types/react-dom": "^18.2.15",
|
|
57
|
+
"@types/uuid": "^9.0.7",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^6.11.0",
|
|
59
|
+
"@typescript-eslint/parser": "^6.11.0",
|
|
60
|
+
"@vitejs/plugin-react": "^4.1.1",
|
|
61
|
+
"clean-publish": "^4.2.0",
|
|
62
|
+
"eslint": "^8.53.0",
|
|
63
|
+
"eslint-config-prettier": "^9.0.0",
|
|
64
|
+
"eslint-import-resolver-typescript": "^3.6.1",
|
|
65
|
+
"eslint-plugin-import": "^2.29.0",
|
|
66
|
+
"eslint-plugin-jsx-a11y": "^6.8.0",
|
|
67
|
+
"eslint-plugin-react": "^7.33.2",
|
|
68
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
69
|
+
"eslint-plugin-simple-import-sort": "^10.0.0",
|
|
70
|
+
"jsdom": "^22.1.0",
|
|
71
|
+
"prettier": "^3.1.0",
|
|
72
|
+
"react": "^18.2.0",
|
|
73
|
+
"react-dom": "^18.2.0",
|
|
74
|
+
"release-it": "^17.0.0",
|
|
75
|
+
"tsup": "^7.2.0",
|
|
76
|
+
"typescript": "^5.2.2",
|
|
77
|
+
"urql": "^4.0.6",
|
|
78
|
+
"vite": "^4.5.0",
|
|
79
|
+
"vitest": "^0.34.6",
|
|
80
|
+
"vitest-fetch-mock": "^0.2.2"
|
|
81
|
+
},
|
|
82
|
+
"lint-staged": {
|
|
83
|
+
"*.{js,ts,tsx}": "eslint --cache --fix",
|
|
84
|
+
"*.{js,ts,tsx,css,md,json}": "prettier --write"
|
|
85
|
+
},
|
|
86
|
+
"exports": {
|
|
87
|
+
"./package.json": "./package.json",
|
|
88
|
+
"./react": {
|
|
89
|
+
"types": "./react/index.d.ts",
|
|
90
|
+
"import": "./react/index.mjs",
|
|
91
|
+
"require": "./react/index.js"
|
|
92
|
+
},
|
|
93
|
+
"./next": {
|
|
94
|
+
"types": "./next/index.d.ts",
|
|
95
|
+
"import": "./next/index.mjs",
|
|
96
|
+
"require": "./next/index.js"
|
|
97
|
+
},
|
|
98
|
+
"./next/server": {
|
|
99
|
+
"types": "./next/server.d.ts",
|
|
100
|
+
"import": "./next/server.mjs",
|
|
101
|
+
"require": "./next/server.js"
|
|
102
|
+
},
|
|
103
|
+
".": {
|
|
104
|
+
"types": "./index.d.ts",
|
|
105
|
+
"import": "./index.mjs",
|
|
106
|
+
"require": "./index.js"
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
"publishConfig": {
|
|
110
|
+
"access": "public",
|
|
111
|
+
"directory": "dist"
|
|
112
|
+
},
|
|
113
|
+
"repository": {
|
|
114
|
+
"type": "git",
|
|
115
|
+
"url": "git+https://github.com/saleor/auth-sdk.git"
|
|
116
|
+
},
|
|
117
|
+
"bugs": {
|
|
118
|
+
"url": "https://github.com/saleor/auth-sdk/issues"
|
|
119
|
+
},
|
|
120
|
+
"homepage": "https://github.com/saleor/auth-sdk#readme",
|
|
121
|
+
"engines": {
|
|
122
|
+
"node": ">=18.0.0"
|
|
123
|
+
}
|
|
124
|
+
}
|
package/setup-tests.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { StorageRepository } from "./types";
|
|
2
|
+
|
|
3
|
+
export const getAccessTokenKey = (prefix?: string) =>
|
|
4
|
+
[prefix, "saleor_auth_access_token"].filter(Boolean).join("+");
|
|
5
|
+
|
|
6
|
+
export class SaleorAccessTokenStorageHandler {
|
|
7
|
+
constructor(
|
|
8
|
+
private storage: StorageRepository,
|
|
9
|
+
private prefix?: string,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
getAccessToken = () => {
|
|
13
|
+
const key = getAccessTokenKey(this.prefix);
|
|
14
|
+
return this.storage.getItem(key);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
setAccessToken = (token: string) => {
|
|
18
|
+
const key = getAccessTokenKey(this.prefix);
|
|
19
|
+
return this.storage.setItem(key, token);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
clearAuthStorage = () => {
|
|
23
|
+
const key = getAccessTokenKey(this.prefix);
|
|
24
|
+
return this.storage.removeItem(key);
|
|
25
|
+
};
|
|
26
|
+
}
|