Commit 205c8dde authored by Le Dinh Trung's avatar Le Dinh Trung

Merge branch 'hotfix/axios-api' into 'dev'

Hotfix/axios api

See merge request !15
parents 9a6040a1 035cdff9
......@@ -43,22 +43,26 @@
export default {
methods: {
logout () {
fetch('http://127.0.0.1:8000/api/logout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
accept: 'application/json'
},
body: JSON.stringify({
async logout () {
try {
const resp = await this.$axios.post('/logout', {
token: this.$auth.$storage.getUniversal('token')
})
if (resp.status == '200') {
this.$toast.success('Logout!', {
duration: 2000
})
this.$router.push('/login')
localStorage.setItem('token', '')
this.$auth.$storage.setUniversal('loggedIn', 'false')
this.$auth.$storage.setUniversal('token', '')
this.$auth.$storage.setUniversal('userName', '')
this.$router.push('/login')
}
} catch (e) {
this.$toast.error('ERR !', {
duration: 2000
})
}
}
}
}
......
......@@ -109,42 +109,33 @@ export default {
this.password = ''
this.checkbox = false
},
async login () {
async login() {
try {
const resp = await fetch('http://127.0.0.1:8000/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify({
const resp = await this.$axios.post('/login',
{
email: this.email,
password: this.password,
status: this.status
})
}).then((response) => {
return response.json()
})
console.log(resp.status)
localStorage.setItem('token', 'Bearer ' + resp.data.bearer_token)
this.$auth.$storage.setUniversal('token', 'Bearer ' + resp.data.bearer_token)
this.$auth.$storage.setUniversal('userName', resp.data.name)
this.$auth.$storage.setUniversal('loggedIn', 'true')
if (resp.status == 'success') {
this.$toast.success('Successfully authenticated', {
duration: 2000
})
this.$router.push('home')
localStorage.setItem("token", resp.data.data.bearer_token)
this.$auth.$storage.setUniversal("token", resp.data.data.bearer_token)
this.$auth.$storage.setUniversal("userName", resp.data.data.name)
this.$auth.$storage.setUniversal("loggedIn", "true")
if (resp.status == "200") {
this.$toast.success("Successfully authenticated", {
duration: 2000,
});
this.$router.push("home");
}
} catch (e) {
this.$toast.error('Username or Password not valid', {
duration: 2000
})
this.$router.push('/login')
}
this.$toast.error("Username or Password not valid", {
duration: 2000,
});
this.$router.push("/login");
}
}
}
}
</script>
<style scoped>
.body {
......
export default function ({ $axios }, inject, redirect) {
$axios.onError(error => {
if(error.response.status === 500) {
$axios.onError((error) => {
if (error.response.status === 500) {
redirect('/500')
}
})
$axios.onError(error => {
$axios.onError((error) => {
const code = parseInt(error.response && error.response.status)
if (code === 400) {
redirect('/400')
......@@ -15,7 +15,8 @@ export default function ({ $axios }, inject, redirect) {
const api = $axios.create({
headers: {
common: {
Accept: 'application/json, text/plain, */*'
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json, text/plain, */*'
}
}
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment