forked from DebaucheryLibrarian/traxxx
49 lines
810 B
JavaScript
49 lines
810 B
JavaScript
import { get, post, del } from '../api';
|
|
|
|
function initAuthActions(store, _router) {
|
|
async function fetchMe({ commit }) {
|
|
try {
|
|
const user = await get('/session');
|
|
|
|
commit('setUser', user);
|
|
|
|
return user;
|
|
} catch (error) {
|
|
// continue as guest
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function login({ commit }, credentials) {
|
|
const user = await post('/session', credentials);
|
|
|
|
commit('setUser', user);
|
|
await store.dispatch('fetchNotifications');
|
|
|
|
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;
|