Skip to content

Instantly share code, notes, and snippets.

@LuisHCK
Created November 29, 2018 06:02
Show Gist options
  • Select an option

  • Save LuisHCK/82823c55afccf91a02a1cfdbc5ca536d to your computer and use it in GitHub Desktop.

Select an option

Save LuisHCK/82823c55afccf91a02a1cfdbc5ca536d to your computer and use it in GitHub Desktop.

Revisions

  1. LuisHCK created this gist Nov 29, 2018.
    48 changes: 48 additions & 0 deletions Login.vue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <template>
    <div>
    <form @submit.prevent="login()">
    <input v-model="email" type="text">
    <input v-model="password" type="password">
    </form>
    </div>
    </template>

    <script>
    // Importamos Axios
    import axios from "axios";
    export default {
    data() {
    return {
    email: "",
    password: ""
    };
    },
    methods: {
    /**
    * Ralizamos la petición POST a la ruta que definimos
    * en el archivo routes.rb
    */
    login() {
    axios
    .post("localhost:3000/login", {
    // Es obligatorio enviar los datos siguiendo esta
    auth: { email: this.email, password: this.password }
    })
    // Si los datos son correcto procederemos a guardar nuestro token
    .then(response => {
    localStorage.setItem("jwt", response.data.jwt);
    })
    // Si falla manejaremos el error
    // en este caso mostraremos el error en la consola
    .catch(error => {
    console.log(error);
    });
    }
    }
    };
    </script>

    <style scoped>
    </style>
    35 changes: 35 additions & 0 deletions Posts.vue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <template>
    <div>
    <div v-for="post in posts" :key="post.id">
    {{post.text}}
    </div>
    </div>
    </template>

    <script>
    import axios from axios
    export default {
    data() {
    return {
    posts: []
    }
    },
    methods: {
    getPosts() {
    let token = localstorage.getItem('token')
    axios.get('localhost:3000/posts', {headers: {'Authorization': token}})
    .then(response => {
    this.posts = response.data
    })
    .catch(error => {
    console.log(error)
    })
    }
    }
    };
    </script>

    <style scoped>
    </style>