mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
Compare commits
2 Commits
@auth/soli
...
fix/svelte
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69f2c37920 | ||
|
|
fc5c97db66 |
@@ -81,7 +81,7 @@
|
|||||||
* return {
|
* return {
|
||||||
* session: await event.locals.getSession()
|
* session: await event.locals.getSession()
|
||||||
* };
|
* };
|
||||||
* };
|
* };
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* What you return in the function `LayoutServerLoad` will be available inside the `$page` store, in the `data` property: `$page.data`.
|
* What you return in the function `LayoutServerLoad` will be available inside the `$page` store, in the `data` property: `$page.data`.
|
||||||
@@ -106,7 +106,7 @@
|
|||||||
* return {};
|
* return {};
|
||||||
* };
|
* };
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* :::danger
|
* :::danger
|
||||||
* Make sure to ALWAYS grab the session information from the parent instead of using the store in the case of a `PageLoad`.
|
* Make sure to ALWAYS grab the session information from the parent instead of using the store in the case of a `PageLoad`.
|
||||||
* Not doing so can lead to users being able to incorrectly access protected information in the case the `+layout.server.ts` does not run for that page load.
|
* Not doing so can lead to users being able to incorrectly access protected information in the case the `+layout.server.ts` does not run for that page load.
|
||||||
@@ -130,14 +130,14 @@
|
|||||||
* The handle hook, available in `hooks.server.ts`, is a function that receives ALL requests sent to your SvelteKit webapp.
|
* The handle hook, available in `hooks.server.ts`, is a function that receives ALL requests sent to your SvelteKit webapp.
|
||||||
* You may intercept them inside the handle hook, add and modify things in the request, block requests, etc.
|
* You may intercept them inside the handle hook, add and modify things in the request, block requests, etc.
|
||||||
* Some readers may notice we are already using this handle hook for SvelteKitAuth which returns a handle itself, so we are going to use SvelteKit's sequence to provide middleware-like functions that set the handle hook.
|
* Some readers may notice we are already using this handle hook for SvelteKitAuth which returns a handle itself, so we are going to use SvelteKit's sequence to provide middleware-like functions that set the handle hook.
|
||||||
*
|
*
|
||||||
* ```ts
|
* ```ts
|
||||||
* import { SvelteKitAuth } from '@auth/sveltekit';
|
* import { SvelteKitAuth } from '@auth/sveltekit';
|
||||||
* import GitHub from '@auth/core/providers/github';
|
* import GitHub from '@auth/core/providers/github';
|
||||||
* import { GITHUB_ID, GITHUB_SECRET } from '$env/static/private';
|
* import { GITHUB_ID, GITHUB_SECRET } from '$env/static/private';
|
||||||
* import { redirect, type Handle } from '@sveltejs/kit';
|
* import { redirect, type Handle } from '@sveltejs/kit';
|
||||||
* import { sequence } from '@sveltejs/kit/hooks';
|
* import { sequence } from '@sveltejs/kit/hooks';
|
||||||
*
|
*
|
||||||
* async function authorization({ event, resolve }) {
|
* async function authorization({ event, resolve }) {
|
||||||
* // Protect any routes under /authenticated
|
* // Protect any routes under /authenticated
|
||||||
* if (event.url.pathname.startsWith('/authenticated')) {
|
* if (event.url.pathname.startsWith('/authenticated')) {
|
||||||
@@ -146,14 +146,14 @@
|
|||||||
* throw redirect(303, '/auth');
|
* throw redirect(303, '/auth');
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* // If the request is still here, just proceed as normally
|
* // If the request is still here, just proceed as normally
|
||||||
* const result = await resolve(event, {
|
* const result = await resolve(event, {
|
||||||
* transformPageChunk: ({ html }) => html
|
* transformPageChunk: ({ html }) => html
|
||||||
* });
|
* });
|
||||||
* return result;
|
* return result;
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* // First handle authentication, then authorization
|
* // First handle authentication, then authorization
|
||||||
* // Each function acts as a middleware, receiving the request handle
|
* // Each function acts as a middleware, receiving the request handle
|
||||||
* // And returning a handle which gets passed to the next function
|
* // And returning a handle which gets passed to the next function
|
||||||
@@ -190,7 +190,7 @@
|
|||||||
import type { Handle } from "@sveltejs/kit"
|
import type { Handle } from "@sveltejs/kit"
|
||||||
|
|
||||||
import { dev } from "$app/environment"
|
import { dev } from "$app/environment"
|
||||||
import { env } from "$env/dynamic/private"
|
import { AUTH_SECRET, AUTH_TRUST_HOST, VERCEL } from "$env/static/private"
|
||||||
|
|
||||||
import { Auth } from "@auth/core"
|
import { Auth } from "@auth/core"
|
||||||
import type { AuthAction, AuthConfig, Session } from "@auth/core/types"
|
import type { AuthAction, AuthConfig, Session } from "@auth/core/types"
|
||||||
@@ -199,7 +199,7 @@ export async function getSession(
|
|||||||
req: Request,
|
req: Request,
|
||||||
config: AuthConfig
|
config: AuthConfig
|
||||||
): ReturnType<App.Locals["getSession"]> {
|
): ReturnType<App.Locals["getSession"]> {
|
||||||
config.secret ??= env.AUTH_SECRET
|
config.secret ??= AUTH_SECRET
|
||||||
config.trustHost ??= true
|
config.trustHost ??= true
|
||||||
|
|
||||||
const url = new URL("/api/auth/session", req.url)
|
const url = new URL("/api/auth/session", req.url)
|
||||||
@@ -261,8 +261,8 @@ function AuthHandle(prefix: string, authOptions: AuthConfig): Handle {
|
|||||||
*/
|
*/
|
||||||
export function SvelteKitAuth(options: SvelteKitAuthConfig): Handle {
|
export function SvelteKitAuth(options: SvelteKitAuthConfig): Handle {
|
||||||
const { prefix = "/auth", ...authOptions } = options
|
const { prefix = "/auth", ...authOptions } = options
|
||||||
authOptions.secret ??= env.AUTH_SECRET
|
authOptions.secret ??= AUTH_SECRET
|
||||||
authOptions.trustHost ??= !!(env.AUTH_TRUST_HOST ?? env.VERCEL ?? dev)
|
authOptions.trustHost ??= !!(AUTH_TRUST_HOST ?? VERCEL ?? dev)
|
||||||
return AuthHandle(prefix, authOptions)
|
return AuthHandle(prefix, authOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +278,7 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module "$env/dynamic/private" {
|
declare module "$env/static/private" {
|
||||||
export const AUTH_SECRET: string
|
export const AUTH_SECRET: string
|
||||||
export const AUTH_TRUST_HOST: string
|
export const AUTH_TRUST_HOST: string
|
||||||
export const VERCEL: string
|
export const VERCEL: string
|
||||||
|
|||||||
Reference in New Issue
Block a user