From 8686b083ac0af3e0b3895c04fe847544a8aa576e Mon Sep 17 00:00:00 2001 From: Evan Langlais Date: Wed, 27 Feb 2019 16:48:46 -0500 Subject: [PATCH] Refactoring and fixing the login sequence and calls --- .../authentication.component.html | 8 +-- .../authentication.component.ts | 49 +++++++++---------- src/app/services/login.service.ts | 9 ++-- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/app/authentication/authentication.component.html b/src/app/authentication/authentication.component.html index dbb4279..92a4a26 100644 --- a/src/app/authentication/authentication.component.html +++ b/src/app/authentication/authentication.component.html @@ -6,17 +6,17 @@
-
- +
- + Submit


@@ -27,4 +27,4 @@

- \ No newline at end of file + diff --git a/src/app/authentication/authentication.component.ts b/src/app/authentication/authentication.component.ts index d311947..4f4486d 100644 --- a/src/app/authentication/authentication.component.ts +++ b/src/app/authentication/authentication.component.ts @@ -1,9 +1,7 @@ -import { Component, OnInit } from '@angular/core'; +import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { LoginService } from '../services/login.service' -import 'rxjs/Rx'; -import { Observable } from 'rxjs/Rx'; +import { LoginService } from '../services/login.service'; @Injectable() @@ -13,37 +11,34 @@ import { Observable } from 'rxjs/Rx'; styleUrls: ['./authentication.component.css'] }) export class AuthenticationComponent implements OnInit { - private items: Array; + // private items: Array; + @ViewChild('email_input') email_input: ElementRef; + @ViewChild('password_input') password_input: ElementRef; - constructor(private data: LoginService) { - this.items = ["username", "password"] + constructor(private data: LoginService) { } - logOn (event: any) { - event.preventDefault(); + logOn () { - let username: HTMLElement = document.getElementById('email'); - let password: HTMLElement = document.getElementById('password'); - username.click(); - password.click(); + const loginjson = { + 'user_email': this.email_input.nativeElement.value, + 'user_password': this.password_input.nativeElement.value + }; - var json = JSON.stringify({ - "user_email": username, - "user_password": password + this.data.authorizeUser(loginjson).subscribe((data) => { + const token = data['token']; + // Authorized + console.log(token); + localStorage.setItem('auth_token', token); }); - - var authInfo = this.data.authorizeUser(json); - - var authorized = authInfo["message"]; - var token = authInfo["token"]; - if (authorized) { - return token; - } - - this.data.getUser(token); - } + /* + this.data.getUser(token).subscribe((me) => { + console.log(me); + }); + */ + ngOnInit() { } } diff --git a/src/app/services/login.service.ts b/src/app/services/login.service.ts index 854afbc..b61af7e 100644 --- a/src/app/services/login.service.ts +++ b/src/app/services/login.service.ts @@ -10,11 +10,14 @@ export class LoginService { constructor(private http: HttpClient) { } authorizeUser(loginInfo) { - return this.http.post('http://sd5-backend.engr.uconn.edu/auth', loginInfo) + return this.http.post('http://sd5-backend.engr.uconn.edu/auth', loginInfo); } getUser(token) { - return this.http.get('http://sd5-backend.engr.uconn.edu/user/me', token) + return this.http.get('http://sd5-backend.engr.uconn.edu/user/me', { + headers: { + Authorization: 'Bearer ' + token + } + }); } - }