1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-01 08:31:59 +00:00

fix(web): browser-side api client to include authorization token

This commit is contained in:
Connery Noble 2023-01-18 17:11:38 -08:00
parent 157d9b9cd7
commit 60e338938f
2 changed files with 27 additions and 1 deletions

View file

@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import * as cookieParse from 'cookie';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import LoadingSpinner from '$lib/components/shared-components/loading-spinner.svelte'; import LoadingSpinner from '$lib/components/shared-components/loading-spinner.svelte';
import { loginPageMessage } from '$lib/constants'; import { loginPageMessage } from '$lib/constants';
@ -19,8 +20,11 @@
if (oauth.isCallback(window.location)) { if (oauth.isCallback(window.location)) {
try { try {
loading = true; loading = true;
await oauth.login(window.location); const { data } = await oauth.login(window.location);
dispatch('success'); dispatch('success');
// Set the access token
saveAccessToken(data.accessToken);
return; return;
} catch (e) { } catch (e) {
console.error('Error [login-form] [oauth.callback]', e); console.error('Error [login-form] [oauth.callback]', e);
@ -63,6 +67,9 @@
return; return;
} }
// Set the access token
saveAccessToken(data.accessToken);
dispatch('success'); dispatch('success');
return; return;
} catch (e) { } catch (e) {
@ -71,6 +78,14 @@
return; return;
} }
}; };
function saveAccessToken(accessToken: string) {
// Set client requests to include access token
api.setAccessToken(accessToken);
// Save the access token cookie
document.cookie = cookieParse.serialize('immich_access_token', accessToken);
}
</script> </script>
<div <div

View file

@ -1,6 +1,8 @@
<script lang="ts"> <script lang="ts">
import '../app.css'; import '../app.css';
import { api } from '@api';
import * as cookieParser from 'cookie';
import { fade } from 'svelte/transition'; import { fade } from 'svelte/transition';
import { page } from '$app/stores'; import { page } from '$app/stores';
import DownloadPanel from '$lib/components/asset-viewer/download-panel.svelte'; import DownloadPanel from '$lib/components/asset-viewer/download-panel.svelte';
@ -22,6 +24,7 @@
let showUploadCover = false; let showUploadCover = false;
onMount(async () => { onMount(async () => {
initApiAccessToken();
checkUserTheme(); checkUserTheme();
const res = await checkAppVersion(); const res = await checkAppVersion();
@ -30,6 +33,14 @@
remoteVersion = res.remoteVersion ?? 'unknown'; remoteVersion = res.remoteVersion ?? 'unknown';
}); });
const initApiAccessToken = () => {
const cookies = cookieParser.parse(document.cookie);
const accessToken = cookies['immich_access_token'];
if (accessToken) {
api.setAccessToken(accessToken);
}
};
const checkUserTheme = () => { const checkUserTheme = () => {
// On page load or when changing themes, best to add inline in `head` to avoid FOUC // On page load or when changing themes, best to add inline in `head` to avoid FOUC
if ( if (