Skip to content

Instantly share code, notes, and snippets.

@LuisHCK
Created November 29, 2018 06:02
Show Gist options
  • Save LuisHCK/82823c55afccf91a02a1cfdbc5ca536d to your computer and use it in GitHub Desktop.
Save LuisHCK/82823c55afccf91a02a1cfdbc5ca536d to your computer and use it in GitHub Desktop.
<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>
<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