5 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
6 | eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
7 | voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
8 | clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
9 | amet. Show me more.
10 |
11 |
12 | Eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
13 | voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
14 | clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
15 | amet.
16 |
17 |
18 |
19 |
20 |
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Implementing a Simple Middleware with Vue Router
2 |
3 | [](https://www.patreon.com/maoberlehner)
4 | [](https://paypal.me/maoberlehner)
5 |
6 | This is an example project for the following article: [Implementing a Simple Middleware with Vue Router](https://markus.oberlehner.net/blog/implementing-a-simple-middleware-with-vue-router/)
7 |
8 | ## Build Setup
9 |
10 | ```bash
11 | # Install dependencies.
12 | npm install
13 |
14 | # Serve with hot reload.
15 | npm run serve
16 |
17 | # Build for production with minification.
18 | npm run build
19 | ```
20 |
21 | ## About
22 |
23 | ### Author
24 |
25 | Markus Oberlehner
26 | Website: https://markus.oberlehner.net
27 | Twitter: https://twitter.com/MaOberlehner
28 | PayPal.me: https://paypal.me/maoberlehner
29 | Patreon: https://www.patreon.com/maoberlehner
30 |
31 | ### License
32 |
33 | MIT
34 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Markus Oberlehner
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/App.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 |
21 |
22 |
66 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | ## Reporting Issues
4 |
5 | Found a problem? Want a new feature?
6 |
7 | - See if your issue or idea has [already been reported].
8 | - Provide a [reduced test case] or a [live example].
9 |
10 | Remember, a bug is a *demonstrable problem* caused by *our* code.
11 |
12 | ## Submitting Pull Requests
13 |
14 | Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.
15 |
16 | 1. To begin, [fork this project], clone your fork, and add our upstream.
17 | ```bash
18 | # Clone your fork of the repo into the current directory
19 | git clone https://github.com//implementing-a-simple-middleware-with-vue-router
20 | # Navigate to the newly cloned directory
21 | cd implementing-a-simple-middleware-with-vue-router
22 | # Assign the original repo to a remote called "upstream"
23 | git remote add upstream https://github.com/maoberlehner/implementing-a-simple-middleware-with-vue-router
24 | # Install the tools necessary for development
25 | yarn install
26 | ```
27 |
28 | 2. Create a branch for your feature or hotfix:
29 | ```bash
30 | # Move into a new branch for a feature
31 | git checkout -b feature/thing
32 | ```
33 |
34 | ```bash
35 | # Move into a new branch for a hotfix
36 | git checkout -b hotfix/something
37 | ```
38 |
39 | 3. Push your branch up to your fork:
40 | ```bash
41 | # Push a feature branch
42 | git push origin feature/thing
43 | ```
44 |
45 | ```bash
46 | # Push a hotfix branch
47 | git push origin hotfix/something
48 | ```
49 |
50 | 4. Now [open a pull request] with a clear title and description.
51 |
52 | [already been reported]: https://github.com/maoberlehner/implementing-a-simple-middleware-with-vue-router/issues
53 | [fork this project]: https://github.com/maoberlehner/implementing-a-simple-middleware-with-vue-router/fork
54 | [live example]: http://codepen.io/pen
55 | [open a pull request]: https://help.github.com/articles/using-pull-requests/
56 | [reduced test case]: https://css-tricks.com/reduced-test-cases/
57 |
--------------------------------------------------------------------------------
/src/router.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue';
2 | import Router from 'vue-router';
3 |
4 | import auth from './middleware/auth';
5 | import log from './middleware/log';
6 |
7 | import Home from './views/Home.vue';
8 | import Login from './views/Login.vue';
9 | import User from './views/User.vue';
10 |
11 | Vue.use(Router);
12 |
13 | const router = new Router({
14 | routes: [
15 | {
16 | path: `/`,
17 | name: `home`,
18 | component: Home,
19 | meta: {
20 | middleware: log,
21 | },
22 | },
23 | {
24 | path: `/login`,
25 | name: `login`,
26 | component: Login,
27 | meta: {
28 | middleware: log,
29 | },
30 | },
31 | {
32 | path: `/user`,
33 | name: `user`,
34 | component: User,
35 | meta: {
36 | // Try to switch those two around to see how
37 | // this affects execution of the callbacks.
38 | middleware: [auth, log],
39 | },
40 | },
41 | ],
42 | mode: `history`,
43 | });
44 |
45 | // Creates a `nextMiddleware()` function which not only
46 | // runs the default `next()` callback but also triggers
47 | // the subsequent Middleware function.
48 | function nextFactory(context, middleware, index) {
49 | const subsequentMiddleware = middleware[index];
50 | // If no subsequent Middleware exists,
51 | // the default `next()` callback is returned.
52 | if (!subsequentMiddleware) return context.next;
53 |
54 | return (...parameters) => {
55 | // Run the default Vue Router `next()` callback first.
56 | context.next(...parameters);
57 | // Than run the subsequent Middleware with a new
58 | // `nextMiddleware()` callback.
59 | const nextMiddleware = nextFactory(context, middleware, index + 1);
60 | subsequentMiddleware({ ...context, next: nextMiddleware });
61 | };
62 | }
63 |
64 | router.beforeEach((to, from, next) => {
65 | if (to.meta.middleware) {
66 | const middleware = Array.isArray(to.meta.middleware)
67 | ? to.meta.middleware
68 | : [to.meta.middleware];
69 |
70 | const context = {
71 | from,
72 | next,
73 | router,
74 | to,
75 | };
76 | const nextMiddleware = nextFactory(context, middleware, 1);
77 |
78 | return middleware[0]({ ...context, next: nextMiddleware });
79 | }
80 |
81 | return next();
82 | });
83 |
84 | export default router;
85 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | - Using welcoming and inclusive language
12 | - Being respectful of differing viewpoints and experiences
13 | - Gracefully accepting constructive criticism
14 | - Focusing on what is best for the community
15 | - Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | - The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | - Trolling, insulting/derogatory comments, and personal or political attacks
21 | - Public or private harassment
22 | - Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | - Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at markus.oberlehner@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------