Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit c7d8011

Browse files
committed
fixed ui and refactored axios into boot file
1 parent 4d6d0bb commit c7d8011

File tree

11 files changed

+59
-80
lines changed

11 files changed

+59
-80
lines changed

quasar/src/boot/axios.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
import axios from "axios";
22

3-
export default async ({ Vue }) => {
4-
Vue.prototype.$axios = axios;
3+
export default async ({ Vue, store, router }) => {
4+
const apiCall = axios.create({
5+
baseURL: process.env.API_URL
6+
});
7+
8+
apiCall.interceptors.request.use(
9+
config => {
10+
const c = config;
11+
if (store.getters.isAuthenticated) {
12+
c.headers.Authorization = `Bearer ${store.getters.getToken}`;
13+
}
14+
return c;
15+
},
16+
error => {
17+
Promise.reject(error);
18+
}
19+
);
20+
21+
function handleSuccess(response) {
22+
return { data: response.data };
23+
}
24+
25+
function handleError(error) {
26+
console.log(error);
27+
switch (error.response.status) {
28+
case 400:
29+
break;
30+
case 401:
31+
// Log out user, remove token, clear state and redirect to login
32+
store.dispatch("AUTH_LOGOUT").then(router.push("/"));
33+
break;
34+
case 404:
35+
// Show 404 page
36+
break;
37+
case 500:
38+
// Serveur Error redirect to 500
39+
break;
40+
default:
41+
// Unknow Error
42+
break;
43+
}
44+
return Promise.reject(error);
45+
}
46+
47+
apiCall.interceptors.response.use(handleSuccess, handleError);
48+
49+
Vue.prototype.$axios = apiCall;
550
};

quasar/src/components/auth/LoginForm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<q-form @submit="login">
2+
<q-form @submit.prevent="login">
33
<q-input
44
:color="$store.getters.isDark ? 'black' : 'primary'"
55
:dark="$store.getters.isDark"

quasar/src/layouts/primary/MainHeader.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export default {
8282
},
8383
methods: {
8484
setLang(lang) {
85-
console.log(lang);
8685
this.lang = lang;
8786
},
8887
logout() {

quasar/src/pages/Auth/Callback.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
<script>
66
import * as Cookies from "js-cookie";
7-
import apiCall from "../../utils/api";
87
export default {
98
methods: {
109
handleOauthCallback() {
1110
const provider = this.$route.params.provider;
12-
apiCall
11+
this.$axios
1312
.post(`/api/social/${provider}/`, { code: this.$route.query.code })
1413
.then(resp => {
1514
Cookies.set("refresh-token", resp.data.refresh);

quasar/src/pages/Auth/GitHub.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
<script>
66
import * as Cookies from "js-cookie";
7-
import apiCall from "../../utils/api";
87
export default {
98
methods: {
109
githubAuth() {
11-
apiCall
10+
this.$axios
1211
.post("/api/social/github/", { code: this.$route.query.code })
1312
.then(resp => {
1413
Cookies.set("refresh-token", resp.data.refresh);

quasar/src/pages/Auth/Google.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
<script>
88
import * as Cookies from "js-cookie";
9-
import apiCall from "../../utils/api";
109
export default {
1110
methods: {
1211
googleAuth() {
13-
apiCall
12+
this.$axios
1413
.post("/api/social/google/", {
1514
code: this.$route.query.code
1615
})

quasar/src/pages/Examples/Redis.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
</template>
2626

2727
<script>
28-
import apiCall from "../../utils/api.js";
2928
export default {
3029
data() {
3130
return {
@@ -42,20 +41,20 @@ export default {
4241
},
4342
methods: {
4443
clearCacheValue() {
45-
apiCall.delete("/api/debug/redis/").then(resp => {
44+
this.$axios.delete("/api/debug/redis/").then(resp => {
4645
console.log(resp);
4746
console.log("getting here..");
4847
this.valueFromCache = null;
4948
});
5049
},
5150
getCachedValue() {
52-
apiCall.get("/api/debug/redis/").then(resp => {
51+
this.$axios.get("/api/debug/redis/").then(resp => {
5352
this.valueFromCache = resp.data["count"];
5453
});
5554
},
5655
setCacheValue() {
5756
if (this.valueToSet) {
58-
apiCall
57+
this.$axios
5958
.post("/api/debug/redis/", { count: this.valueToSet })
6059
.then(resp => {
6160
this.valueFromCache = resp.data.count;

quasar/src/store/auth/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export const AUTH_ERROR = "AUTH_ERROR";
66
export const AUTH_LOGOUT = "AUTH_LOGOUT";
77
export const AUTH_REFRESH = "AUTH_REFRESH";
88

9-
import apiCall from "../../utils/api";
9+
// import axios from "axios";
10+
import Vue from "vue";
1011
import { Cookies } from "quasar";
1112
import { USER_REQUEST } from "../user";
1213

@@ -26,7 +27,7 @@ const actions = {
2627
[AUTH_REQUEST]: ({ commit, dispatch }, user) =>
2728
new Promise((resolve, reject) => {
2829
commit(AUTH_REQUEST);
29-
apiCall
30+
Vue.prototype.$axios
3031
.post("/api/auth/obtain_token/", user)
3132
.then(resp => {
3233
Cookies.set("refresh-token", resp.data.refresh);
@@ -51,7 +52,7 @@ const actions = {
5152
}),
5253
[AUTH_REFRESH]: ({ commit, dispatch }) =>
5354
new Promise((resolve, reject) => {
54-
apiCall
55+
Vue.prototype.$axios
5556
.post("/api/auth/refresh_token/", {
5657
refresh: Cookies.get("refresh-token")
5758
})

quasar/src/store/ui/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ const mutations = {
1919
state.isDark = !state.isDark;
2020
},
2121
setAuthPanel: (state, payload) => {
22-
console.log("change panel...");
23-
console.log(payload);
2422
state.authPanel = payload;
2523
},
2624
toggleLoginMenu: state => {
27-
console.log("firing toggleLoginMenu mutation");
2825
state.visible = !state.visible;
2926
},
3027
toggleLeftDrawer: (state, payload) => {
@@ -33,9 +30,6 @@ const mutations = {
3330
return;
3431
}
3532
state.leftDrawerOpen = !state.leftDrawerOpen;
36-
},
37-
setNextLink: (state, payload) => {
38-
state.nextLink = payload.nextLink;
3933
}
4034
};
4135

quasar/src/store/user/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const USER_ERROR = "USER_ERROR";
66

77
import Vue from "vue";
88
import { AUTH_LOGOUT } from "../auth";
9-
import apiCall from "../../utils/api";
109

1110
const state = {
1211
status: "",
@@ -20,7 +19,7 @@ const getters = {
2019

2120
const actions = {
2221
[USER_REQUEST]: ({ dispatch, commit }) => {
23-
apiCall
22+
Vue.prototype.$axios
2423
.get("/api/users/profile/")
2524
.then(resp => {
2625
const profile = resp.data;

0 commit comments

Comments
 (0)