48 lines
765 B
JavaScript
48 lines
765 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);
|
|
|
|
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;
|