@slashid/remix 0.1.2 → 0.1.4
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 +74 -56
- package/dist/main.js +2542 -2425
- package/dist/main.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-

|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
@@ -7,46 +7,37 @@
|
|
|
7
7
|
|
|
8
8
|
Check out our [developer docs](https://developer.slashid.dev/) for guides and API documentation.
|
|
9
9
|
|
|
10
|
-
##
|
|
10
|
+
## Quick Start
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
`@slashid/remix` requires Remix v2.
|
|
13
13
|
|
|
14
|
-
You will need to [sign up to SlashID](https://console.slashid.dev/) and create an organization, once you've
|
|
15
|
-
|
|
16
|
-
Your environment should have the following dependencies installed:
|
|
17
|
-
|
|
18
|
-
- `node.js` => `>=20.x.x`
|
|
19
|
-
- `react` => `>=18.x.x`
|
|
20
|
-
|
|
21
|
-
### Quick start
|
|
22
|
-
|
|
23
|
-
#### 1. Install `@slashid/remix`
|
|
14
|
+
You will need to [sign up to SlashID](https://console.slashid.dev/) and create an organization, once you've done this you'll find your organization ID in [Settings -> General](https://console.slashid.dev/settings/general).
|
|
24
15
|
|
|
16
|
+
### 1. Install `@slashid/remix`
|
|
25
17
|
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
|
-
|
|
27
18
|
```
|
|
28
19
|
npm install @slashid/remix
|
|
29
20
|
```
|
|
30
21
|
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
### 2. Create SlashID app primitives
|
|
33
23
|
In your Remix application create a file `app/slashid.ts`.
|
|
34
24
|
|
|
35
25
|
In this file create the SlashID application primitives and re-export them, you'll use them later.
|
|
36
|
-
|
|
37
26
|
```ts
|
|
38
27
|
// app/slashid.ts
|
|
39
28
|
|
|
40
|
-
import { createSlashIDApp } from
|
|
29
|
+
import { createSlashIDApp } from '@slashid/remix'
|
|
41
30
|
|
|
42
|
-
export const {
|
|
43
|
-
|
|
44
|
-
|
|
31
|
+
export const {
|
|
32
|
+
SlashIDApp,
|
|
33
|
+
slashIDRootLoader,
|
|
34
|
+
slashIDLoader
|
|
35
|
+
} = createSlashIDApp({ oid: "YOUR_ORGANIZATION_ID" });
|
|
45
36
|
|
|
37
|
+
```
|
|
46
38
|
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).
|
|
47
39
|
|
|
48
|
-
|
|
49
|
-
|
|
40
|
+
### 3. Configure `slashIDRootLoader`
|
|
50
41
|
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.
|
|
51
42
|
|
|
52
43
|
Import the `slashIDRootLoader` you created in the previous step, invoke it and export it as `loader`.
|
|
@@ -66,9 +57,9 @@ import {
|
|
|
66
57
|
} from "@remix-run/react";
|
|
67
58
|
|
|
68
59
|
import { slashIDRootLoader } from "~/slashid";
|
|
69
|
-
|
|
60
|
+
|
|
70
61
|
export const loader: LoaderFunction = slashIDRootLoader();
|
|
71
|
-
|
|
62
|
+
|
|
72
63
|
export default function App() {
|
|
73
64
|
return (
|
|
74
65
|
<html lang="en">
|
|
@@ -94,11 +85,11 @@ If you need to load additional data via the root loader, you can simply pass a l
|
|
|
94
85
|
```ts
|
|
95
86
|
// root.ts
|
|
96
87
|
|
|
97
|
-
import { slashIDRootLoader } from "~/slashid"
|
|
98
|
-
import { getUser } from
|
|
88
|
+
import { slashIDRootLoader } from "~/slashid"
|
|
89
|
+
import { getUser } from '@slashid/remix'
|
|
99
90
|
|
|
100
91
|
export const loader: LoaderFunction = slashIDRootLoader((args) => {
|
|
101
|
-
const user = getUser(args)
|
|
92
|
+
const user = getUser(args)
|
|
102
93
|
|
|
103
94
|
if (user) {
|
|
104
95
|
// the user is logged in
|
|
@@ -107,19 +98,18 @@ export const loader: LoaderFunction = slashIDRootLoader((args) => {
|
|
|
107
98
|
// fetch data
|
|
108
99
|
|
|
109
100
|
return {
|
|
110
|
-
hello: "world!"
|
|
111
|
-
}
|
|
101
|
+
hello: "world!" // your data
|
|
102
|
+
}
|
|
112
103
|
});
|
|
113
104
|
```
|
|
114
105
|
|
|
115
|
-
|
|
116
|
-
|
|
106
|
+
### 4. Wrap your application body with `<SlashIDApp>`
|
|
117
107
|
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.
|
|
118
108
|
|
|
119
109
|
```tsx
|
|
120
110
|
// root.tsx
|
|
121
111
|
|
|
122
|
-
import { SlashIDApp } from
|
|
112
|
+
import { SlashIDApp } from '~/slashid'
|
|
123
113
|
|
|
124
114
|
export default function App() {
|
|
125
115
|
return (
|
|
@@ -144,16 +134,21 @@ export default function App() {
|
|
|
144
134
|
}
|
|
145
135
|
```
|
|
146
136
|
|
|
147
|
-
|
|
148
|
-
|
|
137
|
+
### 5. Create your log-in/sign-up page
|
|
149
138
|
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.
|
|
150
139
|
|
|
151
140
|
```tsx
|
|
152
141
|
// routes/login.tsx
|
|
153
142
|
|
|
154
|
-
import {
|
|
143
|
+
import {
|
|
144
|
+
ConfigurationProvider,
|
|
145
|
+
Form,
|
|
146
|
+
type Factor,
|
|
147
|
+
} from "@slashid/remix";
|
|
155
148
|
|
|
156
|
-
const factors: Factor[] = [
|
|
149
|
+
const factors: Factor[] = [
|
|
150
|
+
{ method: "email_link" }
|
|
151
|
+
];
|
|
157
152
|
|
|
158
153
|
export default function LogIn() {
|
|
159
154
|
return (
|
|
@@ -166,10 +161,28 @@ export default function LogIn() {
|
|
|
166
161
|
}
|
|
167
162
|
```
|
|
168
163
|
|
|
169
|
-
|
|
164
|
+
After the user has logged in, you might want to redirect them to another page. You can this with a loader.
|
|
170
165
|
|
|
171
|
-
|
|
166
|
+
When authentication has complete the form will tell the loader to run again, refreshing the authentication context, and triggering the redirect.
|
|
172
167
|
|
|
168
|
+
```tsx
|
|
169
|
+
// routes/login.tsx
|
|
170
|
+
|
|
171
|
+
import { slashIDLoader } from '~/slashid'
|
|
172
|
+
|
|
173
|
+
export const loader = slashIDLoader((args) => {
|
|
174
|
+
const user = getUser(args)
|
|
175
|
+
|
|
176
|
+
if (user) {
|
|
177
|
+
return redirect("index") // redirect to your desired destination
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return {}
|
|
181
|
+
})
|
|
182
|
+
```
|
|
183
|
+
### 6. Protecting your pages
|
|
184
|
+
|
|
185
|
+
#### Server side
|
|
173
186
|
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.
|
|
174
187
|
|
|
175
188
|
You'll check if the user exists, and if not redirect them to the login page.
|
|
@@ -179,34 +192,35 @@ The `useSlashID()` hook provides authentication state & helper functions to your
|
|
|
179
192
|
```tsx
|
|
180
193
|
// routes/_index.tsx
|
|
181
194
|
|
|
182
|
-
import { slashIDLoader } from
|
|
183
|
-
import { getUser, useSlashID } from
|
|
184
|
-
|
|
195
|
+
import { slashIDLoader } from '~/slashid'
|
|
196
|
+
import { getUser, useSlashID } from '@slashid/remix'
|
|
197
|
+
|
|
185
198
|
export const loader = slashIDLoader((args) => {
|
|
186
|
-
const user = getUser(args)
|
|
199
|
+
const user = getUser(args)
|
|
187
200
|
|
|
188
201
|
if (!user) {
|
|
189
|
-
return redirect("login")
|
|
202
|
+
return redirect("login")
|
|
190
203
|
}
|
|
191
204
|
|
|
192
|
-
return {}
|
|
193
|
-
})
|
|
205
|
+
return {}
|
|
206
|
+
})
|
|
194
207
|
|
|
195
208
|
export default function Index() {
|
|
196
|
-
const { logOut } = useSlashID()
|
|
209
|
+
const { logOut } = useSlashID()
|
|
197
210
|
|
|
198
211
|
return (
|
|
199
212
|
<div>
|
|
200
213
|
<h1>Index</h1>
|
|
201
214
|
<p>You are logged in!</p>
|
|
202
|
-
<button onClick={logOut}>
|
|
215
|
+
<button onClick={logOut}>
|
|
216
|
+
Log out
|
|
217
|
+
</button>
|
|
203
218
|
</div>
|
|
204
|
-
)
|
|
219
|
+
)
|
|
205
220
|
}
|
|
206
221
|
```
|
|
207
222
|
|
|
208
|
-
|
|
209
|
-
|
|
223
|
+
#### Client side
|
|
210
224
|
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
225
|
|
|
212
226
|
You'll implement `<LoggedIn>`, `<LoggedOut>`, and provide the option to log-out by implementing the `logOut` helper function from the `useSlashID()` hook.
|
|
@@ -214,21 +228,25 @@ You'll implement `<LoggedIn>`, `<LoggedOut>`, and provide the option to log-out
|
|
|
214
228
|
```tsx
|
|
215
229
|
// routes/_index.tsx
|
|
216
230
|
|
|
217
|
-
import { LoggedIn, LoggedOut, useSlashID } from
|
|
231
|
+
import { LoggedIn, LoggedOut, useSlashID } from '@slashid/remix'
|
|
218
232
|
import { useNavigate } from "@remix-run/react";
|
|
219
233
|
|
|
220
234
|
export default function Index() {
|
|
221
|
-
const { logOut } = useSlashID()
|
|
235
|
+
const { logOut } = useSlashID()
|
|
222
236
|
const navigate = useNavigate();
|
|
223
237
|
|
|
224
238
|
return (
|
|
225
239
|
<div>
|
|
226
240
|
<LoggedIn>
|
|
227
241
|
You are logged in!
|
|
228
|
-
<button onClick={logOut}>
|
|
242
|
+
<button onClick={logOut}>
|
|
243
|
+
Log out
|
|
244
|
+
</button>
|
|
229
245
|
</LoggedIn>
|
|
230
|
-
<LoggedOut>
|
|
246
|
+
<LoggedOut>
|
|
247
|
+
{navigate("login")}
|
|
248
|
+
</LoggedOut>
|
|
231
249
|
</div>
|
|
232
|
-
)
|
|
250
|
+
)
|
|
233
251
|
}
|
|
234
252
|
```
|