@slashid/remix 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![SlashID Remix SDK](./banner.png)
1
+ ![SlashID Remix SDK](https://raw.githubusercontent.com/slashid/javascript/main/packages/remix/banner.png)
2
2
 
3
3
  ![npm](https://img.shields.io/npm/v/@slashid/remix)
4
4
  ![build](https://github.com/slashid/javascript/actions/workflows/ci.yml/badge.svg)
@@ -14,36 +14,39 @@ Check out our [developer docs](https://developer.slashid.dev/) for guides and AP
14
14
  You will need to [sign up to SlashID](https://console.slashid.dev/) and create an organization, once you've complete this you'll find your organization ID in [Settings -> General](https://console.slashid.dev/settings/general).
15
15
 
16
16
  Your environment should have the following dependencies installed:
17
+
17
18
  - `node.js` => `>=20.x.x`
18
19
  - `react` => `>=18.x.x`
19
20
 
20
21
  ### Quick start
21
22
 
22
23
  #### 1. Install `@slashid/remix`
24
+
23
25
  Once you have a Remix application ready to go, you need to install the SlashID Remix SDK. This SDK provides a prebuilt log-in & sign-up form, control components and hooks - tailor made for Remix.
26
+
24
27
  ```
25
28
  npm install @slashid/remix
26
29
  ```
27
30
 
28
31
  #### 2. Create SlashID app primitives
32
+
29
33
  In your Remix application create a file `app/slashid.ts`.
30
34
 
31
35
  In this file create the SlashID application primitives and re-export them, you'll use them later.
36
+
32
37
  ```ts
33
38
  // app/slashid.ts
34
39
 
35
- import { createSlashIDApp } from '@slashid/remix'
36
-
37
- export const {
38
- SlashIDApp,
39
- slashIDRootLoader,
40
- slashIDLoader
41
- } = createSlashIDApp({ oid: "YOUR_ORGANIZATION_ID" });
40
+ import { createSlashIDApp } from "@slashid/remix";
42
41
 
42
+ export const { SlashIDApp, slashIDRootLoader, slashIDLoader } =
43
+ createSlashIDApp({ oid: "YOUR_ORGANIZATION_ID" });
43
44
  ```
45
+
44
46
  Tip: `oid` is your organization ID, you can find it in the [SlashID console](https://console.slashid.dev/) under [Settings -> General](https://console.slashid.dev/settings/general).
45
47
 
46
48
  #### 3. Configure `slashIDRootLoader`
49
+
47
50
  To configure SlashID in your Remix application you'll need to update your root loader. With a small change you'll have easy access to authentication in all of your Remix routes.
48
51
 
49
52
  Import the `slashIDRootLoader` you created in the previous step, invoke it and export it as `loader`.
@@ -63,9 +66,9 @@ import {
63
66
  } from "@remix-run/react";
64
67
 
65
68
  import { slashIDRootLoader } from "~/slashid";
66
-
69
+
67
70
  export const loader: LoaderFunction = slashIDRootLoader();
68
-
71
+
69
72
  export default function App() {
70
73
  return (
71
74
  <html lang="en">
@@ -91,11 +94,11 @@ If you need to load additional data via the root loader, you can simply pass a l
91
94
  ```ts
92
95
  // root.ts
93
96
 
94
- import { slashIDRootLoader } from "~/slashid"
95
- import { getUser } from '@slashid/remix'
97
+ import { slashIDRootLoader } from "~/slashid";
98
+ import { getUser } from "@slashid/remix";
96
99
 
97
100
  export const loader: LoaderFunction = slashIDRootLoader((args) => {
98
- const user = getUser(args)
101
+ const user = getUser(args);
99
102
 
100
103
  if (user) {
101
104
  // the user is logged in
@@ -104,18 +107,19 @@ export const loader: LoaderFunction = slashIDRootLoader((args) => {
104
107
  // fetch data
105
108
 
106
109
  return {
107
- hello: "world!" // your data
108
- }
110
+ hello: "world!", // your data
111
+ };
109
112
  });
110
113
  ```
111
114
 
112
115
  #### 4. Wrap your application body with `<SlashIDApp>`
116
+
113
117
  In step 2 you created `SlashIDApp`, it provides authentication state to your React component tree. There is no configuration, just add it to your Remix application.
114
118
 
115
119
  ```tsx
116
120
  // root.tsx
117
121
 
118
- import { SlashIDApp } from '~/slashid'
122
+ import { SlashIDApp } from "~/slashid";
119
123
 
120
124
  export default function App() {
121
125
  return (
@@ -141,20 +145,15 @@ export default function App() {
141
145
  ```
142
146
 
143
147
  #### 5. Create your log-in/sign-up page
148
+
144
149
  SlashID offers a prebuilt form that works for both log-in and sign-up. Users with an account can log-in here and users without one need to simply complete the form to create their account.
145
150
 
146
151
  ```tsx
147
152
  // routes/login.tsx
148
153
 
149
- import {
150
- ConfigurationProvider,
151
- Form,
152
- type Factor,
153
- } from "@slashid/remix";
154
+ import { ConfigurationProvider, Form, type Factor } from "@slashid/remix";
154
155
 
155
- const factors: Factor[] = [
156
- { method: "email_link" }
157
- ];
156
+ const factors: Factor[] = [{ method: "email_link" }];
158
157
 
159
158
  export default function LogIn() {
160
159
  return (
@@ -166,9 +165,11 @@ export default function LogIn() {
166
165
  );
167
166
  }
168
167
  ```
168
+
169
169
  #### 6. Protecting your pages
170
170
 
171
171
  ##### Server side
172
+
172
173
  To protect your routes you can use the `slashIDLoader` you created in step 2. This utility is a wrapper for your loaders that provides authentication state to your loader code.
173
174
 
174
175
  You'll check if the user exists, and if not redirect them to the login page.
@@ -178,35 +179,34 @@ The `useSlashID()` hook provides authentication state & helper functions to your
178
179
  ```tsx
179
180
  // routes/_index.tsx
180
181
 
181
- import { slashIDLoader } from '~/slashid'
182
- import { getUser, useSlashID } from '@slashid/remix'
183
-
182
+ import { slashIDLoader } from "~/slashid";
183
+ import { getUser, useSlashID } from "@slashid/remix";
184
+
184
185
  export const loader = slashIDLoader((args) => {
185
- const user = getUser(args)
186
+ const user = getUser(args);
186
187
 
187
188
  if (!user) {
188
- return redirect("login")
189
+ return redirect("login");
189
190
  }
190
191
 
191
- return {}
192
- })
192
+ return {};
193
+ });
193
194
 
194
195
  export default function Index() {
195
- const { logOut } = useSlashID()
196
+ const { logOut } = useSlashID();
196
197
 
197
198
  return (
198
199
  <div>
199
200
  <h1>Index</h1>
200
201
  <p>You are logged in!</p>
201
- <button onClick={logOut}>
202
- Log out
203
- </button>
202
+ <button onClick={logOut}>Log out</button>
204
203
  </div>
205
- )
204
+ );
206
205
  }
207
206
  ```
208
207
 
209
208
  ##### Client side
209
+
210
210
  SlashID has several [Control Components](https://developer.slashid.dev/docs/access/react-sdk/reference/components/react-sdk-reference-loggedin) that allow you to conditionally show or hide content based on the users authentication state.
211
211
 
212
212
  You'll implement `<LoggedIn>`, `<LoggedOut>`, and provide the option to log-out by implementing the `logOut` helper function from the `useSlashID()` hook.
@@ -214,25 +214,21 @@ You'll implement `<LoggedIn>`, `<LoggedOut>`, and provide the option to log-out
214
214
  ```tsx
215
215
  // routes/_index.tsx
216
216
 
217
- import { LoggedIn, LoggedOut, useSlashID } from '@slashid/remix'
217
+ import { LoggedIn, LoggedOut, useSlashID } from "@slashid/remix";
218
218
  import { useNavigate } from "@remix-run/react";
219
219
 
220
220
  export default function Index() {
221
- const { logOut } = useSlashID()
221
+ const { logOut } = useSlashID();
222
222
  const navigate = useNavigate();
223
223
 
224
224
  return (
225
225
  <div>
226
226
  <LoggedIn>
227
227
  You are logged in!
228
- <button onClick={logOut}>
229
- Log out
230
- </button>
228
+ <button onClick={logOut}>Log out</button>
231
229
  </LoggedIn>
232
- <LoggedOut>
233
- {navigate("login")}
234
- </LoggedOut>
230
+ <LoggedOut>{navigate("login")}</LoggedOut>
235
231
  </div>
236
- )
232
+ );
237
233
  }
238
234
  ```