Added user sign up and login.
This commit is contained in:
@@ -39,6 +39,22 @@ async function post(endpoint, data) {
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
|
||||
async function del(endpoint) {
|
||||
const res = await fetch(`${config.api.url}${endpoint}`, {
|
||||
method: 'DELETE',
|
||||
mode: 'cors',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const errorMsg = await res.text();
|
||||
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
|
||||
async function graphql(query, variables = null) {
|
||||
const res = await fetch('/graphql', {
|
||||
method: 'POST',
|
||||
@@ -67,5 +83,6 @@ async function graphql(query, variables = null) {
|
||||
export {
|
||||
get,
|
||||
post,
|
||||
del,
|
||||
graphql,
|
||||
};
|
||||
|
||||
@@ -1,3 +1,42 @@
|
||||
function initAuthActions(_store, _router) {}
|
||||
import { get, post, del } from '../api';
|
||||
|
||||
function initAuthActions(_store, _router) {
|
||||
async function fetchMe({ commit }) {
|
||||
const user = await get('/session');
|
||||
|
||||
commit('setUser', user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -1 +1,7 @@
|
||||
export default {};
|
||||
function setUser(state, user) {
|
||||
state.user = user;
|
||||
}
|
||||
|
||||
export default {
|
||||
setUser,
|
||||
};
|
||||
|
||||
5
assets/js/auth/observers.js
Normal file
5
assets/js/auth/observers.js
Normal file
@@ -0,0 +1,5 @@
|
||||
async function initAuthObserver(store, _router) {
|
||||
await store.dispatch('fetchMe');
|
||||
}
|
||||
|
||||
export default initAuthObserver;
|
||||
@@ -1,4 +1,3 @@
|
||||
export default {
|
||||
authenticated: false,
|
||||
user: null,
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@ import mitt from 'mitt';
|
||||
import router from './router';
|
||||
import initStore from './store';
|
||||
import initUiObservers from './ui/observers';
|
||||
import initAuthObservers from './auth/observers';
|
||||
|
||||
import { formatDate, formatDuration } from './format';
|
||||
|
||||
@@ -64,6 +65,7 @@ async function init() {
|
||||
}
|
||||
|
||||
initUiObservers(store, router);
|
||||
initAuthObservers(store, router);
|
||||
|
||||
if (window.env.sfw) {
|
||||
store.dispatch('setSfw', true);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
|
||||
import Home from '../components/home/home.vue';
|
||||
import Login from '../components/auth/login.vue';
|
||||
import Signup from '../components/auth/signup.vue';
|
||||
import Release from '../components/releases/release.vue';
|
||||
import Entity from '../components/entities/entity.vue';
|
||||
import Networks from '../components/networks/networks.vue';
|
||||
@@ -16,6 +18,7 @@ import NotFound from '../components/errors/404.vue';
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
redirect: {
|
||||
name: 'updates',
|
||||
params: {
|
||||
@@ -25,6 +28,16 @@ const routes = [
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: Login,
|
||||
},
|
||||
{
|
||||
path: '/signup',
|
||||
name: 'singup',
|
||||
component: Signup,
|
||||
},
|
||||
{
|
||||
path: '/updates',
|
||||
redirect: {
|
||||
|
||||
Reference in New Issue
Block a user