-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix home redirect behavior when credentials are bad
- Loading branch information
1 parent
51c4118
commit a7e6f62
Showing
2 changed files
with
26 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
import { AUTH_COOKIE_NAME } from '$lib/cookies.js' | ||
import { redirect } from '@sveltejs/kit' | ||
import { error, redirect } from '@sveltejs/kit' | ||
import type { Models } from '@kittycad/lib' | ||
|
||
export const load = async ({ cookies, url }) => { | ||
const token = import.meta.env.PROD ? cookies.get(AUTH_COOKIE_NAME) : import.meta.env.VITE_TOKEN | ||
export const load = async ({ cookies, url, fetch }) => { | ||
const token = import.meta.env.PROD | ||
? cookies.get(AUTH_COOKIE_NAME) | ||
: import.meta.env.VITE_ZOO_DEV_TOKEN | ||
|
||
if (token) { | ||
const currentUser = await fetch(import.meta.env.VITE_API_BASE_URL + '/user', { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}` | ||
} | ||
}) | ||
.then(async (res) => (await res.json()) as Models['User_type'] | Models['Error_type']) | ||
.catch((e) => { | ||
throw error(500, e) | ||
}) | ||
|
||
// Redirect to the dashboard if the user is already logged in | ||
if (currentUser && 'email' in currentUser) { | ||
throw redirect(302, '/dashboard' + (url.search || '')) | ||
} else if (import.meta.env.DEV) { | ||
console.warn( | ||
'You might be using an invalid or expired token for your VITE_ZOO_DEV_TOKEN environment variable. Please check your .env file.' | ||
) | ||
} | ||
} |