From 4e143a55a4a3176ce8f5954217cc18a525e3b6b1 Mon Sep 17 00:00:00 2001 From: Kayaya Date: Thu, 6 Aug 2020 14:31:07 +0100 Subject: [PATCH] made some changes --- src/app/config/auth-constants.ts | 5 ++ .../explore-container.component.html | 4 -- .../explore-container.component.scss | 27 ---------- .../explore-container.component.spec.ts | 24 --------- .../explore-container.component.ts | 15 ------ .../explore-container.module.ts | 14 ----- src/app/pages/login/login.page.html | 8 +-- src/app/pages/login/login.page.ts | 54 +++++++++++++++++-- src/app/services/auth.service.spec.ts | 16 ++++++ src/app/services/auth.service.ts | 33 ++++++++++++ src/app/services/http.service.ts | 22 ++++---- src/app/services/storage.service.ts | 35 ++++++++---- 12 files changed, 142 insertions(+), 115 deletions(-) create mode 100644 src/app/config/auth-constants.ts delete mode 100644 src/app/explore-container/explore-container.component.html delete mode 100644 src/app/explore-container/explore-container.component.scss delete mode 100644 src/app/explore-container/explore-container.component.spec.ts delete mode 100644 src/app/explore-container/explore-container.component.ts delete mode 100644 src/app/explore-container/explore-container.module.ts create mode 100644 src/app/services/auth.service.spec.ts create mode 100644 src/app/services/auth.service.ts diff --git a/src/app/config/auth-constants.ts b/src/app/config/auth-constants.ts new file mode 100644 index 000000000..bb1fef4ae --- /dev/null +++ b/src/app/config/auth-constants.ts @@ -0,0 +1,5 @@ +/* This file stores all the keys */ +export class AuthConnstants{ + /* My reference key */ + public static readonly AUTH = 'userDataKey' +} \ No newline at end of file diff --git a/src/app/explore-container/explore-container.component.html b/src/app/explore-container/explore-container.component.html deleted file mode 100644 index bbadb7d64..000000000 --- a/src/app/explore-container/explore-container.component.html +++ /dev/null @@ -1,4 +0,0 @@ -
- {{ name }} -

Explore UI Components

