From 337625379240706c05bdf337bea915dbd3b3b70b Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 15 Apr 2021 10:05:54 +0200 Subject: [PATCH] Add login view --- src/api.ts | 13 ++++++++++--- src/components/LoginForm.vue | 18 ++++++++++++------ src/router/index.ts | 1 - src/views/Home.vue | 3 ++- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/api.ts b/src/api.ts index 51c6a17..a90f107 100644 --- a/src/api.ts +++ b/src/api.ts @@ -45,8 +45,10 @@ class Model { init.headers.set("Accept", "application/json"); init.headers.set("Content-Type", "application/json"); const response = await fetch(`/api/${endpoint}/`, init); - const dataOrErrors: Array = await response.json(); - if (response.status !== 201) { + const dataOrErrors = await response.json(); + if (response.status === 200) { + return dataOrErrors; + } else { throw new APIError(response.statusText, dataOrErrors); } } @@ -58,6 +60,9 @@ interface UserData { } export class User extends Model implements UserData { + public isAuthenticated = false; + private token = ""; + constructor(public email: string, public password: string) { super(); } @@ -67,10 +72,12 @@ export class User extends Model implements UserData { } async login(): Promise { - await Model.create("token/login", { + const response = await Model.create("token/login", { email: this.email, password: this.password, }); + this.token = response.auth_token; + this.isAuthenticated = true; } async signup(): Promise { diff --git a/src/components/LoginForm.vue b/src/components/LoginForm.vue index 85e8a33..11844f0 100644 --- a/src/components/LoginForm.vue +++ b/src/components/LoginForm.vue @@ -1,5 +1,5 @@