Last active
August 21, 2024 13:23
-
-
Save clint74/41404be6362b21338aff06b5a9277217 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineStore } from 'pinia' | |
/* store/auth.js */ | |
export const useAuthStore = defineStore('auth', { | |
state: () => ({ | |
token: '' | |
}), | |
getters: { | |
getAuthToken (state) { | |
return state.token | |
} | |
}, | |
actions: { | |
setAuthToken (token) { | |
this.token = token | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* boot/axios.js */ | |
import { boot } from 'quasar/wrappers' | |
import axios from 'axios' | |
import {useAuthStore} from "stores/auth"; | |
// Be careful when using SSR for cross-request state pollution | |
// due to creating a Singleton instance here; | |
// If any client changes this (global) instance, it might be a | |
// good idea to move this instance creation inside of the | |
// "export default () => {}" function below (which runs individually | |
// for each client) | |
const api = axios.create({ baseURL: process.env.API_URL }) | |
export default boot(({ app }) => { | |
// for use inside Vue files (Options API) through this.$axios and this.$api | |
app.config.globalProperties.$axios = axios | |
// ^ ^ ^ this will allow you to use this.$axios (for Vue Options API form) | |
// so you won't necessarily have to import axios in each vue file | |
axios.defaults.baseURL = process.env.API_URL | |
axios.defaults.timeout = 0 | |
const store = useAuthStore() | |
axios.interceptors.request.use((config) => { | |
debugger | |
if (config.url.includes('/auth/obtain_token/')) { | |
config.headers['content-type'] = 'application/json' | |
} | |
else { | |
config.headers['content-type'] = 'application/json' | |
config.headers['authorization'] = `Token ${store.getAuthToken}` | |
} | |
return config | |
}, | |
(error) => { | |
return Promise.reject(error) | |
}); | |
app.config.globalProperties.$api = api | |
// ^ ^ ^ this will allow you to use this.$api (for Vue Options API form) | |
// so you can easily perform requests against your app's API | |
}); | |
export { api } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {api} from "boot/axios"; | |
import { storeToRefs } from 'pinia' | |
import {useAuthStore} from "stores/auth"; | |
import { ref } from 'vue' | |
const usuario = ref('teste') | |
const senha = ref('teste@#12##') | |
const exibe_ajuda = ref(false) | |
const store = useAuthStore() | |
const submit = () => { | |
let data = { | |
username: usuario.value, | |
password: senha.value | |
} | |
console.log(api) | |
api.post('api-token-auth/', data) | |
.then(response => { | |
const token = response.data.tokens[0] | |
store.setAuthToken(token) | |
// console.log(store.token) | |
// let x = store.getAuthToken | |
// console.log(x) | |
}) | |
.catch(error => { | |
console.log(error) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment