@auth/solid-start 0.6.0 → 0.6.1
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 +6 -9
- package/index.d.ts.map +1 -0
- package/index.js +241 -54
- package/package.json +6 -7
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
|
|
@@ -19,8 +17,8 @@ import { AuthConfig, Session } from '@auth/core/types';
|
|
|
19
17
|
*
|
|
20
18
|
* @module @auth/solid-start
|
|
21
19
|
*/
|
|
22
|
-
|
|
23
|
-
interface SolidAuthConfig extends AuthConfig {
|
|
20
|
+
import type { AuthConfig, Session } from "@auth/core/types";
|
|
21
|
+
export interface SolidAuthConfig extends AuthConfig {
|
|
24
22
|
/**
|
|
25
23
|
* Defines the base path for the auth routes.
|
|
26
24
|
* @default '/api/auth'
|
|
@@ -203,11 +201,10 @@ interface SolidAuthConfig extends AuthConfig {
|
|
|
203
201
|
* :::
|
|
204
202
|
*
|
|
205
203
|
*/
|
|
206
|
-
declare function SolidAuth(config: SolidAuthConfig): {
|
|
204
|
+
export declare function SolidAuth(config: SolidAuthConfig): {
|
|
207
205
|
GET(event: any): Promise<Response | undefined>;
|
|
208
206
|
POST(event: any): Promise<Response | undefined>;
|
|
209
207
|
};
|
|
210
|
-
type GetSessionResult = Promise<Session | null>;
|
|
211
|
-
declare function getSession(req: Request, options: Omit<AuthConfig, "raw">): GetSessionResult;
|
|
212
|
-
|
|
213
|
-
export { GetSessionResult, SolidAuth, SolidAuthConfig, getSession };
|
|
208
|
+
export type GetSessionResult = Promise<Session | null>;
|
|
209
|
+
export declare function getSession(req: Request, options: Omit<AuthConfig, "raw">): GetSessionResult;
|
|
210
|
+
//# 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;;;;;;;;;;;;;;;;;;GAkBG;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,248 @@
|
|
|
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
|
+
* We recommended to using [create-jd-app](https://github.com/OrJDev/create-jd-app)
|
|
17
|
+
*
|
|
18
|
+
* @module @auth/solid-start
|
|
19
|
+
*/
|
|
4
20
|
import { Auth } from "@auth/core";
|
|
5
21
|
const actions = [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
22
|
+
"providers",
|
|
23
|
+
"session",
|
|
24
|
+
"csrf",
|
|
25
|
+
"signin",
|
|
26
|
+
"signout",
|
|
27
|
+
"callback",
|
|
28
|
+
"verify-request",
|
|
29
|
+
"error",
|
|
14
30
|
];
|
|
15
31
|
function SolidAuthHandler(prefix, authOptions) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
return async (event) => {
|
|
33
|
+
const { request } = event;
|
|
34
|
+
const url = new URL(request.url);
|
|
35
|
+
const action = url.pathname
|
|
36
|
+
.slice(prefix.length + 1)
|
|
37
|
+
.split("/")[0];
|
|
38
|
+
if (!actions.includes(action) || !url.pathname.startsWith(prefix + "/")) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return await Auth(request, authOptions);
|
|
42
|
+
};
|
|
25
43
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
/**
|
|
45
|
+
* ## Setup
|
|
46
|
+
*
|
|
47
|
+
* [Generate an auth secret](https://generate-secret.vercel.app/32), then set it as an environment variable:
|
|
48
|
+
*
|
|
49
|
+
* ```
|
|
50
|
+
* AUTH_SECRET=your_auth_secret
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* ## Creating the API handler
|
|
54
|
+
*
|
|
55
|
+
* in this example we are using github so make sure to set the following environment variables:
|
|
56
|
+
*
|
|
57
|
+
* ```
|
|
58
|
+
* GITHUB_ID=your_github_oauth_id
|
|
59
|
+
* GITHUB_SECRET=your_github_oauth_secret
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* // routes/api/auth/[...solidauth].ts
|
|
64
|
+
* import { SolidAuth, type SolidAuthConfig } from "@auth/solid-start"
|
|
65
|
+
* import GitHub from "@auth/core/providers/github"
|
|
66
|
+
*
|
|
67
|
+
* export const authOpts: SolidAuthConfig = {
|
|
68
|
+
* providers: [
|
|
69
|
+
* GitHub({
|
|
70
|
+
* clientId: process.env.GITHUB_ID,
|
|
71
|
+
* clientSecret: process.env.GITHUB_SECRET,
|
|
72
|
+
* }),
|
|
73
|
+
* ],
|
|
74
|
+
* debug: false,
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* export const { GET, POST } = SolidAuth(authOpts)
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* ## Getting the current session
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { getSession } from "@auth/solid-start"
|
|
84
|
+
* import { createServerData$ } from "solid-start/server"
|
|
85
|
+
* import { authOpts } from "~/routes/api/auth/[...solidauth]"
|
|
86
|
+
*
|
|
87
|
+
* export const useSession = () => {
|
|
88
|
+
* return createServerData$(
|
|
89
|
+
* async (_, { request }) => {
|
|
90
|
+
* return await getSession(request, authOpts)
|
|
91
|
+
* },
|
|
92
|
+
* { key: () => ["auth_user"] }
|
|
93
|
+
* )
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* // useSession returns a resource:
|
|
97
|
+
* const session = useSession()
|
|
98
|
+
* const loading = session.loading
|
|
99
|
+
* const user = () => session()?.user
|
|
100
|
+
* ```
|
|
101
|
+
* ## Protected Routes
|
|
102
|
+
*
|
|
103
|
+
* ### When Using SSR
|
|
104
|
+
*
|
|
105
|
+
* When using SSR, I recommend creating a `Protected` component that will trigger suspense using the `Show` component. It should look like this:
|
|
106
|
+
*
|
|
107
|
+
*
|
|
108
|
+
* ```tsx
|
|
109
|
+
* // components/Protected.tsx
|
|
110
|
+
* import { type Session } from "@auth/core/types";
|
|
111
|
+
* import { getSession } from "@auth/solid-start";
|
|
112
|
+
* import { Component, Show } from "solid-js";
|
|
113
|
+
* import { useRouteData } from "solid-start";
|
|
114
|
+
* import { createServerData$, redirect } from "solid-start/server";
|
|
115
|
+
* import { authOpts } from "~/routes/api/auth/[...solidauth]";
|
|
116
|
+
*
|
|
117
|
+
* const Protected = (Comp: IProtectedComponent) => {
|
|
118
|
+
* const routeData = () => {
|
|
119
|
+
* return createServerData$(
|
|
120
|
+
* async (_, event) => {
|
|
121
|
+
* const session = await getSession(event.request, authOpts);
|
|
122
|
+
* if (!session || !session.user) {
|
|
123
|
+
* throw redirect("/");
|
|
124
|
+
* }
|
|
125
|
+
* return session;
|
|
126
|
+
* },
|
|
127
|
+
* { key: () => ["auth_user"] }
|
|
128
|
+
* );
|
|
129
|
+
* };
|
|
130
|
+
*
|
|
131
|
+
* return {
|
|
132
|
+
* routeData,
|
|
133
|
+
* Page: () => {
|
|
134
|
+
* const session = useRouteData<typeof routeData>();
|
|
135
|
+
* return (
|
|
136
|
+
* <Show when={session()} keyed>
|
|
137
|
+
* {(sess) => <Comp {...sess} />}
|
|
138
|
+
* </Show>
|
|
139
|
+
* );
|
|
140
|
+
* },
|
|
141
|
+
* };
|
|
142
|
+
* };
|
|
143
|
+
*
|
|
144
|
+
* type IProtectedComponent = Component<Session>;
|
|
145
|
+
*
|
|
146
|
+
* export default Protected;
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* It can be used like this:
|
|
150
|
+
*
|
|
151
|
+
*
|
|
152
|
+
* ```tsx
|
|
153
|
+
* // routes/protected.tsx
|
|
154
|
+
* import Protected from "~/components/Protected";
|
|
155
|
+
*
|
|
156
|
+
* export const { routeData, Page } = Protected((session) => {
|
|
157
|
+
* return (
|
|
158
|
+
* <main class="flex flex-col gap-2 items-center">
|
|
159
|
+
* <h1>This is a protected route</h1>
|
|
160
|
+
* </main>
|
|
161
|
+
* );
|
|
162
|
+
* });
|
|
163
|
+
*
|
|
164
|
+
* export default Page;
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* ### When Using CSR
|
|
168
|
+
*
|
|
169
|
+
* When using CSR, the `Protected` component will not work as expected and will cause the screen to flash, so I had to come up with a tricky solution, we will use a Solid-Start middleware:
|
|
170
|
+
*
|
|
171
|
+
* ```tsx
|
|
172
|
+
* // entry-server.tsx
|
|
173
|
+
* import { Session } from "@auth/core";
|
|
174
|
+
* import { getSession } from "@auth/solid-start";
|
|
175
|
+
* import { redirect } from "solid-start";
|
|
176
|
+
* import {
|
|
177
|
+
* StartServer,
|
|
178
|
+
* createHandler,
|
|
179
|
+
* renderAsync,
|
|
180
|
+
* } from "solid-start/entry-server";
|
|
181
|
+
* import { authOpts } from "./routes/api/auth/[...solidauth]";
|
|
182
|
+
*
|
|
183
|
+
* const protectedPaths = ["/protected"]; // add any route you wish in here
|
|
184
|
+
*
|
|
185
|
+
* export default createHandler(
|
|
186
|
+
* ({ forward }) => {
|
|
187
|
+
* return async (event) => {
|
|
188
|
+
* if (protectedPaths.includes(new URL(event.request.url).pathname)) {
|
|
189
|
+
* const session = await getSession(event.request, authOpts);
|
|
190
|
+
* if (!session) {
|
|
191
|
+
* return redirect("/");
|
|
192
|
+
* }
|
|
193
|
+
* }
|
|
194
|
+
* return forward(event);
|
|
195
|
+
* };
|
|
196
|
+
* },
|
|
197
|
+
* renderAsync((event) => <StartServer event={event} />)
|
|
198
|
+
* );
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* And now you can easily create a protected route:
|
|
202
|
+
*
|
|
203
|
+
*
|
|
204
|
+
* ```tsx
|
|
205
|
+
* // routes/protected.tsx
|
|
206
|
+
* export default () => {
|
|
207
|
+
* return (
|
|
208
|
+
* <main class="flex flex-col gap-2 items-center">
|
|
209
|
+
* <h1>This is a protected route</h1>
|
|
210
|
+
* </main>
|
|
211
|
+
* );
|
|
212
|
+
* };
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* :::note
|
|
216
|
+
* The CSR method should also work when using SSR, the SSR method shouldn't work when using CSR
|
|
217
|
+
* :::
|
|
218
|
+
*
|
|
219
|
+
*/
|
|
220
|
+
export function SolidAuth(config) {
|
|
221
|
+
const { prefix = "/api/auth", ...authOptions } = config;
|
|
222
|
+
authOptions.secret ?? (authOptions.secret = process.env.AUTH_SECRET);
|
|
223
|
+
authOptions.trustHost ?? (authOptions.trustHost = !!(process.env.AUTH_TRUST_HOST ??
|
|
224
|
+
process.env.VERCEL ??
|
|
225
|
+
process.env.NODE_ENV !== "production"));
|
|
226
|
+
const handler = SolidAuthHandler(prefix, authOptions);
|
|
227
|
+
return {
|
|
228
|
+
async GET(event) {
|
|
229
|
+
return await handler(event);
|
|
230
|
+
},
|
|
231
|
+
async POST(event) {
|
|
232
|
+
return await handler(event);
|
|
233
|
+
},
|
|
234
|
+
};
|
|
40
235
|
}
|
|
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);
|
|
236
|
+
export async function getSession(req, options) {
|
|
237
|
+
options.secret ?? (options.secret = process.env.AUTH_SECRET);
|
|
238
|
+
options.trustHost ?? (options.trustHost = true);
|
|
239
|
+
const url = new URL("/api/auth/session", req.url);
|
|
240
|
+
const response = await Auth(new Request(url, { headers: req.headers }), options);
|
|
241
|
+
const { status = 200 } = response;
|
|
242
|
+
const data = await response.json();
|
|
243
|
+
if (!data || !Object.keys(data).length)
|
|
244
|
+
return null;
|
|
245
|
+
if (status === 200)
|
|
246
|
+
return data;
|
|
247
|
+
throw new Error(data.message);
|
|
56
248
|
}
|
|
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.6.
|
|
4
|
+
"version": "0.6.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"*.js",
|
|
8
|
+
"*.d.ts*",
|
|
9
9
|
"src"
|
|
10
10
|
],
|
|
11
11
|
"exports": {
|
|
@@ -32,8 +32,7 @@
|
|
|
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
52
|
"test": "vitest -c ../utils/vitest.config.ts",
|
|
54
|
-
"clean": "rm -rf
|
|
53
|
+
"clean": "rm -rf *.js *.d.ts*"
|
|
55
54
|
}
|
|
56
55
|
}
|