summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichiel Schuurmans <michielschuurmans@gmail.com>2020-10-27 21:15:15 +0100
committerMichiel Schuurmans <michielschuurmans@gmail.com>2020-10-27 21:15:15 +0100
commitd8ed3ce56cffa560452b0592a3e1f036c36c5135 (patch)
tree8ecbdd8539e2af0a2f2bc05a26552e9ad99d5faa
parent9e9ba4227ddcf30e53308d35d174c95d69dc1926 (diff)
Web: Basic start/stop
Signed-off-by: Michiel Schuurmans <michielschuurmans@gmail.com>
-rw-r--r--api/app/Http/Middleware/Auth0Middleware.php6
-rw-r--r--api/app/Http/Middleware/CorsMiddleware.php39
-rw-r--r--api/bootstrap/app.php4
-rw-r--r--web/.gitignore6
-rw-r--r--web/package-lock.json11
-rw-r--r--web/package.json1
-rw-r--r--web/src/App.vue3
-rw-r--r--web/src/main.js3
-rw-r--r--web/src/router/index.js8
-rw-r--r--web/src/views/Blender.vue45
10 files changed, 118 insertions, 8 deletions
diff --git a/api/app/Http/Middleware/Auth0Middleware.php b/api/app/Http/Middleware/Auth0Middleware.php
index 19977b6..185c512 100644
--- a/api/app/Http/Middleware/Auth0Middleware.php
+++ b/api/app/Http/Middleware/Auth0Middleware.php
@@ -43,11 +43,9 @@ class Auth0Middleware
protected function tokenHasScope($token, $scopeRequired)
{
- if (empty($token['scope']))
+ if (empty($token['permissions']))
return false;
- $tokenScopes = explode(' ', $token['scope']);
-
- return in_array($scopeRequired, $tokenScopes);
+ return in_array($scopeRequired, $token['permissions']);
}
}
diff --git a/api/app/Http/Middleware/CorsMiddleware.php b/api/app/Http/Middleware/CorsMiddleware.php
new file mode 100644
index 0000000..4b0e757
--- /dev/null
+++ b/api/app/Http/Middleware/CorsMiddleware.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+
+class CorsMiddleware
+{
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Illuminate\Http\Request $request
+ * @param \Closure $next
+ * @return mixed
+ */
+ public function handle($request, Closure $next)
+ {
+ $headers = [
+ 'Access-Control-Allow-Origin' => '*',
+ 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
+ 'Access-Control-Allow-Credentials' => 'true',
+ 'Access-Control-Max-Age' => '86400',
+ 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With'
+ ];
+
+ if ($request->isMethod('OPTIONS'))
+ {
+ return response()->json('{"method":"OPTIONS"}', 200, $headers);
+ }
+
+ $response = $next($request);
+ foreach($headers as $key => $value)
+ {
+ $response->header($key, $value);
+ }
+
+ return $response;
+ }
+}
diff --git a/api/bootstrap/app.php b/api/bootstrap/app.php
index 74cd156..5f8a18c 100644
--- a/api/bootstrap/app.php
+++ b/api/bootstrap/app.php
@@ -80,6 +80,10 @@ $app->configure('app');
// 'auth' => App\Http\Middleware\Authenticate::class,
// ]);
+$app->middleware([
+ App\Http\Middleware\CorsMiddleware::class,
+]);
+
$app->routeMiddleware([
'auth' => App\Http\Middleware\Auth0Middleware::class,
]);
diff --git a/web/.gitignore b/web/.gitignore
new file mode 100644
index 0000000..d9794a6
--- /dev/null
+++ b/web/.gitignore
@@ -0,0 +1,6 @@
+.DS_Store
+node_modules/
+npm-debug.log
+.idea/
+dist/
+auth_config.json
diff --git a/web/package-lock.json b/web/package-lock.json
index cab9b6c..37b7b27 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -2502,6 +2502,14 @@
"integrity": "sha512-zg7Hz2k5lI8kb7U32998pRRFin7zJlkfezGJjUc2heaD4Pw2wObakCDVzkKztTm/Ln7eiVvYsjqak0Ed4LkMDA==",
"dev": true
},
+ "axios": {
+ "version": "0.21.0",
+ "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.0.tgz",
+ "integrity": "sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==",
+ "requires": {
+ "follow-redirects": "^1.10.0"
+ }
+ },
"babel-eslint": {
"version": "10.1.0",
"resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.1.0.tgz",
@@ -5478,8 +5486,7 @@
"follow-redirects": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz",
- "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==",
- "dev": true
+ "integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA=="
},
"for-in": {
"version": "1.0.2",
diff --git a/web/package.json b/web/package.json
index 5444389..68e35e3 100644
--- a/web/package.json
+++ b/web/package.json
@@ -9,6 +9,7 @@
},
"dependencies": {
"@auth0/auth0-spa-js": "^1.13.0",
+ "axios": "^0.21.0",
"core-js": "^3.6.5",
"vue": "^2.6.11",
"vue-router": "^3.2.0"
diff --git a/web/src/App.vue b/web/src/App.vue
index 86c1be0..4ad4374 100644
--- a/web/src/App.vue
+++ b/web/src/App.vue
@@ -3,7 +3,8 @@
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link> |
- <router-link v-if="$auth.isAuthenticated" to="/profile">Profile</router-link>
+ <router-link v-if="$auth.isAuthenticated" to="/profile">Profile</router-link> |
+ <router-link v-if="$auth.isAuthenticated" to="/blender">Blender</router-link>
</div>
<router-view/>
</div>
diff --git a/web/src/main.js b/web/src/main.js
index 857186e..9192c33 100644
--- a/web/src/main.js
+++ b/web/src/main.js
@@ -2,13 +2,14 @@ import Vue from 'vue'
import App from './App.vue'
import router from './router'
-import { domain, clientId } from "../auth_config.json";
+import { domain, clientId, audience } from "../auth_config.json";
import { Auth0Plugin } from "./auth";
Vue.use(Auth0Plugin, {
domain,
clientId,
+ audience,
onRedirectCallback: appState => {
router.push(appState && appState.targetUrl ? appState.targetUrl : window.location.pathname);
}
diff --git a/web/src/router/index.js b/web/src/router/index.js
index ad9501e..ab4075f 100644
--- a/web/src/router/index.js
+++ b/web/src/router/index.js
@@ -1,7 +1,9 @@
import Vue from 'vue'
+import { authGuard } from "../auth/authGuard";
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Profile from '../views/Profile.vue'
+import Blender from '../views/Blender.vue'
Vue.use(VueRouter)
@@ -24,6 +26,12 @@ const routes = [
name: "profile",
component: Profile,
beforeEnter: authGuard
+ },
+ {
+ path: "/blender",
+ name: "blender",
+ component: Blender,
+ beforeEnter: authGuard
}
]
diff --git a/web/src/views/Blender.vue b/web/src/views/Blender.vue
new file mode 100644
index 0000000..43299b7
--- /dev/null
+++ b/web/src/views/Blender.vue
@@ -0,0 +1,45 @@
+<template>
+ <div>
+ <button @click="start">Start</button> -
+ <button @click="stop">Stop</button>
+ <p>{{ apiMessage }}</p>
+ </div>
+</template>
+
+<script>
+import axios from "axios";
+
+export default {
+ name: "blender",
+ data() {
+ return {
+ apiMessage: ""
+ };
+ },
+ methods: {
+ async start() {
+ const token = await this.$auth.getTokenSilently();
+
+ const { data } = await axios.post("http://localhost:8000/api/v1/blenders/2/start",{ oxygen: 21 }, {
+ headers: {
+ Authorization: `Bearer ${token}`
+ }
+ });
+
+ this.apiMessage = data;
+ },
+ async stop() {
+ const token = await this.$auth.getTokenSilently();
+
+ const { data } = await axios.post("http://localhost:8000/api/v1/blenders/2/stop",{ oxygen: 21 }, {
+ headers: {
+ Authorization: `Bearer ${token}`
+ }
+ });
+
+ this.apiMessage = data;
+ }
+ }
+};
+
+</script>