@auth/solid-start 0.6.0 → 0.7.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/adapters.d.ts +2 -0
- package/adapters.d.ts.map +1 -0
- package/adapters.js +1 -0
- package/client.d.ts +6 -7
- package/client.d.ts.map +1 -0
- package/client.js +78 -60
- package/index.d.ts +10 -15
- package/index.d.ts.map +1 -0
- package/index.js +239 -54
- package/package.json +8 -9
- package/src/index.ts +4 -6
package/adapters.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapters.d.ts","sourceRoot":"","sources":["src/adapters.ts"],"names":[],"mappings":"AAAA,mBAAmB,qBAAqB,CAAA"}
|
package/adapters.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/client.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type { BuiltInProviderType, RedirectableProviderType } from "@auth/core/providers";
|
|
3
2
|
type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);
|
|
4
3
|
interface SignInOptions extends Record<string, unknown> {
|
|
5
4
|
/**
|
|
@@ -18,7 +17,7 @@ interface SignOutParams<R extends boolean = true> {
|
|
|
18
17
|
redirect?: R;
|
|
19
18
|
}
|
|
20
19
|
/** Match `inputType` of `new URLSearchParams(inputType)` */
|
|
21
|
-
type SignInAuthorizationParams = string | string[][] | Record<string, string> | URLSearchParams;
|
|
20
|
+
export type SignInAuthorizationParams = string | string[][] | Record<string, string> | URLSearchParams;
|
|
22
21
|
/**
|
|
23
22
|
* Client-side method to initiate a signin flow
|
|
24
23
|
* or send the user to the signin page listing all possible providers.
|
|
@@ -30,7 +29,7 @@ type SignInAuthorizationParams = string | string[][] | Record<string, string> |
|
|
|
30
29
|
* signIn("provider") // example: signIn("github")
|
|
31
30
|
* ```
|
|
32
31
|
*/
|
|
33
|
-
declare function signIn<P extends RedirectableProviderType | undefined = undefined>(providerId?: LiteralUnion<P extends RedirectableProviderType ? P | BuiltInProviderType : BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorizationParams): Promise<Response | undefined>;
|
|
32
|
+
export declare function signIn<P extends RedirectableProviderType | undefined = undefined>(providerId?: LiteralUnion<P extends RedirectableProviderType ? P | BuiltInProviderType : BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorizationParams): Promise<Response | undefined>;
|
|
34
33
|
/**
|
|
35
34
|
* Signs the user out, by removing the session cookie.
|
|
36
35
|
* Automatically adds the CSRF token to the request.
|
|
@@ -40,6 +39,6 @@ declare function signIn<P extends RedirectableProviderType | undefined = undefin
|
|
|
40
39
|
* signOut()
|
|
41
40
|
* ```
|
|
42
41
|
*/
|
|
43
|
-
declare function signOut(options?: SignOutParams): Promise<void>;
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
export declare function signOut(options?: SignOutParams): Promise<void>;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=client.d.ts.map
|
package/client.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACzB,MAAM,sBAAsB,CAAA;AAE7B,KAAK,YAAY,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAA;AAE3E,UAAU,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACrD;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,uGAAuG;IACvG,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,UAAU,aAAa,CAAC,CAAC,SAAS,OAAO,GAAG,IAAI;IAC9C,kGAAkG;IAClG,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,wGAAwG;IACxG,QAAQ,CAAC,EAAE,CAAC,CAAA;CACb;AAED,4DAA4D;AAC5D,MAAM,MAAM,yBAAyB,GACjC,MAAM,GACN,MAAM,EAAE,EAAE,GACV,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACtB,eAAe,CAAA;AAEnB;;;;;;;;;;GAUG;AACH,wBAAsB,MAAM,CAC1B,CAAC,SAAS,wBAAwB,GAAG,SAAS,GAAG,SAAS,EAE1D,UAAU,CAAC,EAAE,YAAY,CACvB,CAAC,SAAS,wBAAwB,GAC9B,CAAC,GAAG,mBAAmB,GACvB,mBAAmB,CACxB,EACD,OAAO,CAAC,EAAE,aAAa,EACvB,mBAAmB,CAAC,EAAE,yBAAyB,iCA4ChD;AAED;;;;;;;;GAQG;AACH,wBAAsB,OAAO,CAAC,OAAO,CAAC,EAAE,aAAa,iBAsBpD"}
|
package/client.js
CHANGED
|
@@ -1,62 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Client-side method to initiate a signin flow
|
|
3
|
+
* or send the user to the signin page listing all possible providers.
|
|
4
|
+
* Automatically adds the CSRF token to the request.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { signIn } from "@auth/solid-start/client"
|
|
8
|
+
* signIn()
|
|
9
|
+
* signIn("provider") // example: signIn("github")
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export async function signIn(providerId, options, authorizationParams) {
|
|
13
|
+
const { callbackUrl = window.location.href, redirect = true } = options ?? {};
|
|
14
|
+
// TODO: Support custom providers
|
|
15
|
+
const isCredentials = providerId === "credentials";
|
|
16
|
+
const isEmail = providerId === "email";
|
|
17
|
+
const isSupportingReturn = isCredentials || isEmail;
|
|
18
|
+
// TODO: Handle custom base path
|
|
19
|
+
const signInUrl = `/api/auth/${isCredentials ? "callback" : "signin"}/${providerId}`;
|
|
20
|
+
const _signInUrl = `${signInUrl}?${new URLSearchParams(authorizationParams)}`;
|
|
21
|
+
// TODO: Handle custom base path
|
|
22
|
+
const csrfTokenResponse = await fetch("/api/auth/csrf");
|
|
23
|
+
const { csrfToken } = await csrfTokenResponse.json();
|
|
24
|
+
const res = await fetch(_signInUrl, {
|
|
25
|
+
method: "post",
|
|
26
|
+
headers: {
|
|
27
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
28
|
+
"X-Auth-Return-Redirect": "1",
|
|
29
|
+
},
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
body: new URLSearchParams({
|
|
32
|
+
...options,
|
|
33
|
+
csrfToken,
|
|
34
|
+
callbackUrl,
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
const data = await res.clone().json();
|
|
38
|
+
const error = new URL(data.url).searchParams.get("error");
|
|
39
|
+
if (redirect || !isSupportingReturn || !error) {
|
|
40
|
+
// TODO: Do not redirect for Credentials and Email providers by default in next major
|
|
41
|
+
window.location.href = data.url ?? data.redirect ?? callbackUrl;
|
|
42
|
+
// If url contains a hash, the browser does not reload the page. We reload manually
|
|
43
|
+
if (data.url.includes("#"))
|
|
44
|
+
window.location.reload();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
return res;
|
|
35
48
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Signs the user out, by removing the session cookie.
|
|
51
|
+
* Automatically adds the CSRF token to the request.
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { signOut } from "@auth/solid-start/client"
|
|
55
|
+
* signOut()
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export async function signOut(options) {
|
|
59
|
+
const { callbackUrl = window.location.href } = options ?? {};
|
|
60
|
+
// TODO: Custom base path
|
|
61
|
+
const csrfTokenResponse = await fetch("/api/auth/csrf");
|
|
62
|
+
const { csrfToken } = await csrfTokenResponse.json();
|
|
63
|
+
const res = await fetch(`/api/auth/signout`, {
|
|
64
|
+
method: "post",
|
|
65
|
+
headers: {
|
|
66
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
67
|
+
"X-Auth-Return-Redirect": "1",
|
|
68
|
+
},
|
|
69
|
+
body: new URLSearchParams({
|
|
70
|
+
csrfToken,
|
|
71
|
+
callbackUrl,
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
const data = await res.json();
|
|
75
|
+
const url = data.url ?? data.redirect ?? callbackUrl;
|
|
76
|
+
window.location.href = url;
|
|
77
|
+
// If url contains a hash, the browser does not reload the page. We reload manually
|
|
78
|
+
if (url.includes("#"))
|
|
79
|
+
window.location.reload();
|
|
57
80
|
}
|
|
58
|
-
__name(signOut, "signOut");
|
|
59
|
-
export {
|
|
60
|
-
signIn,
|
|
61
|
-
signOut
|
|
62
|
-
};
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { AuthConfig, Session } from '@auth/core/types';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
*
|
|
5
3
|
* :::warning
|
|
@@ -15,12 +13,10 @@ import { AuthConfig, Session } from '@auth/core/types';
|
|
|
15
13
|
* npm install @auth/core @auth/solid-start
|
|
16
14
|
* ```
|
|
17
15
|
*
|
|
18
|
-
* We recommended to using [create-jd-app](https://github.com/OrJDev/create-jd-app)
|
|
19
|
-
*
|
|
20
16
|
* @module @auth/solid-start
|
|
21
17
|
*/
|
|
22
|
-
|
|
23
|
-
interface SolidAuthConfig extends AuthConfig {
|
|
18
|
+
import type { AuthConfig, Session } from "@auth/core/types";
|
|
19
|
+
export interface SolidAuthConfig extends AuthConfig {
|
|
24
20
|
/**
|
|
25
21
|
* Defines the base path for the auth routes.
|
|
26
22
|
* @default '/api/auth'
|
|
@@ -38,7 +34,7 @@ interface SolidAuthConfig extends AuthConfig {
|
|
|
38
34
|
*
|
|
39
35
|
* ## Creating the API handler
|
|
40
36
|
*
|
|
41
|
-
*
|
|
37
|
+
* This example uses github, make sure to set the following environment variables:
|
|
42
38
|
*
|
|
43
39
|
* ```
|
|
44
40
|
* GITHUB_ID=your_github_oauth_id
|
|
@@ -88,7 +84,7 @@ interface SolidAuthConfig extends AuthConfig {
|
|
|
88
84
|
*
|
|
89
85
|
* ### When Using SSR
|
|
90
86
|
*
|
|
91
|
-
* When using SSR,
|
|
87
|
+
* When using SSR, it is recommended to create a `Protected` component that will trigger suspense using the `Show` component. It should look like this:
|
|
92
88
|
*
|
|
93
89
|
*
|
|
94
90
|
* ```tsx
|
|
@@ -152,7 +148,7 @@ interface SolidAuthConfig extends AuthConfig {
|
|
|
152
148
|
*
|
|
153
149
|
* ### When Using CSR
|
|
154
150
|
*
|
|
155
|
-
* When using CSR, the `Protected` component will not work as expected and will cause the screen to flash
|
|
151
|
+
* When using CSR, the `Protected` component will not work as expected and will cause the screen to flash. To fix this, a Solid-Start middleware is used:
|
|
156
152
|
*
|
|
157
153
|
* ```tsx
|
|
158
154
|
* // entry-server.tsx
|
|
@@ -184,7 +180,7 @@ interface SolidAuthConfig extends AuthConfig {
|
|
|
184
180
|
* );
|
|
185
181
|
* ```
|
|
186
182
|
*
|
|
187
|
-
* And now
|
|
183
|
+
* And now a protected route can be created:
|
|
188
184
|
*
|
|
189
185
|
*
|
|
190
186
|
* ```tsx
|
|
@@ -203,11 +199,10 @@ interface SolidAuthConfig extends AuthConfig {
|
|
|
203
199
|
* :::
|
|
204
200
|
*
|
|
205
201
|
*/
|
|
206
|
-
declare function SolidAuth(config: SolidAuthConfig): {
|
|
202
|
+
export declare function SolidAuth(config: SolidAuthConfig): {
|
|
207
203
|
GET(event: any): Promise<Response | undefined>;
|
|
208
204
|
POST(event: any): Promise<Response | undefined>;
|
|
209
205
|
};
|
|
210
|
-
type GetSessionResult = Promise<Session | null>;
|
|
211
|
-
declare function getSession(req: Request, options: Omit<AuthConfig, "raw">): GetSessionResult;
|
|
212
|
-
|
|
213
|
-
export { GetSessionResult, SolidAuth, SolidAuthConfig, getSession };
|
|
206
|
+
export type GetSessionResult = Promise<Session | null>;
|
|
207
|
+
export declare function getSession(req: Request, options: Omit<AuthConfig, "raw">): GetSessionResult;
|
|
208
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,KAAK,EAAc,UAAU,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAEvE,MAAM,WAAW,eAAgB,SAAQ,UAAU;IACjD;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AA6BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+KG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,eAAe;eAU5B,GAAG;gBAGF,GAAG;EAIxB;AAED,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAAA;AAEtD,wBAAsB,UAAU,CAC9B,GAAG,EAAE,OAAO,EACZ,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,GAC/B,gBAAgB,CAiBlB"}
|
package/index.js
CHANGED
|
@@ -1,61 +1,246 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* :::warning
|
|
4
|
+
* `@auth/solid-start` is currently experimental. The API _will_ change in the future.
|
|
5
|
+
* :::
|
|
6
|
+
*
|
|
7
|
+
* SolidStart Auth is the official SolidStart integration for Auth.js.
|
|
8
|
+
* It provides a simple way to add authentication to your SolidStart app in a few lines of code.
|
|
9
|
+
*
|
|
10
|
+
* ## Installation
|
|
11
|
+
*
|
|
12
|
+
* ```bash npm2yarn
|
|
13
|
+
* npm install @auth/core @auth/solid-start
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @module @auth/solid-start
|
|
17
|
+
*/
|
|
4
18
|
import { Auth } from "@auth/core";
|
|
5
19
|
const actions = [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
"providers",
|
|
21
|
+
"session",
|
|
22
|
+
"csrf",
|
|
23
|
+
"signin",
|
|
24
|
+
"signout",
|
|
25
|
+
"callback",
|
|
26
|
+
"verify-request",
|
|
27
|
+
"error",
|
|
14
28
|
];
|
|
15
29
|
function SolidAuthHandler(prefix, authOptions) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
30
|
+
return async (event) => {
|
|
31
|
+
const { request } = event;
|
|
32
|
+
const url = new URL(request.url);
|
|
33
|
+
const action = url.pathname
|
|
34
|
+
.slice(prefix.length + 1)
|
|
35
|
+
.split("/")[0];
|
|
36
|
+
if (!actions.includes(action) || !url.pathname.startsWith(prefix + "/")) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
return await Auth(request, authOptions);
|
|
40
|
+
};
|
|
25
41
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
/**
|
|
43
|
+
* ## Setup
|
|
44
|
+
*
|
|
45
|
+
* [Generate an auth secret](https://generate-secret.vercel.app/32), then set it as an environment variable:
|
|
46
|
+
*
|
|
47
|
+
* ```
|
|
48
|
+
* AUTH_SECRET=your_auth_secret
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* ## Creating the API handler
|
|
52
|
+
*
|
|
53
|
+
* This example uses github, make sure to set the following environment variables:
|
|
54
|
+
*
|
|
55
|
+
* ```
|
|
56
|
+
* GITHUB_ID=your_github_oauth_id
|
|
57
|
+
* GITHUB_SECRET=your_github_oauth_secret
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* // routes/api/auth/[...solidauth].ts
|
|
62
|
+
* import { SolidAuth, type SolidAuthConfig } from "@auth/solid-start"
|
|
63
|
+
* import GitHub from "@auth/core/providers/github"
|
|
64
|
+
*
|
|
65
|
+
* export const authOpts: SolidAuthConfig = {
|
|
66
|
+
* providers: [
|
|
67
|
+
* GitHub({
|
|
68
|
+
* clientId: process.env.GITHUB_ID,
|
|
69
|
+
* clientSecret: process.env.GITHUB_SECRET,
|
|
70
|
+
* }),
|
|
71
|
+
* ],
|
|
72
|
+
* debug: false,
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* export const { GET, POST } = SolidAuth(authOpts)
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* ## Getting the current session
|
|
79
|
+
*
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { getSession } from "@auth/solid-start"
|
|
82
|
+
* import { createServerData$ } from "solid-start/server"
|
|
83
|
+
* import { authOpts } from "~/routes/api/auth/[...solidauth]"
|
|
84
|
+
*
|
|
85
|
+
* export const useSession = () => {
|
|
86
|
+
* return createServerData$(
|
|
87
|
+
* async (_, { request }) => {
|
|
88
|
+
* return await getSession(request, authOpts)
|
|
89
|
+
* },
|
|
90
|
+
* { key: () => ["auth_user"] }
|
|
91
|
+
* )
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* // useSession returns a resource:
|
|
95
|
+
* const session = useSession()
|
|
96
|
+
* const loading = session.loading
|
|
97
|
+
* const user = () => session()?.user
|
|
98
|
+
* ```
|
|
99
|
+
* ## Protected Routes
|
|
100
|
+
*
|
|
101
|
+
* ### When Using SSR
|
|
102
|
+
*
|
|
103
|
+
* When using SSR, it is recommended to create a `Protected` component that will trigger suspense using the `Show` component. It should look like this:
|
|
104
|
+
*
|
|
105
|
+
*
|
|
106
|
+
* ```tsx
|
|
107
|
+
* // components/Protected.tsx
|
|
108
|
+
* import { type Session } from "@auth/core/types";
|
|
109
|
+
* import { getSession } from "@auth/solid-start";
|
|
110
|
+
* import { Component, Show } from "solid-js";
|
|
111
|
+
* import { useRouteData } from "solid-start";
|
|
112
|
+
* import { createServerData$, redirect } from "solid-start/server";
|
|
113
|
+
* import { authOpts } from "~/routes/api/auth/[...solidauth]";
|
|
114
|
+
*
|
|
115
|
+
* const Protected = (Comp: IProtectedComponent) => {
|
|
116
|
+
* const routeData = () => {
|
|
117
|
+
* return createServerData$(
|
|
118
|
+
* async (_, event) => {
|
|
119
|
+
* const session = await getSession(event.request, authOpts);
|
|
120
|
+
* if (!session || !session.user) {
|
|
121
|
+
* throw redirect("/");
|
|
122
|
+
* }
|
|
123
|
+
* return session;
|
|
124
|
+
* },
|
|
125
|
+
* { key: () => ["auth_user"] }
|
|
126
|
+
* );
|
|
127
|
+
* };
|
|
128
|
+
*
|
|
129
|
+
* return {
|
|
130
|
+
* routeData,
|
|
131
|
+
* Page: () => {
|
|
132
|
+
* const session = useRouteData<typeof routeData>();
|
|
133
|
+
* return (
|
|
134
|
+
* <Show when={session()} keyed>
|
|
135
|
+
* {(sess) => <Comp {...sess} />}
|
|
136
|
+
* </Show>
|
|
137
|
+
* );
|
|
138
|
+
* },
|
|
139
|
+
* };
|
|
140
|
+
* };
|
|
141
|
+
*
|
|
142
|
+
* type IProtectedComponent = Component<Session>;
|
|
143
|
+
*
|
|
144
|
+
* export default Protected;
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* It can be used like this:
|
|
148
|
+
*
|
|
149
|
+
*
|
|
150
|
+
* ```tsx
|
|
151
|
+
* // routes/protected.tsx
|
|
152
|
+
* import Protected from "~/components/Protected";
|
|
153
|
+
*
|
|
154
|
+
* export const { routeData, Page } = Protected((session) => {
|
|
155
|
+
* return (
|
|
156
|
+
* <main class="flex flex-col gap-2 items-center">
|
|
157
|
+
* <h1>This is a protected route</h1>
|
|
158
|
+
* </main>
|
|
159
|
+
* );
|
|
160
|
+
* });
|
|
161
|
+
*
|
|
162
|
+
* export default Page;
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* ### When Using CSR
|
|
166
|
+
*
|
|
167
|
+
* When using CSR, the `Protected` component will not work as expected and will cause the screen to flash. To fix this, a Solid-Start middleware is used:
|
|
168
|
+
*
|
|
169
|
+
* ```tsx
|
|
170
|
+
* // entry-server.tsx
|
|
171
|
+
* import { Session } from "@auth/core";
|
|
172
|
+
* import { getSession } from "@auth/solid-start";
|
|
173
|
+
* import { redirect } from "solid-start";
|
|
174
|
+
* import {
|
|
175
|
+
* StartServer,
|
|
176
|
+
* createHandler,
|
|
177
|
+
* renderAsync,
|
|
178
|
+
* } from "solid-start/entry-server";
|
|
179
|
+
* import { authOpts } from "./routes/api/auth/[...solidauth]";
|
|
180
|
+
*
|
|
181
|
+
* const protectedPaths = ["/protected"]; // add any route you wish in here
|
|
182
|
+
*
|
|
183
|
+
* export default createHandler(
|
|
184
|
+
* ({ forward }) => {
|
|
185
|
+
* return async (event) => {
|
|
186
|
+
* if (protectedPaths.includes(new URL(event.request.url).pathname)) {
|
|
187
|
+
* const session = await getSession(event.request, authOpts);
|
|
188
|
+
* if (!session) {
|
|
189
|
+
* return redirect("/");
|
|
190
|
+
* }
|
|
191
|
+
* }
|
|
192
|
+
* return forward(event);
|
|
193
|
+
* };
|
|
194
|
+
* },
|
|
195
|
+
* renderAsync((event) => <StartServer event={event} />)
|
|
196
|
+
* );
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* And now a protected route can be created:
|
|
200
|
+
*
|
|
201
|
+
*
|
|
202
|
+
* ```tsx
|
|
203
|
+
* // routes/protected.tsx
|
|
204
|
+
* export default () => {
|
|
205
|
+
* return (
|
|
206
|
+
* <main class="flex flex-col gap-2 items-center">
|
|
207
|
+
* <h1>This is a protected route</h1>
|
|
208
|
+
* </main>
|
|
209
|
+
* );
|
|
210
|
+
* };
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* :::note
|
|
214
|
+
* The CSR method should also work when using SSR, the SSR method shouldn't work when using CSR
|
|
215
|
+
* :::
|
|
216
|
+
*
|
|
217
|
+
*/
|
|
218
|
+
export function SolidAuth(config) {
|
|
219
|
+
const { prefix = "/api/auth", ...authOptions } = config;
|
|
220
|
+
authOptions.secret ?? (authOptions.secret = process.env.AUTH_SECRET);
|
|
221
|
+
authOptions.trustHost ?? (authOptions.trustHost = !!(process.env.AUTH_TRUST_HOST ??
|
|
222
|
+
process.env.VERCEL ??
|
|
223
|
+
process.env.NODE_ENV !== "production"));
|
|
224
|
+
const handler = SolidAuthHandler(prefix, authOptions);
|
|
225
|
+
return {
|
|
226
|
+
async GET(event) {
|
|
227
|
+
return await handler(event);
|
|
228
|
+
},
|
|
229
|
+
async POST(event) {
|
|
230
|
+
return await handler(event);
|
|
231
|
+
},
|
|
232
|
+
};
|
|
40
233
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (status === 200)
|
|
54
|
-
return data;
|
|
55
|
-
throw new Error(data.message);
|
|
234
|
+
export async function getSession(req, options) {
|
|
235
|
+
options.secret ?? (options.secret = process.env.AUTH_SECRET);
|
|
236
|
+
options.trustHost ?? (options.trustHost = true);
|
|
237
|
+
const url = new URL("/api/auth/session", req.url);
|
|
238
|
+
const response = await Auth(new Request(url, { headers: req.headers }), options);
|
|
239
|
+
const { status = 200 } = response;
|
|
240
|
+
const data = await response.json();
|
|
241
|
+
if (!data || !Object.keys(data).length)
|
|
242
|
+
return null;
|
|
243
|
+
if (status === 200)
|
|
244
|
+
return data;
|
|
245
|
+
throw new Error(data.message);
|
|
56
246
|
}
|
|
57
|
-
__name(getSession, "getSession");
|
|
58
|
-
export {
|
|
59
|
-
SolidAuth,
|
|
60
|
-
getSession
|
|
61
|
-
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auth/solid-start",
|
|
3
3
|
"description": "Authentication for SolidStart.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"*.js",
|
|
8
|
+
"*.d.ts*",
|
|
9
9
|
"src"
|
|
10
10
|
],
|
|
11
11
|
"exports": {
|
|
@@ -26,14 +26,13 @@
|
|
|
26
26
|
"access": "public"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@auth/core": "0.
|
|
29
|
+
"@auth/core": "0.28.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@solidjs/meta": "^0.28.0",
|
|
33
33
|
"@types/node": "^18.7.14",
|
|
34
34
|
"solid-js": "^1.5.7",
|
|
35
|
-
"solid-start": "^0.2.14"
|
|
36
|
-
"tsup": "^6.5.0"
|
|
35
|
+
"solid-start": "^0.2.14"
|
|
37
36
|
},
|
|
38
37
|
"peerDependencies": {
|
|
39
38
|
"solid-js": "^1.5.7",
|
|
@@ -48,9 +47,9 @@
|
|
|
48
47
|
"repository": "https://github.com/nextauthjs/next-auth",
|
|
49
48
|
"license": "ISC",
|
|
50
49
|
"scripts": {
|
|
51
|
-
"build": "
|
|
50
|
+
"build": "tsc",
|
|
52
51
|
"patch": "npm version patch --no-git-tag-version",
|
|
53
|
-
"test": "vitest -c ../utils/vitest.config.ts",
|
|
54
|
-
"clean": "rm -rf
|
|
52
|
+
"test": "vitest run -c ../utils/vitest.config.ts",
|
|
53
|
+
"clean": "rm -rf *.js *.d.ts*"
|
|
55
54
|
}
|
|
56
55
|
}
|
package/src/index.ts
CHANGED
|
@@ -13,8 +13,6 @@
|
|
|
13
13
|
* npm install @auth/core @auth/solid-start
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
16
|
-
* We recommended to using [create-jd-app](https://github.com/OrJDev/create-jd-app)
|
|
17
|
-
*
|
|
18
16
|
* @module @auth/solid-start
|
|
19
17
|
*/
|
|
20
18
|
|
|
@@ -67,7 +65,7 @@ function SolidAuthHandler(prefix: string, authOptions: SolidAuthConfig) {
|
|
|
67
65
|
*
|
|
68
66
|
* ## Creating the API handler
|
|
69
67
|
*
|
|
70
|
-
*
|
|
68
|
+
* This example uses github, make sure to set the following environment variables:
|
|
71
69
|
*
|
|
72
70
|
* ```
|
|
73
71
|
* GITHUB_ID=your_github_oauth_id
|
|
@@ -117,7 +115,7 @@ function SolidAuthHandler(prefix: string, authOptions: SolidAuthConfig) {
|
|
|
117
115
|
*
|
|
118
116
|
* ### When Using SSR
|
|
119
117
|
*
|
|
120
|
-
* When using SSR,
|
|
118
|
+
* When using SSR, it is recommended to create a `Protected` component that will trigger suspense using the `Show` component. It should look like this:
|
|
121
119
|
*
|
|
122
120
|
*
|
|
123
121
|
* ```tsx
|
|
@@ -181,7 +179,7 @@ function SolidAuthHandler(prefix: string, authOptions: SolidAuthConfig) {
|
|
|
181
179
|
*
|
|
182
180
|
* ### When Using CSR
|
|
183
181
|
*
|
|
184
|
-
* When using CSR, the `Protected` component will not work as expected and will cause the screen to flash
|
|
182
|
+
* When using CSR, the `Protected` component will not work as expected and will cause the screen to flash. To fix this, a Solid-Start middleware is used:
|
|
185
183
|
*
|
|
186
184
|
* ```tsx
|
|
187
185
|
* // entry-server.tsx
|
|
@@ -213,7 +211,7 @@ function SolidAuthHandler(prefix: string, authOptions: SolidAuthConfig) {
|
|
|
213
211
|
* );
|
|
214
212
|
* ```
|
|
215
213
|
*
|
|
216
|
-
* And now
|
|
214
|
+
* And now a protected route can be created:
|
|
217
215
|
*
|
|
218
216
|
*
|
|
219
217
|
* ```tsx
|