traxxx/assets/js/auth/actions.js

49 lines
810 B
JavaScript
Raw Normal View History

2021-03-13 03:26:24 +00:00
import { get, post, del } from '../api';
2021-04-28 23:45:01 +00:00
function initAuthActions(store, _router) {
2021-03-13 03:26:24 +00:00
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);
2021-04-28 23:45:01 +00:00
await store.dispatch('fetchNotifications');
2021-03-13 03:26:24 +00:00
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,
};
}
export default initAuthActions;