Permalink
Browse files

Step 14.11: Add getSMS method to phone.ts

  • Loading branch information...
darkbasic committed Jun 3, 2017
1 parent 7e8c061 commit 405ae503e8cf2a9274cdf50607dc01a5255b53e6
Showing with 40 additions and 2 deletions.
  1. +1 −0 api/server/models.ts
  2. +39 −2 src/services/phone.ts
View
@@ -1,4 +1,5 @@
export const DEFAULT_PICTURE_URL = '/assets/default-profile-pic.svg';
export const TWILIO_SMS_NUMBERS = ["+12248032362"];
export interface Profile {
name?: string;
View
@@ -3,12 +3,17 @@ import { Accounts } from 'meteor/accounts-base';
import { Meteor } from 'meteor/meteor';
import { Platform } from 'ionic-angular';
import { Sim } from '@ionic-native/sim';
import { SmsReceiver } from "../ionic/sms-receiver";
import * as Bluebird from "bluebird";
import { TWILIO_SMS_NUMBERS } from "api/models";
import { Observable } from "rxjs";
@Injectable()
export class PhoneService {
constructor(private platform: Platform,
private sim: Sim) {
private sim: Sim,
private smsReceiver: SmsReceiver) {
Bluebird.promisifyAll(this.smsReceiver);
}
async getNumber(): Promise<string> {
@@ -27,6 +32,38 @@ export class PhoneService {
return '+' + (await this.sim.getSimInfo()).phoneNumber;
}
async getSMS(): Promise<string> {
if (!this.platform.is('android')) {
throw new Error('Cannot read SMS, platform is not Android.')
}
try {
await (<any>this.smsReceiver).isSupported();
} catch (e) {
throw new Error('User denied SMS access.');
}
const startObs = Observable.fromPromise((<any>this.smsReceiver).startReceiving()).map((msg: string) => msg);
const timeoutObs = Observable.interval(120000).take(1).map(() => {
throw new Error('Receiving SMS timed out.')
});
try {
var msg = await startObs.takeUntil(timeoutObs).toPromise();
} catch (e) {
await (<any>this.smsReceiver).stopReceiving();
throw e;
}
await (<any>this.smsReceiver).stopReceiving();
if (TWILIO_SMS_NUMBERS.includes(msg.split(">")[0])) {
return msg.substr(msg.length - 4);
} else {
throw new Error('Sender is not a Twilio number.')
}
}
verify(phoneNumber: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
Accounts.requestPhoneVerification(phoneNumber, (e: Error) => {

0 comments on commit 405ae50

Please sign in to comment.