WIP User service, login routing.

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

View File

@ -33,7 +33,7 @@ export class PmApiClient {
public getUser(authToken: string): Promise<User> {
return this.http
.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[]> {
@ -168,8 +168,9 @@ export class PmApiClient {
.then((resp) => true);
}
private authHeader(user: User): { [key: string]: string } {
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);