From 4934b99068759ff3cd3a2db7bc3883cd77911f31 Mon Sep 17 00:00:00 2001
From: Emmanuel Raviart <emmanuel@raviart.com>
Date: Fri, 26 Aug 2022 23:42:24 +0200
Subject: [PATCH] Move session to root layout data.

---
 src/app.d.ts                 | 45 +-----------------------------------
 src/lib/server/oauth2.ts     |  3 ++-
 src/routes/+layout.server.ts | 31 ++++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 46 deletions(-)

diff --git a/src/app.d.ts b/src/app.d.ts
index fc20f32c2..00539e81b 100644
--- a/src/app.d.ts
+++ b/src/app.d.ts
@@ -2,51 +2,8 @@
 
 declare namespace App {
   interface Locals {
-    user?: User
+    user?: import("$lib/users").User
   }
 
   // interface Platform {}
-
-  interface Session {
-    advanced: boolean
-    apiBaseUrls: string[]
-    apiWebSocketBaseUrls: string[]
-    baseUrl: string
-    authenticationEnabled: boolean
-    childrenKey: string
-    familyEntityKey: string
-    hasGithubPersonalAccessToken: boolean
-    hiddenEntitiesKeyPlural?: string[]
-    householdEntityKey: string
-    legalUrl: string
-    matomo?: {
-      prependDomain?: boolean
-      siteId: number
-      subdomains?: string
-      url: string
-    }
-    openfiscaRepository: import("$lib/repositories").RepositoryConfig
-    portalUrl: string
-    reformName?: string
-    showTutorial?: boolean
-    taxableHouseholdEntityKey: string
-    territoiresUrl: string
-    title: string
-    user?: User
-  }
-
-  // interface Stuff {}
-
-  export interface User {
-    email: string // "john@doe.com"
-    email_verified: boolean
-    family_name: string // "Doe"
-    given_name: string // "John"
-    last_name: string // "Doe"
-    locale: string // "fr"
-    name: string // "John Doe"
-    preferred_username: string // "jdoe",
-    roles?: string[] // [ 'offline_access', 'default-roles-leximpact', 'uma_authorization' ],
-    sub: string // "12345678-9abc-def0-1234-56789abcdef0"
-  }
 }
diff --git a/src/lib/server/oauth2.ts b/src/lib/server/oauth2.ts
index e260d9e40..2322afb99 100644
--- a/src/lib/server/oauth2.ts
+++ b/src/lib/server/oauth2.ts
@@ -2,8 +2,9 @@ import { SvelteKitAuth } from "sk-auth"
 import { OAuth2Provider } from "sk-auth/providers"
 
 import config from "$lib/server/config"
+import type { User } from "$lib/users"
 
-type Profile = App.User
+type Profile = User
 
 interface Tokens {
   access_token: string
diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts
index b780a90da..8b5631e62 100644
--- a/src/routes/+layout.server.ts
+++ b/src/routes/+layout.server.ts
@@ -1,3 +1,4 @@
+import type { User } from "$lib/users"
 import config from "$lib/server/config"
 import { oauth2Authenticator } from "$lib/server/oauth2"
 
@@ -5,7 +6,35 @@ import type { LayoutServerLoad } from "./$types"
 
 const { githubPersonalAccessToken, openfiscaRepository } = config
 
-export const load: LayoutServerLoad = async (event) => {
+export const load: LayoutServerLoad = async (
+  event,
+): Promise<{
+  advanced: boolean
+  apiBaseUrls: string[]
+  apiWebSocketBaseUrls: string[]
+  baseUrl: string
+  authenticationEnabled: boolean
+  childrenKey: string
+  familyEntityKey: string
+  hasGithubPersonalAccessToken: boolean
+  hiddenEntitiesKeyPlural?: string[]
+  householdEntityKey: string
+  legalUrl: string
+  matomo?: {
+    prependDomain?: boolean
+    siteId: number
+    subdomains?: string
+    url: string
+  }
+  openfiscaRepository: import("$lib/repositories").RepositoryConfig
+  portalUrl: string
+  reformName?: string
+  showTutorial?: boolean
+  taxableHouseholdEntityKey: string
+  territoiresUrl: string
+  title: string
+  user?: User
+}> => {
   const oauth2Session = await oauth2Authenticator?.getSession(event)
   const user = oauth2Session?.user
   return {
-- 
GitLab