Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API-key fetch and fetch in load/Action types #1

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PUBLIC_MAPTILER_API_KEY=""
PUBLIC_REST_API_URL="http://localhost:1337/v1"
PUBLIC_REST_API_URL="http://localhost:1337/v1"
PRIVATE_REST_API_KEY=""
9 changes: 9 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Handle } from '@sveltejs/kit';
import type { HandleFetch } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { jwtDecode } from 'jwt-decode';
import type { CustomJwtPayload } from './lib/types/CustomJwtPayload';
import { PRIVATE_REST_API_KEY } from '$env/static/private';

export const handle: Handle = async ({ event, resolve }) => {
// ONLY RUN this hook for admin pages (e.g. /admin/dashboard, /admin/users)
Expand All @@ -12,6 +14,7 @@ export const handle: Handle = async ({ event, resolve }) => {

const token = event.cookies.get('access_token');


// Redirect early to login page if the user is not logged in
if (typeof token !== 'string' || !token) {
// Sometimes the user object is still in the request locals, for example if:
Expand Down Expand Up @@ -48,3 +51,9 @@ export const handle: Handle = async ({ event, resolve }) => {

return await resolve(event); // <-- all good, allow the request to proceed
};

export const handleFetch: HandleFetch = async ({ request, fetch }) => {
request.headers.set('x-api-key', PRIVATE_REST_API_KEY);

return fetch(request);
};
2 changes: 1 addition & 1 deletion src/routes/(auth)/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const load: PageServerLoad = async ({ cookies }) => {
}
};

const login: Action = async ({ cookies, request }) => {
const login: Action = async ({ cookies, request, fetch }) => {
const data = await request.formData();

const username = data.get('username');
Expand Down
8 changes: 4 additions & 4 deletions src/routes/(protected)/admin/bikes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { PUBLIC_REST_API_URL } from '$env/static/public';

import { fail } from '@sveltejs/kit';

export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`, {
method: 'GET'
}).then((r) => r.json())
};
};

const deactivate: Action = async ({ request, cookies }) => {
const deactivate: Action = async ({ request, cookies, fetch }) => {
const data = await request.formData();

const bikeId = data.get('id');
Expand Down Expand Up @@ -48,7 +48,7 @@ const deactivate: Action = async ({ request, cookies }) => {
return { success: true };
};

const activate: Action = async ({ request, cookies }) => {
const activate: Action = async ({ request, cookies, fetch }) => {
const data = await request.formData();

const bikeId = data.get('id');
Expand Down Expand Up @@ -84,7 +84,7 @@ const activate: Action = async ({ request, cookies }) => {
return { success: true };
};

const changeStatus: Action = async ({ request, cookies }) => {
const changeStatus: Action = async ({ request, cookies, fetch }) => {
const data = await request.formData();

const bikeId = data.get('id');
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(protected)/admin/bikes/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ params }) => {
export const load: PageServerLoad = async ({ params, fetch }) => {
return {
bike: await fetch(`${PUBLIC_REST_API_URL}/bikes/${params.id}`, {
method: 'GET'
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(protected)/admin/map/live/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`, {
method: 'GET'
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(protected)/admin/map/live/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import type { BikePointFeature } from '$lib/types/BikePointFeature';
import type { PageData } from './$types';
import type { Bike } from '$lib/types/Bike';
import { PUBLIC_REST_API_URL } from '$env/static/public';

Check failure on line 9 in src/routes/(protected)/admin/map/live/+page.svelte

View workflow job for this annotation

GitHub Actions / build (18.x)

'PUBLIC_REST_API_URL' is defined but never used

Check failure on line 9 in src/routes/(protected)/admin/map/live/+page.svelte

View workflow job for this annotation

GitHub Actions / build (20.x)

'PUBLIC_REST_API_URL' is defined but never used

export let data: PageData;

Expand Down Expand Up @@ -227,7 +227,7 @@
});

const startSimulation = () => {
const result = fetch(`${PUBLIC_REST_API_URL}/admin/simulate`);
const result = fetch(`http://localhost:1337/v1/admin/simulate`);
console.log(result);
};
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(protected)/admin/map/zones/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
zones: await fetch(`${PUBLIC_REST_API_URL}/zones`, {
method: 'GET'
Expand Down
Loading