Created
November 29, 2018 06:02
-
-
Save LuisHCK/82823c55afccf91a02a1cfdbc5ca536d to your computer and use it in GitHub Desktop.
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 characters
<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 characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment