Added logout and localStorage
This commit is contained in:
parent
d8ff4064e2
commit
1bd586b3ae
5 changed files with 59 additions and 13 deletions
|
@ -38,7 +38,7 @@
|
|||
<a class="dropdown-item" href="#"><i class="fa fa-file"></i> Notifications<span class="badge badge-primary">42</span></a>
|
||||
<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-lock"></i> Logout</a>
|
||||
<a class="dropdown-item" (click)="userlogout()"><i class="fa fa-lock"></i> Logout</a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="nav-item d-md-down-none">
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
import { ApiService } from '../providers/api-service';
|
||||
import {Router } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-dashboard',
|
||||
templateUrl: './full-layout.component.html'
|
||||
templateUrl: './full-layout.component.html',
|
||||
providers: [ApiService]
|
||||
})
|
||||
export class FullLayoutComponent implements OnInit {
|
||||
|
||||
constructor(
|
||||
private api: ApiService,
|
||||
private router: Router,
|
||||
) {}
|
||||
public disabled = false;
|
||||
public status: {isopen: boolean} = {isopen: false};
|
||||
|
||||
|
@ -20,4 +26,14 @@ export class FullLayoutComponent implements OnInit {
|
|||
}
|
||||
|
||||
ngOnInit(): void {}
|
||||
userlogout() {
|
||||
console.log('logout clicked');
|
||||
this.api
|
||||
.logout()
|
||||
.subscribe(
|
||||
result => {
|
||||
console.log('Logged out!');
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
|
@ -32,7 +32,7 @@
|
|||
<div>
|
||||
<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>
|
||||
<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>
|
||||
|
|
|
@ -72,7 +72,7 @@ export class RegisterComponent {
|
|||
age_range: customerForm.age_range,
|
||||
};
|
||||
console.log(data);
|
||||
/* this.api
|
||||
this.api
|
||||
.register(data)
|
||||
.subscribe(
|
||||
result => {
|
||||
|
@ -82,7 +82,7 @@ export class RegisterComponent {
|
|||
error => {
|
||||
console.log( error._body );
|
||||
}
|
||||
);*/
|
||||
);
|
||||
}
|
||||
onSubmitOrganisation() {
|
||||
|
||||
|
@ -105,7 +105,7 @@ export class RegisterComponent {
|
|||
postcode: organisationForm.postcode,
|
||||
};
|
||||
console.log(data);
|
||||
/* this.api
|
||||
this.api
|
||||
.register(data)
|
||||
.subscribe(
|
||||
result => {
|
||||
|
@ -115,7 +115,7 @@ export class RegisterComponent {
|
|||
error => {
|
||||
console.log( error._body );
|
||||
}
|
||||
);*/
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -3,16 +3,36 @@ import { Http } from '@angular/http';
|
|||
import { Observable } from 'rxjs/Rx';
|
||||
import 'rxjs/add/operator/map';
|
||||
|
||||
|
||||
/* this provider handles the interaction between server and client */
|
||||
|
||||
@Injectable()
|
||||
export class ApiService {
|
||||
private apiUrl = 'https://dev.app.peartrade.org/api';
|
||||
private sessionKey: string;
|
||||
private sessionKey: string = null;
|
||||
constructor(
|
||||
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() {
|
||||
return this.http.get(
|
||||
|
@ -33,11 +53,21 @@ export class ApiService {
|
|||
data
|
||||
).map( response => response.json() );
|
||||
login_event.subscribe(
|
||||
result => { this.sessionKey = result.session_key }
|
||||
result => { this.setSessionKey(result.session_key) }
|
||||
);
|
||||
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) {
|
||||
data.session_key = this.sessionKey;
|
||||
return this.http.post(
|
||||
|
|
Reference in a new issue