WIP User service, login routing.

This commit is contained in:
Jonathan Bernard 2019-03-02 02:07:56 -06:00
parent a6e8da95f4
commit bcde5fbfc0
3 changed files with 69 additions and 13 deletions

View File

@ -1,13 +1,17 @@
import Vue from 'vue'; import Vue from 'vue';
import Router from 'vue-router'; import { default as Router, Route } from 'vue-router';
import Dashboard from './views/Dashboard.vue';
import Measures from './views/Measures.vue'; import Dashboard from '@/views/Dashboard.vue';
import UserAccount from './views/UserAccount.vue'; import Login from '@/views/Login.vue';
import QuickPanels from './views/QuickPanels.vue'; import Measures from '@/views/Measures.vue';
import UserAccount from '@/views/UserAccount.vue';
import QuickPanels from '@/views/QuickPanels.vue';
import userService from '@/services/user';
Vue.use(Router); Vue.use(Router);
export default new Router({ const router = new Router({
mode: 'history', mode: 'history',
base: process.env.BASE_URL, base: process.env.BASE_URL,
routes: [ routes: [
@ -15,25 +19,47 @@ export default new Router({
path: '/', path: '/',
redirect: '/dashboard' redirect: '/dashboard'
}, },
{
path: '/login',
name: 'login',
component: Login
},
{ {
path: '/dashboard', path: '/dashboard',
name: 'dashboard', name: 'dashboard',
component: Dashboard component: Dashboard,
meta: { requiresAuth: true }
}, },
{ {
path: '/measures', path: '/measures',
name: 'measures', name: 'measures',
component: Measures component: Measures,
meta: { requiresAuth: true }
}, },
{ {
path: '/user-account', path: '/user-account',
name: 'user-account', name: 'user-account',
component: UserAccount component: UserAccount,
meta: { requiresAuth: true }
}, },
{ {
path: '/quick-panels', path: '/quick-panels',
name: 'quick-panels', name: 'quick-panels',
component: QuickPanels component: QuickPanels,
meta: { requiresAuth: true }
} }
] ]
}); });
// Auth filter
router.beforeEach((to, from, next) => {
if (to.matched.some((record) => record.meta.requiresAuth)) {
if (!userService.isAuthed()) {
next({
path: '/login',
params: { nextUrl: to.fullPath } });
} else { next(); } // if authed
} else { next(); } // if auth required
});
export default router;

View File

@ -33,7 +33,7 @@ export class PmApiClient {
public getUser(authToken: string): Promise<User> { public getUser(authToken: string): Promise<User> {
return this.http return this.http
.get('/user', { headers: { Authorization: 'Bearer ' + authToken }}) .get('/user', { headers: { Authorization: 'Bearer ' + authToken }})
.then((resp) => return merge(resp.data, { authToken }); ); .then((resp) => merge(resp.data, { authToken }) );
} }
public getAllUsers(user: User): Promise<User[]> { public getAllUsers(user: User): Promise<User[]> {
@ -168,8 +168,9 @@ export class PmApiClient {
.then((resp) => true); .then((resp) => true);
} }
private authHeader(user: User): { [key: string]: string } { private authHeader(user: User): { [key: string]: string } {
return { Authorization: 'Bearer ' + user.authToken }; return { Authorization: 'Bearer ' + user.authToken };
}
} }
export default new PmApiClient(process.env.VUE_APP_PM_API_BASE);

29
web/src/services/user.ts Normal file
View File

@ -0,0 +1,29 @@
import User from '@/models/user.ts';
import { default as apiClient, PmApiClient } from '@/services/pm-api-client.ts';
export class UserService {
private user?: User = undefined;
constructor(private api: PmApiClient) { }
public isAuthed(): boolean {
return this.user != null && this.user.authToken != null;
}
public getUser(): User | undefined { return this.user; }
public authUser(email: string, password: string): Promise<User> {
return this.api.createAuthToken(email, password)
.then((token) => this.api.getUser(token));
}
public getUserById(reqUserId: string): Promise<User> {
if (this.user == null) {
return Promise.reject(new Error('no currently authenticated user'));
} else {
return this.api.getUserById(this.user, reqUserId);
}
}
}
export default new UserService(apiClient);