diff --git a/src/api.ts b/src/api.ts index 9d349fd..a90f107 100644 --- a/src/api.ts +++ b/src/api.ts @@ -32,7 +32,7 @@ class Model { } } - protected async create(endpoint: string, data: any) { + static async create(endpoint: string, data: any) { const init = { headers: new Headers(), method: "post", @@ -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,18 +60,27 @@ interface UserData { } export class User extends Model implements UserData { + public isAuthenticated = false; + private token = ""; + constructor(public email: string, public password: string) { super(); } + static async confirm(uid: string, token: string): Promise { + await super.create("users/activation", { uid, token }); + } + async login(): Promise { - await super.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 { - await super.create("users", { email: this.email, password: this.password }); + await Model.create("users", { email: this.email, password: this.password }); } } 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 @@