This repository was archived by the owner on Mar 27, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 11 files changed +59
-80
lines changed
Expand file tree Collapse file tree 11 files changed +59
-80
lines changed Original file line number Diff line number Diff line change 11import 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} ;
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff 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 () {
Original file line number Diff line number Diff line change 44
55<script >
66import * as Cookies from " js-cookie" ;
7- import apiCall from " ../../utils/api" ;
87export 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 );
Original file line number Diff line number Diff line change 44
55<script >
66import * as Cookies from " js-cookie" ;
7- import apiCall from " ../../utils/api" ;
87export 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 );
Original file line number Diff line number Diff line change 66
77<script >
88import * as Cookies from " js-cookie" ;
9- import apiCall from " ../../utils/api" ;
109export default {
1110 methods: {
1211 googleAuth () {
13- apiCall
12+ this . $axios
1413 .post (" /api/social/google/" , {
1514 code: this .$route .query .code
1615 })
Original file line number Diff line number Diff line change 2525</template >
2626
2727<script >
28- import apiCall from " ../../utils/api.js" ;
2928export 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 ;
Original file line number Diff line number Diff line change @@ -6,7 +6,8 @@ export const AUTH_ERROR = "AUTH_ERROR";
66export const AUTH_LOGOUT = "AUTH_LOGOUT" ;
77export const AUTH_REFRESH = "AUTH_REFRESH" ;
88
9- import apiCall from "../../utils/api" ;
9+ // import axios from "axios";
10+ import Vue from "vue" ;
1011import { Cookies } from "quasar" ;
1112import { 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 } )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -6,7 +6,6 @@ export const USER_ERROR = "USER_ERROR";
66
77import Vue from "vue" ;
88import { AUTH_LOGOUT } from "../auth" ;
9- import apiCall from "../../utils/api" ;
109
1110const state = {
1211 status : "" ,
@@ -20,7 +19,7 @@ const getters = {
2019
2120const 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 ;
You can’t perform that action at this time.
0 commit comments