Created
January 3, 2018 22:22
-
-
Save jessemlay/4b15fb31416f1671a236895b7327c2b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { InAppBrowser, InAppBrowserObject } from '@ionic-native/in-app-browser'; | |
import { UserServiceProvider } from './../../providers/user-service/user-service'; | |
import { NgForm } from '@angular/forms'; | |
import { Component } from '@angular/core'; | |
import { IonicPage, Events, Platform, AlertController } from 'ionic-angular'; | |
import { AndroidFingerprintAuth, AFAAuthOptions } from '@ionic-native/android-fingerprint-auth'; | |
import { TouchID } from '@ionic-native/touch-id'; | |
import { Keychain } from '@ionic-native/keychain'; | |
import { ToastController } from 'ionic-angular/components/toast/toast-controller'; | |
import { LoginInfo } from '../../interfaces/login-Info'; | |
@IonicPage() | |
@Component({ | |
selector: 'page-login', | |
templateUrl: 'login.html', | |
providers: [UserServiceProvider] | |
}) | |
export class LoginPage { | |
login: LoginInfo = { username: '', password: '' }; | |
submitted = false; | |
browser: InAppBrowserObject; | |
loggedIn: boolean = false; | |
constructor(public events: Events, public platform: Platform, public userService: UserServiceProvider, private androidFingerprintAuth: AndroidFingerprintAuth, private iab: InAppBrowser, | |
private alertCtrl: AlertController, private toastCtrl: ToastController, private iosFingerprintAuth: TouchID, private iosKeychain: Keychain) { | |
this.platform.ready().then(() => { | |
// subscribe to events | |
this.listenToEvents(); | |
//check if logged in | |
this.userService.hasLoggedIn() | |
.then(async (hasLoggedIn) => { | |
if (hasLoggedIn) { | |
this.loggedIn = true; | |
// fingerprint available check if a matching token exists | |
let token = await this.userService.getUserToken(await this.userService.getLoggedInUsername()); | |
this.androidFingerprintAuth.isAvailable().then(async (result) => { | |
if (result.isAvailable) { | |
if (result.hasEnrolledFingerprints && token) { | |
var decryptConfig: AFAAuthOptions = { | |
clientId: token.clientId, | |
username: token.username, | |
token: token.token | |
} | |
this.androidFingerprintAuth.decrypt(decryptConfig).then(result => { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${token.username}&p=${result.password}`, "_self", "location=no,hardwareback=yes") | |
}).catch(error => { | |
if (error === 'FINGERPRINT_CANCELLED') { | |
} else { | |
//this.androidFingerprintAuth.delete({ clientId: token.clientId, username: token.username }) | |
this.onLogout(); | |
} | |
}) | |
} | |
} else { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${token.username}&p=${token.password}`, "_self", "location=no,hardwareback=yes") | |
} | |
}) | |
} | |
}) | |
//subscribe to resume event | |
this.platform.resume.subscribe(() => { | |
//hide the browser | |
//if logged and fingerprint setup show it | |
this.userService.hasLoggedIn() | |
.then(async (hasLoggedIn) => { | |
if (hasLoggedIn) { | |
// fingerprint available check if a matching token exists | |
let token = await this.userService.getUserToken(await this.userService.getLoggedInUsername()); | |
if (this.platform.is('android')) { | |
this.androidFingerprintAuth.isAvailable().then(async (result) => { | |
if (result.isAvailable) { | |
if (result.hasEnrolledFingerprints && token) { | |
if (this.browser) { | |
this.browser.hide() | |
} | |
var decryptConfig: AFAAuthOptions = { | |
clientId: token.clientId, | |
username: token.username, | |
token: token.token | |
} | |
this.androidFingerprintAuth.decrypt(decryptConfig).then(result => { | |
if (this.browser) { | |
this.browser.show(); | |
} else { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${token.username}&p=${result.password}`, "_self", "location=no,hardwareback=yes") | |
} | |
}).catch(error => { | |
if (error === 'FINGERPRINT_CANCELLED') { | |
} else { | |
this.androidFingerprintAuth.delete({ clientId: token.clientId, username: token.username }) | |
this.onLogout(); | |
} | |
}) | |
} | |
} else { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${token.username}&p=${token.password}`, "_self", "location=no,hardwareback=yes") | |
} | |
}) | |
} else if (this.platform.is('ios')) { | |
this.iosFingerprintAuth.isAvailable().then(async (reason) => { | |
// fingerprint available check if a matching token exists | |
let token = await this.userService.getUserToken(await this.userService.getLoggedInUsername()); | |
this.iosFingerprintAuth.verifyFingerprint('Confirm fingerprint to continue') | |
.then(reason => { | |
if (this.browser) { | |
this.browser.show(); | |
} else { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${token.username}&p=${token.password}`, "_self", "location=no,hardwareback=yes") | |
} | |
}), | |
error => { | |
this.onLogout(); | |
} | |
}), | |
error => { | |
if (this.browser) { | |
this.browser.show(); | |
} else { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${token.username}&p=${token.password}`, "_self", "location=no,hardwareback=yes") | |
} | |
} | |
} | |
} | |
}) | |
}); | |
}); | |
} | |
async onLogin(form: NgForm) { | |
this.submitted = true; | |
if (form.valid) { | |
if (await this.userService.login(this.login)) { | |
this.loggedIn = true; | |
// running on android | |
if (this.platform.is('android')) { | |
this.androidFingerprintAuth.isAvailable().then(async result => { | |
if (result.isAvailable) { | |
// fingerprint available check if a matching token exists | |
let token = await this.userService.getUserToken(await this.userService.getLoggedInUsername()); | |
if (result.hasEnrolledFingerprints && token) { | |
var decryptConfig: AFAAuthOptions = { | |
clientId: token.clientId, | |
username: token.username, | |
token: token.token | |
} | |
return this.androidFingerprintAuth.decrypt(decryptConfig) | |
.then(async (result) => { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${decryptConfig.username}&p=${result.password}`, "_self", "location=no,hardwareback=yes") | |
}).catch(async (error) => { | |
console.log(error); | |
this.androidFingerprintAuth.delete({ clientId: token.clientId, username: token.username }).then(result => { | |
console.log(`cipher deleted: ${error}`) | |
}) | |
}) | |
} else { | |
let alert = this.alertCtrl.create({ | |
title: "Setup Fingerprint", | |
buttons: [ | |
{ | |
text: 'Ok', | |
handler: () => { | |
this.userService.getAppName().then(appName => { | |
if (this.platform.is('android')) { | |
return this.androidFingerprintAuth.encrypt({ clientId: appName, username: this.login.username, password: this.login.password }).then(result => { | |
this.userService.setUserToken({ username: this.login.username, password: this.login.password, token: result.token }) | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${this.login.username}&p=${this.login.password}`, "_self", "location=no,hardwareback=yes") | |
}); | |
} | |
}); | |
} | |
}, | |
{ | |
text: 'Cancel', | |
role: 'cancel' | |
} | |
] | |
}); | |
// present alert | |
alert.present(); | |
} | |
} else { | |
this.userService.setUserToken({ username: this.login.username, password: this.login.password, token: '' }) | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${this.login.username}&p=${this.login.password}`, "_self", "location=no,hardwareback=yes") | |
} | |
}) | |
} else if (this.platform.is('ios')) { | |
// fingerprint available check if a matching token exists | |
let token = await this.userService.getUserToken(await this.userService.getLoggedInUsername()); | |
this.iosFingerprintAuth.isAvailable() | |
.then(reason => { | |
console.log(reason); | |
this.iosKeychain.get(token.clientId) | |
.then(value => { | |
if (value) { | |
this.iosFingerprintAuth.verifyFingerprint('Confirm fingerprint to continue') | |
.then(result => { | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${token.username}&p=${token.password}`, "_self", "location=no,hardwareback=yes") | |
}) | |
} else { | |
let alert = this.alertCtrl.create({ | |
title: "Setup Fingerprint", | |
buttons: [ | |
{ | |
text: 'Ok', | |
handler: () => { | |
let key = btoa(`${token.username}:${token.password}`); | |
this.userService.getAppName().then(appName => { | |
return this.iosKeychain.set(key, token.password).then(result => { | |
this.userService.setUserToken({ username: this.login.username, password: this.login.password, token: key }) | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${this.login.username}&p=${this.login.password}`, "_self", "location=no,hardwareback=yes") | |
}); | |
}); | |
} | |
}, | |
{ | |
text: 'Cancel', | |
role: 'cancel' | |
} | |
] | |
}); | |
// present alert | |
alert.present(); | |
} | |
}) | |
}), | |
error => { | |
this.userService.setUserToken({ username: this.login.username, password: this.login.password, token: '' }) | |
this.browser = this.iab.create(`http://rta.rastrac.net/rtds?package=mobile&username=${this.login.username}&p=${this.login.password}`, "_self", "location=no,hardwareback=yes") | |
} | |
} | |
} else { | |
let invalidLoginToast = this.toastCtrl.create({ | |
message: 'Invalid Username or Password', | |
duration: 3000, | |
position: 'bottom', | |
cssClass: 'bottomToast' | |
}); | |
invalidLoginToast.present(); | |
} | |
//form.reset(); | |
} | |
} | |
async onLogout() { | |
console.log('logout') | |
this.loggedIn = false; | |
this.submitted = false; | |
this.userService.logout(); | |
if (this.platform.is('android')) { | |
this.androidFingerprintAuth.delete({ clientId: await this.userService.getAppName(), username: await this.userService.getLoggedInUsername() }) | |
} else if (this.platform.is('ios')) { | |
let key = await this.userService.getUserToken(await this.userService.getLoggedInUsername()); | |
this.iosKeychain.remove(key.token) | |
} | |
} | |
listenToEvents() { | |
this.events.subscribe('user:login', () => { | |
// disable login features | |
}); | |
this.events.subscribe('user:logout', () => { | |
// disable logout features | |
}) | |
} | |
ionViewDidLoad() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment