Skip to content

Instantly share code, notes, and snippets.

@rico
Last active February 11, 2018 14:42
Show Gist options
  • Save rico/57b1b5a6af51ff20eee0c9ee4a6ed8e3 to your computer and use it in GitHub Desktop.
Save rico/57b1b5a6af51ff20eee0c9ee4a6ed8e3 to your computer and use it in GitHub Desktop.

Revisions

  1. rico revised this gist Feb 11, 2018. 1 changed file with 36 additions and 0 deletions.
    36 changes: 36 additions & 0 deletions router.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // File: router/index.js

    import Vue from 'vue'
    import VueRouter from 'vue-router';
    import Login from '../pages/Login';
    import Dashboard from '../pages/Dashboard';
    import '../providers/authorize';

    Vue.use(VueRouter);

    const routes = [
    {
    path: '/',
    component: Dashboard,
    meta: {
    permissions: {
    roles: ['user'],
    redirectTo: '/login'
    }
    }
    },
    {
    path: '/login',
    component: Login,
    meta: {
    permissions: {
    roles: ['guest'],
    redirectTo: '/'
    }
    }
    },
    ];

    export default new VueRouter({
    routes // short for `routes: routes`
    });
  2. rico revised this gist Feb 11, 2018. 1 changed file with 47 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions authorize.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    // File: providers/authorize.js

    import Vue from "vue";
    import auth from './authenticate'
    import { VueAuthorize } from 'vue-authorize'

    import IsAuthorizedComponent from "vue-authorize/src/component";
    import IsAuthorizedDirective from "vue-authorize/src/directive";


    const authorize = new VueAuthorize(auth, {
    roles: {
    user: {
    handler: function () {

    console.log('authorize.js 10 => user');

    return this.$auth.isAuthorized()
    },
    permissions: []
    },
    guest: {
    handler: function () {

    console.log('authorize.js 19 => guest');

    return !this.$auth.isAuthorized()
    },
    permissions: []
    }
    },

    permissions: {}
    });

    Vue.directive('isAuthorized', IsAuthorizedDirective);
    Vue.component('isAuthorized', IsAuthorizedComponent);

    Object.defineProperties(Vue.prototype, {
    $authorize: {
    get() {
    return authorize
    }
    }
    });

    export default authorize;
  3. rico revised this gist Feb 11, 2018. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions Login.vue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    // Component: Login.vue
    data: () => ({
    credentials: {
    username: '',
    password: '',
    },
    errors: {},
    loading:false
    }),
    methods : {
    login: function () {

    this.errors = {};
    this.loading = true;
    this.$store.dispatch('login', {credentials: this.credentials})
    .then(() => {
    this.loading = false;
    this.$router.push('/');
    })
    .catch((e) => {

    if (e.response.status == 422) {
    this.errors = e.response.data.errors;
    } else if (e.response.status == 401) {
    this.errors.message = 'Kein Benutzer mit diesen Angaben gefunden.'
    }

    this.loading = false;
    });
    }
    }
  4. rico created this gist Feb 11, 2018.
    64 changes: 64 additions & 0 deletions auth.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    // File: store/modules/auth.js

    import authProvider from '../../providers/authenticate'
    import http from '../../providers/http'

    // You can use it as state property
    const state = {
    isAuthenticated: false,
    user: null
    };

    const getters = {
    isAuthenticated () {
    return authProvider.isAuthenticated()
    },
    user () {
    return state.user
    }
    };

    const mutations = {
    isAuthenticated (state, payload) {
    state.isAuthenticated = payload.isAuthenticated
    },
    user (state, payload) {
    state.user = payload;
    }
    };

    const actions = {

    // Perform VueAuthenticate login using Vuex actions
    login (context, payload) {

    return new Promise((resolve, reject) => {
    authProvider.login(payload.credentials).then((response) => {


    context.commit('isAuthenticated', {
    isAuthenticated: authProvider.isAuthenticated()
    });

    http.get('/auth/user')
    .then(function (response) {
    context.commit('user', response.data);

    resolve();
    });

    })
    .catch((err) => {
    reject(err);
    });
    });

    }
    };

    export default {
    state,
    getters,
    actions,
    mutations
    }
    10 changes: 10 additions & 0 deletions authenticate.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    // File: providers/authenticate.js

    import Vue from 'vue'
    import { VueAuthenticate } from 'vue-authenticate'
    import http from "./http";

    export default new VueAuthenticate(Vue.prototype.$http, {
    loginUrl: 'auth/user/login',
    tokenName: 'access_token'
    });
    10 changes: 10 additions & 0 deletions https.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    // File: providers/http.js

    import Vue from "vue";
    import axios from "axios/index";
    import VueAxios from "vue-axios";

    Vue.use(VueAxios, axios);
    Vue.axios.defaults.baseURL = 'http://local.test';

    export default Vue.axios;
    14 changes: 14 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    // File: store/index.js

    import Vue from 'vue'
    import Vuex from 'vuex'

    Vue.use(Vuex);

    import auth from './modules/auth'

    export default new Vuex.Store({
    modules: {
    auth
    },
    });