Created
November 29, 2018 06:02
-
-
Save LuisHCK/82823c55afccf91a02a1cfdbc5ca536d to your computer and use it in GitHub Desktop.
Revisions
-
LuisHCK created this gist
Nov 29, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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>