adding latest
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import Vue from "vue";
|
||||
import VueRouter from "vue-router";
|
||||
// import Home from "../views/Home.vue";
|
||||
import Home from "../views/Home.vue";
|
||||
import { AmplifyEventBus } from "aws-amplify-vue";
|
||||
import { getUser } from "@/utils/auth.js";
|
||||
|
||||
Vue.use(VueRouter);
|
||||
|
||||
const routes = [
|
||||
// {
|
||||
// path: "/",
|
||||
// name: "Home",
|
||||
// component: Home
|
||||
// },
|
||||
{
|
||||
path: "/",
|
||||
name: "Home",
|
||||
component: Home,
|
||||
meta: { requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: "/about",
|
||||
name: "About",
|
||||
@@ -23,19 +26,22 @@ const routes = [
|
||||
path: "/signUp",
|
||||
name: "signUp",
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "signup" */ "../views/SignUp.vue")
|
||||
import(/* webpackChunkName: "signup" */ "../views/SignUp.vue"),
|
||||
meta: { requiresAuth: false }
|
||||
},
|
||||
{
|
||||
path: "/signUpConfirm",
|
||||
name: "signUpConfirm",
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "confirm" */ "../views/SignUpConfirm.vue")
|
||||
import(/* webpackChunkName: "confirm" */ "../views/SignUpConfirm.vue"),
|
||||
meta: { requiresAuth: false }
|
||||
},
|
||||
{
|
||||
path: "/signIn",
|
||||
name: "signIn",
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "signin" */ "../views/SignIn.vue")
|
||||
import(/* webpackChunkName: "signin" */ "../views/SignIn.vue"),
|
||||
meta: { requiresAuth: false }
|
||||
}
|
||||
];
|
||||
|
||||
@@ -45,4 +51,59 @@ const router = new VueRouter({
|
||||
routes
|
||||
});
|
||||
|
||||
getUser().then(user => {
|
||||
console.log("router getting user.")
|
||||
if (user) {
|
||||
router.push({ path: "/" });
|
||||
}
|
||||
});
|
||||
|
||||
AmplifyEventBus.$on("authState", async state => {
|
||||
const pushPathes = {
|
||||
signedOut: () => {
|
||||
router.push({ path: "/signIn" });
|
||||
},
|
||||
signUp: () => {
|
||||
router.push({ path: "/signUp" });
|
||||
},
|
||||
confirmSignUp: () => {
|
||||
router.push({ path: "/signUpConfirm" });
|
||||
},
|
||||
signIn: () => {
|
||||
router.push({ path: "/signIn" });
|
||||
},
|
||||
signedIn: () => {
|
||||
router.push({ path: "/" });
|
||||
}
|
||||
};
|
||||
if (typeof pushPathes[state] === "function") {
|
||||
pushPathes[state]();
|
||||
}
|
||||
});
|
||||
|
||||
router.beforeResolve(async (to, from, next) => {
|
||||
console.log("router before resolve getting current user.")
|
||||
const user = await getUser();
|
||||
if (!user) {
|
||||
if (to.matched.some(record => record.meta.requiresAuth)) {
|
||||
return next({
|
||||
path: "/signIn"
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
to.matched.some(
|
||||
record =>
|
||||
typeof record.meta.requiresAuth === "boolean" &&
|
||||
!record.meta.requiresAuth
|
||||
)
|
||||
) {
|
||||
return next({
|
||||
path: "/"
|
||||
});
|
||||
}
|
||||
}
|
||||
return next();
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user