-
\ No newline at end of file diff --git a/src/app/explore-container/explore-container.component.scss b/src/app/explore-container/explore-container.component.scss deleted file mode 100644 index 8993e7ca0..000000000 --- a/src/app/explore-container/explore-container.component.scss +++ /dev/null @@ -1,27 +0,0 @@ -#container { - text-align: center; - - position: absolute; - left: 0; - right: 0; - top: 50%; - transform: translateY(-50%); -} - -#container strong { - font-size: 20px; - line-height: 26px; -} - -#container p { - font-size: 16px; - line-height: 22px; - - color: #8c8c8c; - - margin: 0; -} - -#container a { - text-decoration: none; -} \ No newline at end of file diff --git a/src/app/explore-container/explore-container.component.spec.ts b/src/app/explore-container/explore-container.component.spec.ts deleted file mode 100644 index 9f56ba62a..000000000 --- a/src/app/explore-container/explore-container.component.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { IonicModule } from '@ionic/angular'; - -import { ExploreContainerComponent } from './explore-container.component'; - -describe('ExploreContainerComponent', () => { - let component: ExploreContainerComponent; - let fixture: ComponentFixture; - - beforeEach(async(() => { - TestBed.configureTestingModule({ - declarations: [ ExploreContainerComponent ], - imports: [IonicModule.forRoot()] - }).compileComponents(); - - fixture = TestBed.createComponent(ExploreContainerComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - })); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); diff --git a/src/app/explore-container/explore-container.component.ts b/src/app/explore-container/explore-container.component.ts deleted file mode 100644 index 60554cbd0..000000000 --- a/src/app/explore-container/explore-container.component.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Component, OnInit, Input } from '@angular/core'; - -@Component({ - selector: 'app-explore-container', - templateUrl: './explore-container.component.html', - styleUrls: ['./explore-container.component.scss'], -}) -export class ExploreContainerComponent implements OnInit { - @Input() name: string; - - constructor() { } - - ngOnInit() {} - -} diff --git a/src/app/explore-container/explore-container.module.ts b/src/app/explore-container/explore-container.module.ts deleted file mode 100644 index 43762961f..000000000 --- a/src/app/explore-container/explore-container.module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; -import { FormsModule } from '@angular/forms'; - -import { IonicModule } from '@ionic/angular'; - -import { ExploreContainerComponent } from './explore-container.component'; - -@NgModule({ - imports: [ CommonModule, FormsModule, IonicModule], - declarations: [ExploreContainerComponent], - exports: [ExploreContainerComponent] -}) -export class ExploreContainerComponentModule {} diff --git a/src/app/pages/login/login.page.html b/src/app/pages/login/login.page.html index 844c3f284..27b4ecb68 100644 --- a/src/app/pages/login/login.page.html +++ b/src/app/pages/login/login.page.html @@ -6,18 +6,18 @@ Username - + Password - + Domínio - + - Entrar + Entrar Criar conta diff --git a/src/app/pages/login/login.page.ts b/src/app/pages/login/login.page.ts index e57de8207..3e8be0e13 100644 --- a/src/app/pages/login/login.page.ts +++ b/src/app/pages/login/login.page.ts @@ -1,5 +1,8 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; +import { AuthService } from 'src/app/services/auth.service'; +import { StorageService } from 'src/app/services/storage.service'; +import { AuthConnstants } from 'src/app/config/auth-constants'; @Component({ selector: 'app-login', @@ -8,14 +11,59 @@ import { Router } from '@angular/router'; }) export class LoginPage implements OnInit { - constructor(private router: Router) { } + /* Declare variables */ + public postData = { + username: '', + password: '', + domainName: '' + } + + constructor(private router: Router, private authService: AuthService, private storageService: StorageService) { } ngOnInit() { } + /* Function to validade the login inputs */ + validateInput(){ + let username = this.postData.username.trim(); + let password = this.postData.password.trim(); + let domainName = this.postData.domainName.trim(); - login(){ + return ( + this.postData.username && + this.postData.password && + username.length > 0 + && password.length > 0 + ); + } + + loginAction(){ //Go to our home in home/feed. - this.router.navigate(['/home/feed']); + /* this.router.navigate(['/home/feed']); */ + /* console.log(this.postData); */ + if(this.validateInput()){ + //Try to login + this.authService.login(this.postData).subscribe((res: any)=> { + /* userData must be in the API results as paratemers otherwise change to the param that is in the API */ + if(res.userData){ + /* Then we store this information at the staorage service */ + this.storageService.store(AuthConnstants.AUTH, res.userData); + /* Then go to home view */ + this.router.navigate(['home']); + } + else{ + console.log("Os dados inseridos são inválidos"); + } + }, + /* Write a connection error */ + (error: any)=>{ + console.log("Erro de conexão com a API."); + } + ); + } + else{ + //Display an error message + console.log("Preencha todos campos"); + } } } diff --git a/src/app/services/auth.service.spec.ts b/src/app/services/auth.service.spec.ts new file mode 100644 index 000000000..f1251cacf --- /dev/null +++ b/src/app/services/auth.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthService } from './auth.service'; + +describe('AuthService', () => { + let service: AuthService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AuthService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts new file mode 100644 index 000000000..4c7a03a48 --- /dev/null +++ b/src/app/services/auth.service.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@angular/core'; +import { StorageService } from './storage.service'; +import { HttpService } from './http.service'; +import { Router } from '@angular/router'; +import { Observable } from 'rxjs'; +import { AuthConnstants } from '../config/auth-constants'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthService { + + constructor( + private httpService: HttpService, + private storageService: StorageService, + private router: Router + ) { } + + login(postData: any): Observable { + return this.httpService.post('login', postData); + } + + signup(postData: any): Observable { + return this.httpService.post('signup', postData); + } + + logout(){ + this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{ + this.router.navigate(['']) + }) + + } +} diff --git a/src/app/services/http.service.ts b/src/app/services/http.service.ts index 9264d87aa..c3f9cd3a7 100644 --- a/src/app/services/http.service.ts +++ b/src/app/services/http.service.ts @@ -1,22 +1,18 @@ import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { environment } from 'src/environments/environment'; - -/* import { environment } from 'src/environments/environment.prod'; */ +import { environment } from '../../environments/environment'; @Injectable({ - providedIn: 'root' +providedIn: 'root' }) export class HttpService { +constructor(private http: HttpClient) {} - constructor(private http: HttpClient) { } +post(serviceName: string, data: any) { +const headers = new HttpHeaders(); +const options = { headers: headers, withCredintials: false }; +const url = environment.apiURL + serviceName; - post(serviceName: string, data: any){ - const headers = new HttpHeaders(); - const options = { header: headers, withCredintials: false} - - const url = environment.apiURL + serviceName; - - /* const this.http.post(url, JSON.stringify(data), options); */ - } +return this.http.post(url, JSON.stringify(data), options); } +} \ No newline at end of file diff --git a/src/app/services/storage.service.ts b/src/app/services/storage.service.ts index 5cbab29dd..d47dacad2 100644 --- a/src/app/services/storage.service.ts +++ b/src/app/services/storage.service.ts @@ -1,20 +1,33 @@ import { Injectable } from '@angular/core'; import { Plugins } from '@capacitor/core'; - const { Storage } = Plugins; - @Injectable({ providedIn: 'root' -}) -export class StorageService { + }) + export class StorageService { + constructor() {} - constructor() { - /* async store(storageKey: String, value: any){ - await Storage.set({ - key: storageKey, - value: value - }) + // Store the value + async store(storageKey: string, value: any) { + const encryptedValue = btoa(escape(JSON.stringify(value))); + await Storage.set({ + key: storageKey, + value: encryptedValue + }); + } - } */ + // Get the value + async get(storageKey: string) { + const ret = await Storage.get({ key: storageKey }); + return JSON.parse(unescape(atob(ret.value))); + } + + async removeStorageItem(storageKey: string) { + await Storage.remove({ key: storageKey }); + } + + // Clear storage + async clear() { + await Storage.clear(); } }