Added logout and localStorage

This commit is contained in:
piratefinn 2017-06-05 18:47:34 +01:00
parent d8ff4064e2
commit 1bd586b3ae
5 changed files with 59 additions and 13 deletions

View file

@ -38,7 +38,7 @@
<a class="dropdown-item" href="#"><i class="fa fa-file"></i> Notifications<span class="badge badge-primary">42</span></a> <a class="dropdown-item" href="#"><i class="fa fa-file"></i> Notifications<span class="badge badge-primary">42</span></a>
<div class="divider"></div> <div class="divider"></div>
<a class="dropdown-item" href="#"><i class="fa fa-shield"></i> Lock account</a> <a class="dropdown-item" href="#"><i class="fa fa-shield"></i> Lock account</a>
<a class="dropdown-item" href="#"><i class="fa fa-lock"></i> Logout</a> <a class="dropdown-item" (click)="userlogout()"><i class="fa fa-lock"></i> Logout</a>
</div> </div>
</li> </li>
<li class="nav-item d-md-down-none"> <li class="nav-item d-md-down-none">

View file

@ -1,11 +1,17 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ApiService } from '../providers/api-service';
import {Router } from '@angular/router';
@Component({ @Component({
selector: 'app-dashboard', selector: 'app-dashboard',
templateUrl: './full-layout.component.html' templateUrl: './full-layout.component.html',
providers: [ApiService]
}) })
export class FullLayoutComponent implements OnInit { export class FullLayoutComponent implements OnInit {
constructor(
private api: ApiService,
private router: Router,
) {}
public disabled = false; public disabled = false;
public status: {isopen: boolean} = {isopen: false}; public status: {isopen: boolean} = {isopen: false};
@ -20,4 +26,14 @@ export class FullLayoutComponent implements OnInit {
} }
ngOnInit(): void {} ngOnInit(): void {}
userlogout() {
console.log('logout clicked');
this.api
.logout()
.subscribe(
result => {
console.log('Logged out!');
}
);
}
} }

View file

@ -32,7 +32,7 @@
<div> <div>
<h2>Sign up</h2> <h2>Sign up</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<button type="button" class="btn btn-primary active mt-3">Register Now!</button> <button type="button" class="btn btn-primary active mt-3" routerLinkActive="active" [routerLink]="['/pages/register']">Register Now!</button>
</div> </div>
</div> </div>
</div> </div>

View file

@ -72,7 +72,7 @@ export class RegisterComponent {
age_range: customerForm.age_range, age_range: customerForm.age_range,
}; };
console.log(data); console.log(data);
/* this.api this.api
.register(data) .register(data)
.subscribe( .subscribe(
result => { result => {
@ -82,7 +82,7 @@ export class RegisterComponent {
error => { error => {
console.log( error._body ); console.log( error._body );
} }
);*/ );
} }
onSubmitOrganisation() { onSubmitOrganisation() {
@ -105,7 +105,7 @@ export class RegisterComponent {
postcode: organisationForm.postcode, postcode: organisationForm.postcode,
}; };
console.log(data); console.log(data);
/* this.api this.api
.register(data) .register(data)
.subscribe( .subscribe(
result => { result => {
@ -115,7 +115,7 @@ export class RegisterComponent {
error => { error => {
console.log( error._body ); console.log( error._body );
} }
);*/ );
} }

View file

@ -3,16 +3,36 @@ import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
/* this provider handles the interaction between server and client */ /* this provider handles the interaction between server and client */
@Injectable() @Injectable()
export class ApiService { export class ApiService {
private apiUrl = 'https://dev.app.peartrade.org/api'; private apiUrl = 'https://dev.app.peartrade.org/api';
private sessionKey: string; private sessionKey: string = null;
constructor( constructor(
private http: Http, private http: Http,
) {} ) {
if (localStorage.getItem('sessionKey') ) {
this.sessionKey = localStorage.getItem('sessionKey');
}
}
private getSessionKey() {
console.log('get key');
return this.sessionKey;
}
private setSessionKey(key) {
console.log('set key');
this.sessionKey = key;
localStorage.setItem('sessionKey', this.sessionKey);
}
private removeSessionKey() {
console.log('remove key');
this.sessionKey = null;
localStorage.removeItem('sessionKey');
}
public getAgeRanges() { public getAgeRanges() {
return this.http.get( return this.http.get(
@ -33,11 +53,21 @@ export class ApiService {
data data
).map( response => response.json() ); ).map( response => response.json() );
login_event.subscribe( login_event.subscribe(
result => { this.sessionKey = result.session_key } result => { this.setSessionKey(result.session_key) }
); );
return login_event; return login_event;
} }
public logout() {
console.log(this.sessionKey);
return this.http.post(
this.apiUrl + '/logout',
{
session_key : this.sessionKey,
}
).map( response => { this.removeSessionKey(); return response.json() } );
}
public search(data) { public search(data) {
data.session_key = this.sessionKey; data.session_key = this.sessionKey;
return this.http.post( return this.http.post(