@seamless-auth/svelte 0.0.0-stage → 0.2.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/CHANGELOG.md +29 -0
- package/LICENSE +202 -0
- package/README.md +253 -2
- package/dist/auth.svelte.d.ts +170 -0
- package/dist/auth.svelte.js +208 -0
- package/dist/config.d.ts +49 -0
- package/dist/config.js +6 -0
- package/dist/context.d.ts +13 -0
- package/dist/context.js +35 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +21 -0
- package/dist/kit/index.d.ts +55 -0
- package/dist/kit/index.js +121 -0
- package/dist/kit/watch.svelte.d.ts +10 -0
- package/dist/kit/watch.svelte.js +28 -0
- package/dist/navigation.d.ts +40 -0
- package/dist/navigation.js +22 -0
- package/dist/screens/AuthLayout.svelte +38 -0
- package/dist/screens/AuthLayout.svelte.d.ts +8 -0
- package/dist/screens/FallbackOptions.svelte +69 -0
- package/dist/screens/FallbackOptions.svelte.d.ts +13 -0
- package/dist/screens/Login.svelte +253 -0
- package/dist/screens/Login.svelte.d.ts +3 -0
- package/dist/screens/MagicLinkSent.svelte +74 -0
- package/dist/screens/MagicLinkSent.svelte.d.ts +3 -0
- package/dist/screens/OAuthCallback.svelte +69 -0
- package/dist/screens/OAuthCallback.svelte.d.ts +3 -0
- package/dist/screens/OAuthProviderButtons.svelte +49 -0
- package/dist/screens/OAuthProviderButtons.svelte.d.ts +6 -0
- package/dist/screens/OtpInput.svelte +101 -0
- package/dist/screens/OtpInput.svelte.d.ts +10 -0
- package/dist/screens/PasskeyLogin.svelte +34 -0
- package/dist/screens/PasskeyLogin.svelte.d.ts +3 -0
- package/dist/screens/RegisterPasskey.svelte +110 -0
- package/dist/screens/RegisterPasskey.svelte.d.ts +3 -0
- package/dist/screens/VerifyMagicLink.svelte +195 -0
- package/dist/screens/VerifyMagicLink.svelte.d.ts +3 -0
- package/dist/screens/VerifyOtp.svelte +141 -0
- package/dist/screens/VerifyOtp.svelte.d.ts +7 -0
- package/dist/seamless-auth.css +260 -0
- package/dist/styles.generated.d.ts +2 -0
- package/dist/styles.generated.js +3 -0
- package/package.json +76 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @seamless-auth/svelte
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 59802e2: Add a confirmation step for magic links opened on another device.
|
|
8
|
+
|
|
9
|
+
A magic link opened on a device other than the one that asked for it now waits for the reader to approve it (auth API 0.19.0 and later). The bundled verify screens show which kind of device asked and when, and offer "Yes, sign in that device" or "No, this wasn't me". Approving signs in the requesting device and tells the reader to return to it; declining changes nothing.
|
|
10
|
+
|
|
11
|
+
`@seamless-auth/client` adds `client.confirmMagicLink(token)`, the `getMagicLinkConfirmation`, `isMagicLinkConfirmationRequired`, `describeMagicLinkDevice`, `magicLinkConfirmationPrompt` and `confirmMagicLinkSignIn` helpers, and the `MagicLinkConfirmation` and `MagicLinkDeviceClass` types. In bearer mode the verify call now sends the sign-in's ephemeral token when one is held. A custom verify screen that treats every failure as an invalid link should check `getMagicLinkConfirmation(error)` first.
|
|
12
|
+
|
|
13
|
+
The React Native README describes the new step for apps that handle magic links.
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [59802e2]
|
|
18
|
+
- @seamless-auth/client@0.6.0
|
|
19
|
+
|
|
20
|
+
## 0.1.0
|
|
21
|
+
|
|
22
|
+
### Minor Changes
|
|
23
|
+
|
|
24
|
+
- d1a70a1: First release of `@seamless-auth/svelte`, the Svelte binding for Seamless Auth (Svelte 5).
|
|
25
|
+
- `createSeamlessAuth({ apiHost })` creates a session whose properties (`user`, `isAuthenticated`, `loading`, and the rest) are runes, with every auth action. `setSeamlessAuth` and `getSeamlessAuth` share it through context.
|
|
26
|
+
- `@seamless-auth/svelte/kit` provides `requireAuth(auth, { roles })` and `requireGuest(auth)` `load` guards, and `createKitNavigator` for the bundled screens, for SvelteKit 2.26 and later, and SvelteKit 3. Each guard waits for the session to be read.
|
|
27
|
+
- Sign-in screens (`SaLogin`, `SaVerifyOtp`, `SaMagicLinkSent`, `SaVerifyMagicLink`, `SaOAuthCallback`, `SaPasskeyLogin`, `SaRegisterPasskey`) to mount at your routes, themed with the same `--seamless-*` properties as the other bindings. They navigate through an `AuthNavigator`, so any router can drive them.
|
|
28
|
+
|
|
29
|
+
The binding uses cookie transport only: tokens stay in the server adapter, and nothing is written where page scripts can read it.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,254 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @seamless-auth/svelte
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@seamless-auth/svelte)
|
|
4
|
+
[](https://github.com/fells-code/seamless-auth-react/actions/workflows/ci.yml)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
Svelte bindings for Seamless Auth: a rune-based session with every auth action, SvelteKit `load`
|
|
8
|
+
guards, and optional sign-in screens. It is a thin binding over
|
|
9
|
+
[`@seamless-auth/client`](../client/README.md), the same core the React, Angular, and Vue bindings
|
|
10
|
+
use, so the flows behave the same in all of them.
|
|
11
|
+
|
|
12
|
+
## Start here
|
|
13
|
+
|
|
14
|
+
New to Seamless Auth? The [self-hosted quickstart](https://docs.seamlessauth.com/start/quickstart/)
|
|
15
|
+
runs the full stack locally with Docker. The [compatibility matrix](https://docs.seamlessauth.com/build/ecosystem/#compatibility-matrix)
|
|
16
|
+
lists which package versions work together.
|
|
17
|
+
|
|
18
|
+
The browser talks only to your backend's `/auth` routes, served by a server adapter. The adapter keeps
|
|
19
|
+
the tokens; the browser holds only HttpOnly cookies. This package has no bearer mode and never writes
|
|
20
|
+
a token to `localStorage`, `sessionStorage`, or anywhere page scripts can read it.
|
|
21
|
+
|
|
22
|
+
## Requirements
|
|
23
|
+
|
|
24
|
+
- Svelte 5
|
|
25
|
+
- For the guards and the SvelteKit navigator in `@seamless-auth/svelte/kit`: SvelteKit 2.26 or later,
|
|
26
|
+
or SvelteKit 3. The root entry works in any Svelte 5 application
|
|
27
|
+
- A Seamless Auth server adapter mounted at `/auth` on `apiHost`
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install @seamless-auth/svelte
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick start with SvelteKit
|
|
36
|
+
|
|
37
|
+
The examples import from `$lib`, as SvelteKit 2 does. In SvelteKit 3, which renamed it, import from
|
|
38
|
+
`#lib`.
|
|
39
|
+
|
|
40
|
+
Create the session once, in the browser:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// src/lib/auth.ts
|
|
44
|
+
import { createSeamlessAuth } from '@seamless-auth/svelte';
|
|
45
|
+
|
|
46
|
+
export const auth = createSeamlessAuth({ apiHost: 'https://app.example.com' });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The session lives in the browser, so render the application there. A backend-for-frontend app
|
|
50
|
+
usually ships as a single-page app (`adapter-static` with a fallback page):
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// src/routes/+layout.ts
|
|
54
|
+
export const ssr = false;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Hand the session and a navigator to the components below the root layout:
|
|
58
|
+
|
|
59
|
+
```svelte
|
|
60
|
+
<!-- src/routes/+layout.svelte -->
|
|
61
|
+
<script lang="ts">
|
|
62
|
+
import { setAuthNavigator, setSeamlessAuth } from '@seamless-auth/svelte';
|
|
63
|
+
import { createKitNavigator } from '@seamless-auth/svelte/kit';
|
|
64
|
+
import { auth } from '$lib/auth';
|
|
65
|
+
|
|
66
|
+
let { children } = $props();
|
|
67
|
+
|
|
68
|
+
setSeamlessAuth(auth);
|
|
69
|
+
setAuthNavigator(createKitNavigator(auth));
|
|
70
|
+
</script>
|
|
71
|
+
|
|
72
|
+
{@render children()}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Guard a route with a `load` function:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
// src/routes/(app)/+layout.ts
|
|
79
|
+
import { requireAuth } from '@seamless-auth/svelte/kit';
|
|
80
|
+
import { auth } from '$lib/auth';
|
|
81
|
+
|
|
82
|
+
export const load = requireAuth(auth);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Mount each sign-in screen at its route:
|
|
86
|
+
|
|
87
|
+
```svelte
|
|
88
|
+
<!-- src/routes/login/+page.svelte -->
|
|
89
|
+
<script lang="ts">
|
|
90
|
+
import { SaLogin } from '@seamless-auth/svelte';
|
|
91
|
+
</script>
|
|
92
|
+
|
|
93
|
+
<SaLogin />
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
| Option | Default | Purpose |
|
|
97
|
+
| -------------------------- | ---------------- | ----------------------------------------------------------- |
|
|
98
|
+
| `apiHost` | required | Origin of the server adapter |
|
|
99
|
+
| `basePath` | `/auth` | Where the adapter is mounted |
|
|
100
|
+
| `magicLinkRedirectUri` | the deployment's | Where an emailed magic link lands |
|
|
101
|
+
| `autoDetectPreviousSignIn` | `true` | Open the login screen on Sign In for a returning browser |
|
|
102
|
+
| `initialSession` | none | A session the server already resolved |
|
|
103
|
+
| `ports` | browser | `passkeys` and `oauthRedirect` ports |
|
|
104
|
+
| `fetch` | global `fetch` | The fetch auth requests go through |
|
|
105
|
+
| `trustedOrigins` | none | Other origins of yours that may receive the session cookies |
|
|
106
|
+
| `injectStyles` | `true` | Whether the screens add their stylesheet to the page |
|
|
107
|
+
| `cspNonce` | none | A CSP nonce for that stylesheet |
|
|
108
|
+
| `signedInPath` | `/` | Where the bundled screens go after sign-in |
|
|
109
|
+
| `loginPath` | `/login` | Where `requireAuth` sends someone signed out |
|
|
110
|
+
|
|
111
|
+
## Reading session state
|
|
112
|
+
|
|
113
|
+
Every property of the session is reactive, so read it straight in markup or an `$effect`:
|
|
114
|
+
|
|
115
|
+
```svelte
|
|
116
|
+
<script lang="ts">
|
|
117
|
+
import { goto } from '$app/navigation';
|
|
118
|
+
import { getSeamlessAuth } from '@seamless-auth/svelte';
|
|
119
|
+
|
|
120
|
+
const auth = getSeamlessAuth();
|
|
121
|
+
|
|
122
|
+
async function signOut() {
|
|
123
|
+
await auth.logout();
|
|
124
|
+
await goto('/login');
|
|
125
|
+
}
|
|
126
|
+
</script>
|
|
127
|
+
|
|
128
|
+
{#if auth.user}
|
|
129
|
+
<span>{auth.user.email}</span>
|
|
130
|
+
<button type="button" onclick={signOut}>Logout</button>
|
|
131
|
+
{/if}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Properties: `state`, `user`, `isAuthenticated`, `loading`, `credentials`, `organizations`,
|
|
135
|
+
`activeOrganization`, `stepUpStatus`, `hasSignedInBefore`, plus `loginMethods`,
|
|
136
|
+
`loginMethodsLoading`, `passkeySupported`, and `passkeySupportLoading`.
|
|
137
|
+
|
|
138
|
+
Actions, each resolving to a `SeamlessAuthResult<T>` (`{ data, error }`) that never throws for an HTTP
|
|
139
|
+
failure: `login`, `handlePasskeyLogin`, `registerPasskey`, `refreshSession`, `logout`,
|
|
140
|
+
`logoutAllSessions`, `deleteUser`, `updateCredential`, `deleteCredential`, `switchOrganization`,
|
|
141
|
+
`listOAuthProviders`, `startOAuthLogin`, `finishOAuthLogin`, `refreshStepUpStatus`,
|
|
142
|
+
`verifyStepUpWithPasskey`, `verifyStepUpWithPasskeyPrf`, `verifyStepUpWithTotp`.
|
|
143
|
+
|
|
144
|
+
Also: `hasRole`, `hasScopedRole`, `markSignedIn`, `whenSettled()` (resolves once the session is read),
|
|
145
|
+
`loadLoginMethods()` and `checkPasskeySupport()` (each read once and cached), `authorizedFetch` (a
|
|
146
|
+
fetch for your own API that carries the cookies to `apiHost` and `trustedOrigins` only), `client`
|
|
147
|
+
(the headless client for everything else, such as OTP and magic link calls), `ports`, and
|
|
148
|
+
`destroy()`.
|
|
149
|
+
|
|
150
|
+
## Guards
|
|
151
|
+
|
|
152
|
+
`@seamless-auth/svelte/kit` exports `load` functions. They wait for the session to be read before
|
|
153
|
+
deciding, so reloading a protected page does not bounce a signed-in user to the login screen.
|
|
154
|
+
|
|
155
|
+
| Guard | Admits | Otherwise |
|
|
156
|
+
| ------------------------------ | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
|
|
157
|
+
| `requireAuth(auth, options?)` | A signed-in user, with `roles` when given | Redirects to `redirectTo` (default `loginPath`), or `forbiddenRedirectTo` (default `signedInPath`) for a missing role |
|
|
158
|
+
| `requireGuest(auth, options?)` | Someone signed out | Redirects to `redirectTo` (default `signedInPath`) |
|
|
159
|
+
|
|
160
|
+
The guards register a dependency on the session and on the URL, so they run again on every
|
|
161
|
+
navigation and whenever the session changes (a sign-out in another tab, an expired session, a
|
|
162
|
+
switched organization), as long as the navigator from `createKitNavigator` is set in the root layout.
|
|
163
|
+
A server render that was not handed an `initialSession` cannot see the session, so `requireAuth`
|
|
164
|
+
treats it as signed out and redirects. Turn server rendering off for guarded routes
|
|
165
|
+
(`export const ssr = false`). `roles` uses the same matching as
|
|
166
|
+
the auth API, scoped roles such as `org:admin` included. A guard is a convenience for navigation, not
|
|
167
|
+
access control: your server still checks every request.
|
|
168
|
+
|
|
169
|
+
Put `requireGuest` only on the screens that start a sign-in (`login`, `passkey-login`,
|
|
170
|
+
`magic-link-sent`). A user can already have a session when `register-passkey`, `oauth/callback`, or
|
|
171
|
+
`verify-magiclink` renders.
|
|
172
|
+
|
|
173
|
+
## Built-in screens
|
|
174
|
+
|
|
175
|
+
Mount each at its route. The paths below are what the screens navigate between by default; pass
|
|
176
|
+
`paths` to `createKitNavigator` if you mount them elsewhere.
|
|
177
|
+
|
|
178
|
+
| Default path | Component | Purpose |
|
|
179
|
+
| ------------------- | ----------------------------- | ----------------------------------------------------------- |
|
|
180
|
+
| `/login` | `SaLogin` | Sign in with an email or phone number, or create an account |
|
|
181
|
+
| `/passkey-login` | `SaPasskeyLogin` | Sign in with a passkey alone |
|
|
182
|
+
| `/verify-email-otp` | `SaVerifyOtp channel="email"` | Check an email code |
|
|
183
|
+
| `/verify-phone-otp` | `SaVerifyOtp channel="phone"` | Check a text message code |
|
|
184
|
+
| `/magic-link-sent` | `SaMagicLinkSent` | Wait for an emailed link to be used |
|
|
185
|
+
| `/verify-magiclink` | `SaVerifyMagicLink` | Where the emailed link lands. The auth API builds this URL |
|
|
186
|
+
| `/oauth/callback` | `SaOAuthCallback` | The OAuth redirect URI to register with providers |
|
|
187
|
+
| `/register-passkey` | `SaRegisterPasskey` | Offer passkey enrolment after sign-up |
|
|
188
|
+
|
|
189
|
+
The building blocks are exported too: `SaOtpInput` (bind with `bind:value`), `SaFallbackOptions`,
|
|
190
|
+
`SaOAuthProviderButtons`, `SaAuthLayout`, and `authRoutePaths`.
|
|
191
|
+
|
|
192
|
+
The magic link and OAuth callback screens remove the token or code from the URL once they have read
|
|
193
|
+
it. Serve those routes with `Referrer-Policy: no-referrer` (or `same-origin`) so a single-use secret
|
|
194
|
+
does not leave in a `Referer` before then.
|
|
195
|
+
|
|
196
|
+
### Without SvelteKit
|
|
197
|
+
|
|
198
|
+
The screens never import a router. They navigate through an `AuthNavigator` set with
|
|
199
|
+
`setAuthNavigator`, so any router can drive them: implement its seven methods (`toScreen`, `toApp`,
|
|
200
|
+
`toLocation`, `state`, `query`, `dropQuery`, `absoluteUrl`) on top of your router.
|
|
201
|
+
`createKitNavigator` is the SvelteKit one.
|
|
202
|
+
|
|
203
|
+
## Headless use
|
|
204
|
+
|
|
205
|
+
Every screen is a thin view over a flow in `@seamless-auth/client` (`beginSignIn`,
|
|
206
|
+
`registerWithEmail`, `verifyOtp`, `watchMagicLink`, `finishMagicLinkSignIn`,
|
|
207
|
+
`confirmMagicLinkSignIn`, `startOAuthSignIn`, `completeOAuthCallback`, `enrollPasskey`, and helpers). A custom screen calls the same flows with the
|
|
208
|
+
session as its actions:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { beginSignIn } from '@seamless-auth/client';
|
|
212
|
+
|
|
213
|
+
const step = await beginSignIn(auth, {
|
|
214
|
+
identifier: email,
|
|
215
|
+
passkeySupported: await auth.checkPasskeySupport(),
|
|
216
|
+
configuredMethods: await auth.loadLoginMethods(),
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
if (step.kind === 'signed_in') await goto('/');
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
See the [client README](../client/README.md#flows) for the full list.
|
|
223
|
+
|
|
224
|
+
## Styling
|
|
225
|
+
|
|
226
|
+
The screens render inside `.sa-auth` with plain `.sa-*` classes. The first screen to mount adds the
|
|
227
|
+
stylesheet to the top of `<head>`, so your own styles override it. Colours come from the same
|
|
228
|
+
`--seamless-*` custom properties the React, Angular, and Vue screens read, so one theme covers every
|
|
229
|
+
binding:
|
|
230
|
+
|
|
231
|
+
```css
|
|
232
|
+
:root {
|
|
233
|
+
--seamless-accent: #1f3a34;
|
|
234
|
+
--seamless-accent-hover: #16302b;
|
|
235
|
+
--seamless-surface: #f7f5f0;
|
|
236
|
+
--seamless-surface-raised: #ffffff;
|
|
237
|
+
--seamless-text: #1a1a1a;
|
|
238
|
+
--seamless-text-muted: #4b5563;
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Under a Content Security Policy, pass `cspNonce`, or set `injectStyles: false` and link
|
|
243
|
+
`@seamless-auth/svelte/seamless-auth.css` yourself.
|
|
244
|
+
|
|
245
|
+
## Server rendering
|
|
246
|
+
|
|
247
|
+
The session is meant for the browser. On a server it does not read the session or browser storage,
|
|
248
|
+
the screens start no requests, and `requireAuth` redirects. If you do render on a server, create the
|
|
249
|
+
session per request (never in a module shared between requests) and pass `initialSession` from your
|
|
250
|
+
adapter. Never forward the browser's cookies to the adapter's `/auth/users/me` from a server.
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
Apache-2.0. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { type AuthSessionActions, type AuthSessionState, type LoginMethod, type SeamlessAuthClient } from '@seamless-auth/client';
|
|
2
|
+
import type { SeamlessAuthConfig, SeamlessAuthPorts } from './config.js';
|
|
3
|
+
/**
|
|
4
|
+
* Session state and auth actions for a Svelte application, from
|
|
5
|
+
* `createSeamlessAuth`.
|
|
6
|
+
*
|
|
7
|
+
* A thin binding over the framework-agnostic session store in
|
|
8
|
+
* `@seamless-auth/client`: the store owns the state machine, and this mirrors
|
|
9
|
+
* it into runes, so every property reads reactively in a component or an
|
|
10
|
+
* `$effect`. Change the session through the actions.
|
|
11
|
+
*/
|
|
12
|
+
export declare class SeamlessAuth implements AuthSessionActions {
|
|
13
|
+
#private;
|
|
14
|
+
/** The platform ports the bundled screens go through. */
|
|
15
|
+
readonly ports: SeamlessAuthPorts;
|
|
16
|
+
/** The client behind the session, for calls the session does not wrap. */
|
|
17
|
+
readonly client: SeamlessAuthClient;
|
|
18
|
+
/** How the bundled screens add their stylesheet. */
|
|
19
|
+
readonly styles: {
|
|
20
|
+
inject: boolean;
|
|
21
|
+
nonce?: string;
|
|
22
|
+
};
|
|
23
|
+
/** Where the bundled screens go once someone is signed in. */
|
|
24
|
+
readonly signedInPath: string;
|
|
25
|
+
/** Where `requireAuth` sends someone who is signed out. */
|
|
26
|
+
readonly loginPath: string;
|
|
27
|
+
constructor(config: SeamlessAuthConfig);
|
|
28
|
+
/** Everything the session knows. Replaced wholesale on every change. */
|
|
29
|
+
get state(): AuthSessionState;
|
|
30
|
+
get user(): {
|
|
31
|
+
id: string;
|
|
32
|
+
email: string;
|
|
33
|
+
phone: string | null;
|
|
34
|
+
roles: string[];
|
|
35
|
+
lastLogin?: string | null | undefined;
|
|
36
|
+
activeOrganizationId?: string | null | undefined;
|
|
37
|
+
} | null;
|
|
38
|
+
get isAuthenticated(): boolean;
|
|
39
|
+
/** True until the session has been read. Guards wait on this. */
|
|
40
|
+
get loading(): boolean;
|
|
41
|
+
get credentials(): {
|
|
42
|
+
id: string;
|
|
43
|
+
createdAt: string;
|
|
44
|
+
counter: number;
|
|
45
|
+
backedup: boolean;
|
|
46
|
+
backedUp: boolean;
|
|
47
|
+
platform?: string | null | undefined;
|
|
48
|
+
browser?: string | null | undefined;
|
|
49
|
+
aaguid?: string | null | undefined;
|
|
50
|
+
transports?: ("hybrid" | "ble" | "internal" | "nfc" | "usb" | "cable" | "smart-card")[] | undefined;
|
|
51
|
+
deviceType?: "singleDevice" | "multiDevice" | undefined;
|
|
52
|
+
friendlyName?: string | null | undefined;
|
|
53
|
+
lastUsedAt?: string | null | undefined;
|
|
54
|
+
deviceInfo?: string | null | undefined;
|
|
55
|
+
prfCapable?: boolean | undefined;
|
|
56
|
+
}[];
|
|
57
|
+
get organizations(): {
|
|
58
|
+
id: string;
|
|
59
|
+
name: string;
|
|
60
|
+
slug: string;
|
|
61
|
+
createdByUserId: string | null;
|
|
62
|
+
metadata: Record<string, unknown> | null;
|
|
63
|
+
createdAt: string;
|
|
64
|
+
updatedAt: string;
|
|
65
|
+
membership?: {
|
|
66
|
+
id: string;
|
|
67
|
+
organizationId: string;
|
|
68
|
+
userId: string;
|
|
69
|
+
roles: string[];
|
|
70
|
+
scopes: string[];
|
|
71
|
+
createdAt: string;
|
|
72
|
+
updatedAt: string;
|
|
73
|
+
user?: {
|
|
74
|
+
id: string;
|
|
75
|
+
email: string;
|
|
76
|
+
phone: string | null;
|
|
77
|
+
roles: string[];
|
|
78
|
+
} | undefined;
|
|
79
|
+
} | undefined;
|
|
80
|
+
memberCount?: number | undefined;
|
|
81
|
+
retiredOAuthProviders?: string[] | undefined;
|
|
82
|
+
}[];
|
|
83
|
+
get activeOrganization(): {
|
|
84
|
+
id: string;
|
|
85
|
+
name: string;
|
|
86
|
+
slug: string;
|
|
87
|
+
createdByUserId: string | null;
|
|
88
|
+
metadata: Record<string, unknown> | null;
|
|
89
|
+
createdAt: string;
|
|
90
|
+
updatedAt: string;
|
|
91
|
+
membership?: {
|
|
92
|
+
id: string;
|
|
93
|
+
organizationId: string;
|
|
94
|
+
userId: string;
|
|
95
|
+
roles: string[];
|
|
96
|
+
scopes: string[];
|
|
97
|
+
createdAt: string;
|
|
98
|
+
updatedAt: string;
|
|
99
|
+
user?: {
|
|
100
|
+
id: string;
|
|
101
|
+
email: string;
|
|
102
|
+
phone: string | null;
|
|
103
|
+
roles: string[];
|
|
104
|
+
} | undefined;
|
|
105
|
+
} | undefined;
|
|
106
|
+
memberCount?: number | undefined;
|
|
107
|
+
retiredOAuthProviders?: string[] | undefined;
|
|
108
|
+
} | null;
|
|
109
|
+
get stepUpStatus(): {
|
|
110
|
+
fresh: boolean;
|
|
111
|
+
method: "webauthn" | "totp" | null;
|
|
112
|
+
verifiedAt: string | null;
|
|
113
|
+
expiresAt: string | null;
|
|
114
|
+
maxAgeSeconds: number;
|
|
115
|
+
} | null;
|
|
116
|
+
get hasSignedInBefore(): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Sign-in methods this instance has enabled, once `loadLoginMethods()` has
|
|
119
|
+
* answered. Null means unknown, never "none".
|
|
120
|
+
*/
|
|
121
|
+
get loginMethods(): ("passkey" | "magic_link" | "email_otp" | "phone_otp" | "oauth")[] | null;
|
|
122
|
+
get loginMethodsLoading(): boolean;
|
|
123
|
+
/** Whether this device can use a passkey, once `checkPasskeySupport()` has answered. */
|
|
124
|
+
get passkeySupported(): boolean;
|
|
125
|
+
get passkeySupportLoading(): boolean;
|
|
126
|
+
/** Reads the instance's sign-in methods once and caches the answer. */
|
|
127
|
+
loadLoginMethods(): Promise<LoginMethod[] | null>;
|
|
128
|
+
/** Detects passkey support once and caches the answer. */
|
|
129
|
+
checkPasskeySupport(): Promise<boolean>;
|
|
130
|
+
/**
|
|
131
|
+
* Resolves with the state once the session has been read. During a server
|
|
132
|
+
* render the session is never read there, so this resolves at once with what
|
|
133
|
+
* the server knows. It also resolves if the session is destroyed first.
|
|
134
|
+
*/
|
|
135
|
+
whenSettled(): Promise<AuthSessionState>;
|
|
136
|
+
/**
|
|
137
|
+
* A fetch for the application's own API that carries the session cookies.
|
|
138
|
+
* A path resolves on `apiHost`. Only `apiHost` and `trustedOrigins` receive
|
|
139
|
+
* the session; any other origin rejects with `UntrustedOriginError`.
|
|
140
|
+
*/
|
|
141
|
+
authorizedFetch: SeamlessAuthClient['authorizedFetch'];
|
|
142
|
+
login: AuthSessionActions['login'];
|
|
143
|
+
handlePasskeyLogin: AuthSessionActions['handlePasskeyLogin'];
|
|
144
|
+
registerPasskey: AuthSessionActions['registerPasskey'];
|
|
145
|
+
refreshSession: AuthSessionActions['refreshSession'];
|
|
146
|
+
logout: AuthSessionActions['logout'];
|
|
147
|
+
logoutAllSessions: AuthSessionActions['logoutAllSessions'];
|
|
148
|
+
deleteUser: AuthSessionActions['deleteUser'];
|
|
149
|
+
updateCredential: AuthSessionActions['updateCredential'];
|
|
150
|
+
deleteCredential: AuthSessionActions['deleteCredential'];
|
|
151
|
+
switchOrganization: AuthSessionActions['switchOrganization'];
|
|
152
|
+
listOAuthProviders: AuthSessionActions['listOAuthProviders'];
|
|
153
|
+
startOAuthLogin: AuthSessionActions['startOAuthLogin'];
|
|
154
|
+
finishOAuthLogin: AuthSessionActions['finishOAuthLogin'];
|
|
155
|
+
refreshStepUpStatus: AuthSessionActions['refreshStepUpStatus'];
|
|
156
|
+
verifyStepUpWithPasskey: AuthSessionActions['verifyStepUpWithPasskey'];
|
|
157
|
+
verifyStepUpWithPasskeyPrf: AuthSessionActions['verifyStepUpWithPasskeyPrf'];
|
|
158
|
+
verifyStepUpWithTotp: AuthSessionActions['verifyStepUpWithTotp'];
|
|
159
|
+
hasRole: AuthSessionActions['hasRole'];
|
|
160
|
+
hasScopedRole: AuthSessionActions['hasScopedRole'];
|
|
161
|
+
markSignedIn: AuthSessionActions['markSignedIn'];
|
|
162
|
+
/** Stops the session. Pending `whenSettled()` calls resolve with the last state. */
|
|
163
|
+
destroy(): void;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Creates the session for an application. In the browser it is read straight
|
|
167
|
+
* away. Create one per application (one per request when rendering on a
|
|
168
|
+
* server), and hand it to the screens with `setSeamlessAuth`.
|
|
169
|
+
*/
|
|
170
|
+
export declare function createSeamlessAuth(config: SeamlessAuthConfig): SeamlessAuth;
|