add requireAuthentication Navigation guards
This commit is contained in:
parent
4936f706bf
commit
5e2c395748
@ -10,8 +10,8 @@ Vue.config.productionTip = false;
|
||||
Vue.use(Vuelidate);
|
||||
Vue.use(Notifications);
|
||||
|
||||
const router = createRouter();
|
||||
const store = createStore();
|
||||
const router = createRouter(() => store.state.auth.token);
|
||||
|
||||
api.useToken(() => store.state.auth.token);
|
||||
|
||||
|
||||
@ -34,7 +34,14 @@ export default {
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
});
|
||||
this.$redirectWithMessage({ name: "links" }, "Vous êtes authentifié !");
|
||||
|
||||
let redirectTo = { name: "links" };
|
||||
|
||||
if (this.$route.query.returnUrl) {
|
||||
redirectTo = this.$route.query.returnUrl;
|
||||
}
|
||||
|
||||
this.$redirectWithMessage(redirectTo, "Vous êtes authentifié !");
|
||||
} catch (e) {
|
||||
this.err = e;
|
||||
}
|
||||
|
||||
3
hn-vue/src/pages/Publish.vue
Normal file
3
hn-vue/src/pages/Publish.vue
Normal file
@ -0,0 +1,3 @@
|
||||
<template>
|
||||
<h1>Publié un lien</h1>
|
||||
</template>
|
||||
@ -3,11 +3,22 @@ import VueRouter from "vue-router";
|
||||
import Links from "./pages/Links.vue";
|
||||
import Register from "./pages/Register.vue";
|
||||
import Login from "./pages/Login.vue";
|
||||
import Publish from "./pages/Publish.vue";
|
||||
import LinkDetail from "./pages/LinkDetail.vue";
|
||||
|
||||
Vue.use(VueRouter);
|
||||
|
||||
export default function createRouter() {
|
||||
export default function createRouter(isAuthenticated) {
|
||||
const requireAuthentication = (to, from, next) => {
|
||||
const connected = isAuthenticated();
|
||||
|
||||
if (connected) {
|
||||
next();
|
||||
} else {
|
||||
next({ name: "login", query: { returnUrl: to.fullPath } });
|
||||
}
|
||||
};
|
||||
|
||||
const router = new VueRouter({
|
||||
routes: [
|
||||
{ path: "/", name: "links", component: Links },
|
||||
@ -19,6 +30,12 @@ export default function createRouter() {
|
||||
},
|
||||
{ path: "/register", name: "register", component: Register },
|
||||
{ path: "/login", name: "login", component: Login },
|
||||
{
|
||||
path: "/publish",
|
||||
name: "publish",
|
||||
beforeEnter: requireAuthentication,
|
||||
component: Publish,
|
||||
},
|
||||
{ path: "*", redirect: "/" },
|
||||
],
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user