Added JWT based Authorization for any operation
This commit is contained in:
parent
52a6fa3005
commit
2f40f3f5ac
@ -64,4 +64,7 @@ configureWunderGraphApplication({
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
authorization: {
|
||||||
|
roles: ['admin'],
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
@ -5,7 +5,7 @@ export default configureWunderGraphOperations<OperationsConfiguration>({
|
|||||||
operations: {
|
operations: {
|
||||||
defaultConfig: {
|
defaultConfig: {
|
||||||
authentication: {
|
authentication: {
|
||||||
required: false,
|
required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
queries: (config) => ({
|
queries: (config) => ({
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
async function login() {
|
async function login() {
|
||||||
const response = await fetch("/api/auth", { method: "POST" });
|
const response = await fetch("/api/auth", { method: "POST" });
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@ -8,6 +9,7 @@
|
|||||||
let { token } = await response.json();
|
let { token } = await response.json();
|
||||||
Cookies.set("token", token);
|
Cookies.set("token", token);
|
||||||
alert(`Login Success: ${token}`);
|
alert(`Login Success: ${token}`);
|
||||||
|
console.log(token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
{#if user}
|
{#if user}
|
||||||
<p>Welcome, {user.name}!</p>
|
<p>Welcome, {user.name}!</p>
|
||||||
|
<p>Your roles: {user.roles.join(", ")}</p>
|
||||||
{:else}
|
{:else}
|
||||||
<p>Loading...</p>
|
<p>Loading...</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
@ -7,4 +7,4 @@ const client = createClient();
|
|||||||
const { createFileUpload, createMutation, createQuery, createSubscription, getAuth, getUser, queryKey, prefetchQuery } =
|
const { createFileUpload, createMutation, createQuery, createSubscription, getAuth, getUser, queryKey, prefetchQuery } =
|
||||||
createSvelteClient<Operations>(client);
|
createSvelteClient<Operations>(client);
|
||||||
|
|
||||||
export { createFileUpload, createMutation, createQuery, createSubscription, getAuth, getUser, queryKey, prefetchQuery };
|
export { createFileUpload, createMutation, createQuery, createSubscription, getAuth, getUser, queryKey, prefetchQuery, client };
|
||||||
|
@ -1,8 +1,17 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { QueryClientProvider } from "@tanstack/svelte-query";
|
import { QueryClientProvider } from "@tanstack/svelte-query";
|
||||||
import type { LayoutData } from "./$types";
|
import type { LayoutData } from "./$types";
|
||||||
|
import { client } from "$lib/wundergraph";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
export let data: LayoutData;
|
export let data: LayoutData;
|
||||||
|
|
||||||
|
const token = Cookies.get("token");
|
||||||
|
|
||||||
|
// Set the Authorization header token
|
||||||
|
if (token) {
|
||||||
|
client.setAuthorizationToken(token);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<QueryClientProvider client={data.queryClient}>
|
<QueryClientProvider client={data.queryClient}>
|
||||||
|
@ -5,7 +5,7 @@ import { error } from '@sveltejs/kit';
|
|||||||
const secretKey = 'mysecrettestkey';
|
const secretKey = 'mysecrettestkey';
|
||||||
|
|
||||||
export async function POST() {
|
export async function POST() {
|
||||||
const token = jwt.sign({ name: 'Samuel', loggedIn: true }, secretKey);
|
const token = jwt.sign({ name: 'Samuel', loggedIn: true, roles: ['admin'] }, secretKey);
|
||||||
if (!token) {
|
if (!token) {
|
||||||
throw error(400, 'No token created.');
|
throw error(400, 'No token created.');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user