This tutorial is an advanced tutorial from my tutorial on JWT. you can see how to create a server-side JWT API through this tutorial authentication. I
will use the API that I have created in the previous tutorial. This time I will consume API in client side by using framework Vue.js.
There are several stages that we will do to ready to consume JWT:
Because we will use tokens as authentication, of course, we must save tokens on the client side. the question is where will we store the token?.
Where will we store the token?
There are two options we can do to store tokens. ie using local storage or using a cookie. but in my opinion, to be more secure I will use a cookie. but of course, you can look for other references.
npm install vue-cookie --save
and if you have not installed vue, you can install vue and vue-cookies simultaneously like this:
npm install vue vue-cookie --save
noted: Make sure you have NPM init.
Then you can use it in main.js
import Vue from 'vue'
import VueCookie from 'vue-cookie'
Vue.use(VueCookie )
in this way you can use vue-cookies globally
<template>
<div id="app">
<h1>Vue JWT Client</h1>
<form v-on:submit.prevent="onSubmit">
<input type="email" name="email" v-model="email">
<input type="password" name="password" v-model="password">
<input type="submit" name="submit" value="Login">
</form>
</div>
</template>
<script>
export default {
data(){
return{
email:'',
password:''
}
}
}
</script>
< form > element. We can create an event by v-on or shorthand '@' (v-on.submit or Example:
data(){
return{
email:'',
password:''
}
}
We will request ajax to the server with the endpoint we created earlier in the server.
Get User http:/localhost:3000/api/users
Profile http:/localhost:3000/api/profile
npm install axioslocally
import axios from 'axios'
You can import axios only in the files you want to use axios. in this tutorial, I'm using it in app.vue.
Globally
import Vue from 'vue'
import axios from 'axios'
Vue.use(axios );
You can import axios in main.js and use Vue.use (axios), this way you do not need to import them one by one in each component.
noted: using globally may affect the speed.
You can see more details in my tutorial on axios and vue
< form v-on:submit.prevent="onSubmit" >,onSubmit (). I will use axios locally so I need to import in the component which i will use. The endpoint we will use is Login http:/localhost:3000/api/loginExample:
onSubmit(){
axios.post('http:/localhost:3000/api/login', {
email: this.email,
password: this.password
})
.then(res=>{
this.$cookie.set('token',res.data.token);
})
.catch(err=>{
console.log(error);
});
}
1. The first parameter: The first parameter is the URL or endpoint to be addressed. for our login API endpoint is HTTP:/localhost:3000/api/login.
2. The Second parameter: The second parameter is the object that contains the key and value we will post to the endpoint.in this tutorial key and value, we will post is {email: this.email, password: this.password. The key is email and password. The value is this.email and this.password. We use v-model that has binding input with the local variable, therefore we need to access the local variable in data (). to access it, we just need to add this.
this.$cookie.set('token',res.data.token);: We will save the result from post data to endpoint http:/localhost/api/login, to save we can use the set () method, the set method has two mandatory parameters namely key and value. The key is 'token' and the value is obtained from res.data.token. The res comes from the callback function
.then(res=>{}. If we look at the inspect element in the response section we can see the results of the API:
Get User Data with API JWT
I will create a function to retrieve user data from endpoint Get User http:/localhost:3000/api/users. I will put the function in the on-click event on the < button v-on:click="getuser()" >Get user< /button >.
Example:
getuser(){
axios.get('http:/localhost:3000/api/users', {
'headers': { 'Authorization': this.$cookie.get('token') }
})
.then(res=>{
console.log(res)
})
.catch(err=>{
console.log(error);
});
}
'headers': { 'Authorization': this.$cookie.get('token') }, The key is 'Authorization', and the tokens can be found in the cookie by this.$cookie.get('token').< button v-on:click="profile()" >Get Profile< /button >.Example:
profile(){
axios.get('http:/localhost:3000/api/profile', {
'headers': { 'Authorization': this.$cookie.get('token') }
})
.then(res=>{
console.log(res)
})
.catch(err=>{
console.log(error);
});
}
'headers': { 'Authorization': this.$cookie.get('token') }, The key is 'Authorization', and the tokens can be found in the cookie by this.$cookie.get('token').Note: make sure the server nodes and mongod databases you have run, as long as you interact with the endpoint or API that we created in the previous tutorial, if you are confused you can follow some tutorials below.
API JWT
Vue.js