@auth/solid-start 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/client.d.ts CHANGED
@@ -24,14 +24,21 @@ type SignInAuthorizationParams = string | string[][] | Record<string, string> |
24
24
  * or send the user to the signin page listing all possible providers.
25
25
  * Automatically adds the CSRF token to the request.
26
26
  *
27
- * [Documentation](https://next-auth.js.org/getting-started/client#signin)
27
+ * ```ts
28
+ * import { signIn } from "@auth/solid-start/client"
29
+ * signIn()
30
+ * signIn("provider") // example: signIn("github")
31
+ * ```
28
32
  */
29
33
  declare function signIn<P extends RedirectableProviderType | undefined = undefined>(providerId?: LiteralUnion<P extends RedirectableProviderType ? P | BuiltInProviderType : BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorizationParams): Promise<Response | undefined>;
30
34
  /**
31
35
  * Signs the user out, by removing the session cookie.
32
36
  * Automatically adds the CSRF token to the request.
33
37
  *
34
- * [Documentation](https://next-auth.js.org/getting-started/client#signout)
38
+ * ```ts
39
+ * import { signOut } from "@auth/solid-start/client"
40
+ * signOut()
41
+ * ```
35
42
  */
36
43
  declare function signOut(options?: SignOutParams): Promise<void>;
37
44
 
package/client.js CHANGED
@@ -1,3 +1,6 @@
1
+ import {
2
+ __name
3
+ } from "./chunk-HXRSFH6L.js";
1
4
  async function signIn(providerId, options, authorizationParams) {
2
5
  const { callbackUrl = window.location.href, redirect = true } = options ?? {};
3
6
  const isCredentials = providerId === "credentials";
@@ -29,6 +32,7 @@ async function signIn(providerId, options, authorizationParams) {
29
32
  }
30
33
  return res;
31
34
  }
35
+ __name(signIn, "signIn");
32
36
  async function signOut(options) {
33
37
  const { callbackUrl = window.location.href } = options ?? {};
34
38
  const csrfTokenResponse = await fetch("/api/auth/csrf");
@@ -50,6 +54,7 @@ async function signOut(options) {
50
54
  if (url.includes("#"))
51
55
  window.location.reload();
52
56
  }
57
+ __name(signOut, "signOut");
53
58
  export {
54
59
  signIn,
55
60
  signOut
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as _auth_core_types from '@auth/core/types';
1
2
  import { AuthConfig, Session } from '@auth/core/types';
2
3
 
3
4
  interface SolidAuthConfig extends AuthConfig {
@@ -7,11 +8,187 @@ interface SolidAuthConfig extends AuthConfig {
7
8
  */
8
9
  prefix?: string;
9
10
  }
11
+ /**
12
+ * ## Setup
13
+ *
14
+ * [Generate an auth secret](https://generate-secret.vercel.app/32), then set it as an environment variable:
15
+ *
16
+ * ```
17
+ * AUTH_SECRET=your_auth_secret
18
+ * ```
19
+ *
20
+ * ## Creating the API handler
21
+ *
22
+ * in this example we are using github so make sure to set the following environment variables:
23
+ *
24
+ * ```
25
+ * GITHUB_ID=your_github_oauth_id
26
+ * GITHUB_SECRET=your_github_oauth_secret
27
+ * ```
28
+ *
29
+ * ```ts
30
+ * // routes/api/auth/[...solidauth].ts
31
+ * import { SolidAuth, type SolidAuthConfig } from "@auth/solid-start"
32
+ * import GitHub from "@auth/core/providers/github"
33
+ *
34
+ * export const authOpts: SolidAuthConfig = {
35
+ * providers: [
36
+ * GitHub({
37
+ * clientId: process.env.GITHUB_ID,
38
+ * clientSecret: process.env.GITHUB_SECRET,
39
+ * }),
40
+ * ],
41
+ * debug: false,
42
+ * }
43
+ *
44
+ * export const { GET, POST } = SolidAuth(authOpts)
45
+ * ```
46
+ *
47
+ * ## Getting the current session
48
+ *
49
+ * ```ts
50
+ * import { getSession } from "@auth/solid-start"
51
+ * import { createServerData$ } from "solid-start/server"
52
+ * import { authOpts } from "~/routes/api/auth/[...solidauth]"
53
+ *
54
+ * export const useSession = () => {
55
+ * return createServerData$(
56
+ * async (_, { request }) => {
57
+ * return await getSession(request, authOpts)
58
+ * },
59
+ * { key: () => ["auth_user"] }
60
+ * )
61
+ * }
62
+ *
63
+ * // useSession returns a resource:
64
+ * const session = useSession()
65
+ * const loading = session.loading
66
+ * const user = () => session()?.user
67
+ * ```
68
+ * ## Protected Routes
69
+ *
70
+ * ### When Using SSR
71
+ *
72
+ * When using SSR, I recommend creating a `Protected` component that will trigger suspense using the `Show` component. It should look like this:
73
+ *
74
+ *
75
+ * ```tsx
76
+ * // components/Protected.tsx
77
+ * import { type Session } from "@auth/core/types";
78
+ * import { getSession } from "@auth/solid-start";
79
+ * import { Component, Show } from "solid-js";
80
+ * import { useRouteData } from "solid-start";
81
+ * import { createServerData$, redirect } from "solid-start/server";
82
+ * import { authOpts } from "~/routes/api/auth/[...solidauth]";
83
+ *
84
+ * const Protected = (Comp: IProtectedComponent) => {
85
+ * const routeData = () => {
86
+ * return createServerData$(
87
+ * async (_, event) => {
88
+ * const session = await getSession(event.request, authOpts);
89
+ * if (!session || !session.user) {
90
+ * throw redirect("/");
91
+ * }
92
+ * return session;
93
+ * },
94
+ * { key: () => ["auth_user"] }
95
+ * );
96
+ * };
97
+ *
98
+ * return {
99
+ * routeData,
100
+ * Page: () => {
101
+ * const session = useRouteData<typeof routeData>();
102
+ * return (
103
+ * <Show when={session()} keyed>
104
+ * {(sess) => <Comp {...sess} />}
105
+ * </Show>
106
+ * );
107
+ * },
108
+ * };
109
+ * };
110
+ *
111
+ * type IProtectedComponent = Component<Session>;
112
+ *
113
+ * export default Protected;
114
+ * ```
115
+ *
116
+ * It can be used like this:
117
+ *
118
+ *
119
+ * ```tsx
120
+ * // routes/protected.tsx
121
+ * import Protected from "~/components/Protected";
122
+ *
123
+ * export const { routeData, Page } = Protected((session) => {
124
+ * return (
125
+ * <main class="flex flex-col gap-2 items-center">
126
+ * <h1>This is a protected route</h1>
127
+ * </main>
128
+ * );
129
+ * });
130
+ *
131
+ * export default Page;
132
+ * ```
133
+ *
134
+ * ### When Using CSR
135
+ *
136
+ * 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:
137
+ *
138
+ * ```tsx
139
+ * // entry-server.tsx
140
+ * import { Session } from "@auth/core";
141
+ * import { getSession } from "@auth/solid-start";
142
+ * import { redirect } from "solid-start";
143
+ * import {
144
+ * StartServer,
145
+ * createHandler,
146
+ * renderAsync,
147
+ * } from "solid-start/entry-server";
148
+ * import { authOpts } from "./routes/api/auth/[...solidauth]";
149
+ *
150
+ * const protectedPaths = ["/protected"]; // add any route you wish in here
151
+ *
152
+ * export default createHandler(
153
+ * ({ forward }) => {
154
+ * return async (event) => {
155
+ * if (protectedPaths.includes(new URL(event.request.url).pathname)) {
156
+ * const session = await getSession(event.request, authOpts);
157
+ * if (!session) {
158
+ * return redirect("/");
159
+ * }
160
+ * }
161
+ * return forward(event);
162
+ * };
163
+ * },
164
+ * renderAsync((event) => <StartServer event={event} />)
165
+ * );
166
+ * ```
167
+ *
168
+ * And now you can easily create a protected route:
169
+ *
170
+ *
171
+ * ```tsx
172
+ * // routes/protected.tsx
173
+ * export default () => {
174
+ * return (
175
+ * <main class="flex flex-col gap-2 items-center">
176
+ * <h1>This is a protected route</h1>
177
+ * </main>
178
+ * );
179
+ * };
180
+ * ```
181
+ *
182
+ * :::note
183
+ * The CSR method should also work when using SSR, the SSR method shouldn't work when using CSR
184
+ * :::
185
+ *
186
+ */
10
187
  declare function SolidAuth(config: SolidAuthConfig): {
11
- GET(event: any): Promise<Response | undefined>;
12
- POST(event: any): Promise<Response | undefined>;
188
+ GET(event: any): Promise<_auth_core_types.ResponseInternal<any> | undefined>;
189
+ POST(event: any): Promise<_auth_core_types.ResponseInternal<any> | undefined>;
13
190
  };
14
191
  type GetSessionResult = Promise<Session | null>;
15
- declare function getSession(req: Request, options: AuthConfig): GetSessionResult;
192
+ declare function getSession(req: Request, options: Omit<AuthConfig, "raw">): GetSessionResult;
16
193
 
17
194
  export { GetSessionResult, SolidAuth, SolidAuthConfig, getSession };
package/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import {
2
+ __name
3
+ } from "./chunk-HXRSFH6L.js";
1
4
  import { Auth } from "@auth/core";
2
5
  const actions = [
3
6
  "providers",
@@ -20,6 +23,7 @@ function SolidAuthHandler(prefix, authOptions) {
20
23
  return await Auth(request, authOptions);
21
24
  };
22
25
  }
26
+ __name(SolidAuthHandler, "SolidAuthHandler");
23
27
  function SolidAuth(config) {
24
28
  const { prefix = "/api/auth", ...authOptions } = config;
25
29
  authOptions.secret ??= process.env.AUTH_SECRET;
@@ -34,14 +38,14 @@ function SolidAuth(config) {
34
38
  }
35
39
  };
36
40
  }
41
+ __name(SolidAuth, "SolidAuth");
37
42
  async function getSession(req, options) {
38
43
  options.secret ??= process.env.AUTH_SECRET;
39
44
  options.trustHost ??= true;
40
45
  const url = new URL("/api/auth/session", req.url);
41
- const response = await Auth(
42
- new Request(url, { headers: req.headers }),
43
- options
44
- );
46
+ const response = await Auth(new Request(url, {
47
+ headers: req.headers
48
+ }), options);
45
49
  const { status = 200 } = response;
46
50
  const data = await response.json();
47
51
  if (!data || !Object.keys(data).length)
@@ -50,6 +54,7 @@ async function getSession(req, options) {
50
54
  return data;
51
55
  throw new Error(data.message);
52
56
  }
57
+ __name(getSession, "getSession");
53
58
  export {
54
59
  SolidAuth,
55
60
  getSession
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@auth/solid-start",
3
3
  "description": "Authentication for SolidStart.",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "client.*",
@@ -22,17 +22,17 @@
22
22
  "publishConfig": {
23
23
  "access": "public"
24
24
  },
25
+ "dependencies": {
26
+ "@auth/core": "0.18.2"
27
+ },
25
28
  "devDependencies": {
26
29
  "@solidjs/meta": "^0.28.0",
27
30
  "@types/node": "^18.7.14",
28
31
  "solid-js": "^1.5.7",
29
32
  "solid-start": "^0.2.14",
30
- "tsup": "^6.5.0",
31
- "typescript": "5.2.2",
32
- "@auth/core": "0.16.0"
33
+ "tsup": "^6.5.0"
33
34
  },
34
35
  "peerDependencies": {
35
- "@auth/core": "~0.2.2 || ^0.2.2",
36
36
  "solid-js": "^1.5.7",
37
37
  "solid-start": "^0.2.14"
38
38
  },
package/src/client.ts CHANGED
@@ -35,7 +35,11 @@ export type SignInAuthorizationParams =
35
35
  * or send the user to the signin page listing all possible providers.
36
36
  * Automatically adds the CSRF token to the request.
37
37
  *
38
- * [Documentation](https://next-auth.js.org/getting-started/client#signin)
38
+ * ```ts
39
+ * import { signIn } from "@auth/solid-start/client"
40
+ * signIn()
41
+ * signIn("provider") // example: signIn("github")
42
+ * ```
39
43
  */
40
44
  export async function signIn<
41
45
  P extends RedirectableProviderType | undefined = undefined
@@ -96,7 +100,10 @@ export async function signIn<
96
100
  * Signs the user out, by removing the session cookie.
97
101
  * Automatically adds the CSRF token to the request.
98
102
  *
99
- * [Documentation](https://next-auth.js.org/getting-started/client#signout)
103
+ * ```ts
104
+ * import { signOut } from "@auth/solid-start/client"
105
+ * signOut()
106
+ * ```
100
107
  */
101
108
  export async function signOut(options?: SignOutParams) {
102
109
  const { callbackUrl = window.location.href } = options ?? {}
package/src/index.ts CHANGED
@@ -1,3 +1,23 @@
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
+ */
20
+
1
21
  import { Auth } from "@auth/core"
2
22
  import type { AuthAction, AuthConfig, Session } from "@auth/core/types"
3
23
 
@@ -36,6 +56,182 @@ function SolidAuthHandler(prefix: string, authOptions: SolidAuthConfig) {
36
56
  }
37
57
  }
38
58
 
59
+ /**
60
+ * ## Setup
61
+ *
62
+ * [Generate an auth secret](https://generate-secret.vercel.app/32), then set it as an environment variable:
63
+ *
64
+ * ```
65
+ * AUTH_SECRET=your_auth_secret
66
+ * ```
67
+ *
68
+ * ## Creating the API handler
69
+ *
70
+ * in this example we are using github so make sure to set the following environment variables:
71
+ *
72
+ * ```
73
+ * GITHUB_ID=your_github_oauth_id
74
+ * GITHUB_SECRET=your_github_oauth_secret
75
+ * ```
76
+ *
77
+ * ```ts
78
+ * // routes/api/auth/[...solidauth].ts
79
+ * import { SolidAuth, type SolidAuthConfig } from "@auth/solid-start"
80
+ * import GitHub from "@auth/core/providers/github"
81
+ *
82
+ * export const authOpts: SolidAuthConfig = {
83
+ * providers: [
84
+ * GitHub({
85
+ * clientId: process.env.GITHUB_ID,
86
+ * clientSecret: process.env.GITHUB_SECRET,
87
+ * }),
88
+ * ],
89
+ * debug: false,
90
+ * }
91
+ *
92
+ * export const { GET, POST } = SolidAuth(authOpts)
93
+ * ```
94
+ *
95
+ * ## Getting the current session
96
+ *
97
+ * ```ts
98
+ * import { getSession } from "@auth/solid-start"
99
+ * import { createServerData$ } from "solid-start/server"
100
+ * import { authOpts } from "~/routes/api/auth/[...solidauth]"
101
+ *
102
+ * export const useSession = () => {
103
+ * return createServerData$(
104
+ * async (_, { request }) => {
105
+ * return await getSession(request, authOpts)
106
+ * },
107
+ * { key: () => ["auth_user"] }
108
+ * )
109
+ * }
110
+ *
111
+ * // useSession returns a resource:
112
+ * const session = useSession()
113
+ * const loading = session.loading
114
+ * const user = () => session()?.user
115
+ * ```
116
+ * ## Protected Routes
117
+ *
118
+ * ### When Using SSR
119
+ *
120
+ * When using SSR, I recommend creating a `Protected` component that will trigger suspense using the `Show` component. It should look like this:
121
+ *
122
+ *
123
+ * ```tsx
124
+ * // components/Protected.tsx
125
+ * import { type Session } from "@auth/core/types";
126
+ * import { getSession } from "@auth/solid-start";
127
+ * import { Component, Show } from "solid-js";
128
+ * import { useRouteData } from "solid-start";
129
+ * import { createServerData$, redirect } from "solid-start/server";
130
+ * import { authOpts } from "~/routes/api/auth/[...solidauth]";
131
+ *
132
+ * const Protected = (Comp: IProtectedComponent) => {
133
+ * const routeData = () => {
134
+ * return createServerData$(
135
+ * async (_, event) => {
136
+ * const session = await getSession(event.request, authOpts);
137
+ * if (!session || !session.user) {
138
+ * throw redirect("/");
139
+ * }
140
+ * return session;
141
+ * },
142
+ * { key: () => ["auth_user"] }
143
+ * );
144
+ * };
145
+ *
146
+ * return {
147
+ * routeData,
148
+ * Page: () => {
149
+ * const session = useRouteData<typeof routeData>();
150
+ * return (
151
+ * <Show when={session()} keyed>
152
+ * {(sess) => <Comp {...sess} />}
153
+ * </Show>
154
+ * );
155
+ * },
156
+ * };
157
+ * };
158
+ *
159
+ * type IProtectedComponent = Component<Session>;
160
+ *
161
+ * export default Protected;
162
+ * ```
163
+ *
164
+ * It can be used like this:
165
+ *
166
+ *
167
+ * ```tsx
168
+ * // routes/protected.tsx
169
+ * import Protected from "~/components/Protected";
170
+ *
171
+ * export const { routeData, Page } = Protected((session) => {
172
+ * return (
173
+ * <main class="flex flex-col gap-2 items-center">
174
+ * <h1>This is a protected route</h1>
175
+ * </main>
176
+ * );
177
+ * });
178
+ *
179
+ * export default Page;
180
+ * ```
181
+ *
182
+ * ### When Using CSR
183
+ *
184
+ * 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:
185
+ *
186
+ * ```tsx
187
+ * // entry-server.tsx
188
+ * import { Session } from "@auth/core";
189
+ * import { getSession } from "@auth/solid-start";
190
+ * import { redirect } from "solid-start";
191
+ * import {
192
+ * StartServer,
193
+ * createHandler,
194
+ * renderAsync,
195
+ * } from "solid-start/entry-server";
196
+ * import { authOpts } from "./routes/api/auth/[...solidauth]";
197
+ *
198
+ * const protectedPaths = ["/protected"]; // add any route you wish in here
199
+ *
200
+ * export default createHandler(
201
+ * ({ forward }) => {
202
+ * return async (event) => {
203
+ * if (protectedPaths.includes(new URL(event.request.url).pathname)) {
204
+ * const session = await getSession(event.request, authOpts);
205
+ * if (!session) {
206
+ * return redirect("/");
207
+ * }
208
+ * }
209
+ * return forward(event);
210
+ * };
211
+ * },
212
+ * renderAsync((event) => <StartServer event={event} />)
213
+ * );
214
+ * ```
215
+ *
216
+ * And now you can easily create a protected route:
217
+ *
218
+ *
219
+ * ```tsx
220
+ * // routes/protected.tsx
221
+ * export default () => {
222
+ * return (
223
+ * <main class="flex flex-col gap-2 items-center">
224
+ * <h1>This is a protected route</h1>
225
+ * </main>
226
+ * );
227
+ * };
228
+ * ```
229
+ *
230
+ * :::note
231
+ * The CSR method should also work when using SSR, the SSR method shouldn't work when using CSR
232
+ * :::
233
+ *
234
+ */
39
235
  export function SolidAuth(config: SolidAuthConfig) {
40
236
  const { prefix = "/api/auth", ...authOptions } = config
41
237
  authOptions.secret ??= process.env.AUTH_SECRET
@@ -59,7 +255,7 @@ export type GetSessionResult = Promise<Session | null>
59
255
 
60
256
  export async function getSession(
61
257
  req: Request,
62
- options: AuthConfig
258
+ options: Omit<AuthConfig, "raw">
63
259
  ): GetSessionResult {
64
260
  options.secret ??= process.env.AUTH_SECRET
65
261
  options.trustHost ??= true