40 行
1.1 KiB
JavaScript
40 行
1.1 KiB
JavaScript
import Cookie from 'js-cookie'
|
|
|
|
const LOGIN_URL = '/api/auth/login'
|
|
const SIGNUP_URL = '/api/auth/register'
|
|
|
|
export default {
|
|
domain: '076.ne.jp',
|
|
user: { id: 0, authenticated: false },
|
|
login (creds) {
|
|
axios.post(LOGIN_URL, creds).then(res => {
|
|
if (creds.username === res.data.username && creds.password === res.data.rawPassword) {
|
|
Cookie.set('kero_token', res.data.kero_token, { expires: 365, domain: this.domain, path: '/' });
|
|
|
|
this.user.authenticated = true;
|
|
window.location.reload();
|
|
}
|
|
});
|
|
},
|
|
signup (creds) { axios.post(SIGNUP_URL, creds).then(res => { if (res.data[0] === '1') this.login(creds); }); },
|
|
logout () {
|
|
Cookie.remove('kero_token', { domain: this.domain, path: '/' });
|
|
this.user.authenticated = false;
|
|
window.location.reload();
|
|
},
|
|
checkAuth () {
|
|
const tok = Cookie.get('kero_token');
|
|
|
|
axios.post('/api/auth/checkauth', { kero_token: tok }).then(res => {
|
|
if (tok === res.data.kero_token) {
|
|
this.user.id = res.data.id;
|
|
this.user.authenticated = true;
|
|
}
|
|
else {
|
|
this.user.id = 0;
|
|
this.user.authenticated = false;
|
|
}
|
|
})
|
|
}
|
|
}
|