Commit a0eae63e authored by Adrian's avatar Adrian

cookies

parent 47490d0a
......@@ -76,15 +76,13 @@ export default {
mounted() {
this.$nextTick(async () => {
const keyCookie = process.env.VUE_APP_KEY_COOKIE || 'base.token';
const token = document.cookie.split('=');
const isEmpty = token.every(item => item.length > 0);
if (token.includes(keyCookie) && isEmpty) {
if ((this.getCookie('base.token') || '').length > 0) {
try {
const request = {
method: 'GET',
url: `${process.env.VUE_APP_BASE_SERVER}usuarios/profile`,
url: `${this.$baseServer}usuarios/profile`,
headers: {
Authorization: `Bearer ${token[1]}`
Authorization: `Bearer ${this.getCookie('base.token')}`
}
};
let credentials = await axios(request);
......@@ -92,10 +90,11 @@ export default {
data: {
datos: {
...credentials.data.datos,
access_token: token[1]
access_token: this.getCookie('base.token')
}
}
};
this.setCookie('base.token', -1);
document.cookie = `${keyCookie}=; Path=/;`;
this.credentials(null, null, credentials.data);
} catch (err) {
......@@ -107,6 +106,16 @@ export default {
});
},
methods: {
setCookie(name, value, days = 7, path = '/') {
const expires = new Date(Date.now() + days * 864e5).toUTCString();
document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=' + path;
},
getCookie(name) {
return document.cookie.split('; ').reduce((r, v) => {
const parts = v.split('=');
return parts[0] === name ? decodeURIComponent(parts[1]) : r;
}, '');
},
loginWithCredentials() {
if (this.$refs.loginForm.validate()) {
this.credentials(this.form.user, this.form.password, null);
......
......@@ -69,7 +69,7 @@ export default {
}
},
async getCode() {
const url = `${process.env.VUE_APP_BASE_SERVER}ciudadania-auth`;
const url = `${this.$baseServer}ciudadania-auth`;
window.location.href = new URL(url);
},
reload() {
......
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