From 36173c61176077f8c809a88db2466440875cc02e Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Wed, 26 May 2021 20:51:19 +0200 Subject: [PATCH] BETTERZON-111: Adding service functions for login and registration (#64) --- Frontend/src/app/services/api.service.ts | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index 640b317..c38581a 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -207,4 +207,48 @@ export class ApiService { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } } + + + /* __ __ + / / / /_______ __________ + / / / / ___/ _ \/ ___/ ___/ + / /_/ (__ ) __/ / (__ ) + \____/____/\___/_/ /____/ + */ + + /** + * Registers a new user with the API + * @param username The username for the new user + * @param password The password for the new user + * @param email The email address for the new user + * @return Observable The observable response of the api + */ + registerUser(username: string, password: string, email: string): Observable { + try { + return this.http.post((this.apiUrl + '/users/register'), JSON.stringify({ + username, + password, + email + })); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + /** + * Logs a user in with the api + * @param username The username of the user to log in + * @param password The password of the user to log in + * @return Observable The observable response of the api + */ + loginUser(username: string, password: string): Observable { + try { + return this.http.post((this.apiUrl + '/users/login'), JSON.stringify({ + username, + password + })); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } }