2021-03-13 03:26:24 +00:00
|
|
|
import { get, post, del } from '../api';
|
|
|
|
|
|
|
|
function initAuthActions(_store, _router) {
|
|
|
|
async function fetchMe({ commit }) {
|
2021-03-15 02:30:47 +00:00
|
|
|
try {
|
|
|
|
const user = await get('/session');
|
2021-03-13 03:26:24 +00:00
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
commit('setUser', user);
|
2021-03-13 03:26:24 +00:00
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
return user;
|
|
|
|
} catch (error) {
|
|
|
|
// continue as guest
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-13 03:26:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function login({ commit }, credentials) {
|
|
|
|
const user = await post('/session', credentials);
|
|
|
|
|
|
|
|
commit('setUser', user);
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function signup({ commit }, credentials) {
|
|
|
|
const user = await post('/users', credentials);
|
|
|
|
|
|
|
|
commit('setUser', user);
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function logout({ commit }) {
|
|
|
|
await del('/session');
|
|
|
|
|
|
|
|
commit('setUser', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
fetchMe,
|
|
|
|
login,
|
|
|
|
logout,
|
|
|
|
signup,
|
|
|
|
};
|
|
|
|
}
|
2019-06-03 03:31:38 +00:00
|
|
|
|
|
|
|
export default initAuthActions;
|