Skip to content

Commit

Permalink
feat(login): Display "loggin-in" popup when atempting to log via stor…
Browse files Browse the repository at this point in the history
…ed API key
  • Loading branch information
MathieuNls committed Nov 3, 2016
1 parent f2312eb commit acd26e6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 72 deletions.
72 changes: 1 addition & 71 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { Component, ViewChild } from '@angular/core';
import { Platform, Nav } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { Storage } from '@ionic/storage';

import { LogInPage } from '../pages/login/login';
import { DashboardPage } from '../pages/dashboard/dashboard';

import {
TwAPIService,
User
} from './../share/src/app/';

@Component({
template: `<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>`
Expand All @@ -19,76 +12,13 @@ export class MyApp {
rootPage: any = LogInPage;

constructor(
private platform: Platform,
private twapi: TwAPIService,
private storage: Storage
private platform: Platform
) {

platform.ready().then(() => {

this.initOnResume();

this.fetchUser()
.then(
user => {

this.nav.setRoot(DashboardPage, {
user:user
});
},
error => {
console.log("No valid key set");
}
);


// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
});
}

private initOnResume(){
document.addEventListener('resume', () => {

this.fetchUser().then(
user => {
DashboardPage.userChanged.emit(user)
}
);

TwAPIService.resetTime();
});
}

private fetchUser():Promise<User>{

return this.fetchAPIKey().then(
key => {
return this.twapi.getUser(key).then(
user => user,
err => {
console.log("removing", key);
this.storage.remove('tw-api')
throw err;
}
)
},
err => {
throw err;
}
)
}

private fetchAPIKey():Promise<string>{

return this.storage.get('tw-api').then((key) => {

if(key !== "cordova_not_available" && key != null){
return key;
}else{
throw new Error("awd");
}
});
}
}
75 changes: 75 additions & 0 deletions src/pages/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ export class LogInPage extends LoginComponent{
this.loginAttempt.subscribe(
attempt => this.onLoggingAttempt(attempt)
);

this.initOnResume();

this.fetchUser().then(
user => {
this.userLogged.emit(user);
},
error => {
console.log("No valid key set");
}
);
}

/**
Expand Down Expand Up @@ -146,4 +157,68 @@ export class LogInPage extends LoginComponent{
console.log("setting tw-api to", user.key);
this.storage.set('tw-api', user.key);
}

/**
* Fetches and user given a stored API key
* @return {Promise<User>} [description]
*/
private fetchUser():Promise<User>{

return this.fetchAPIKey().then(
key => {
return this.twapi.getUser(key).then(
user => user,
err => {
this.loginAttempt.emit(false);
console.log("removing", key);
this.storage.remove('tw-api')
throw err;
}
)
},
err => {
throw err;
}
)
}

/**
* Add resume event to html body and fetches up to date
* user from the API
*/
private initOnResume(){
document.addEventListener('resume', () => {

this.fetchUser().then(
user => {
DashboardPage.userChanged.emit(user)
},
error => {
console.log("API Key changed");
this.nav.setRoot(LogInPage);
}
);

TwAPIService.resetTime();
});
}

/**
* Fetches a stored api key
*
* @throws on no key found
* @return {Promise<string>} [description]
*/
private fetchAPIKey():Promise<string>{

return this.storage.get('tw-api').then((key) => {

if(key !== "cordova_not_available" && key != null){
this.loginAttempt.emit(true);
return key;
}else{
throw new Error("No valid key");
}
});
}
}
2 changes: 1 addition & 1 deletion src/share
Submodule share updated from 076b96 to 09bfb3

0 comments on commit acd26e6

Please sign in to comment.