Skip to content

Commit

Permalink
Step 17.18: Create getFbProfile Meteor method
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Jun 13, 2017
1 parent 6ad97fe commit e90aae0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/server/methods.ts
Expand Up @@ -4,6 +4,7 @@ import { MessageType, Profile } from './models';
import { check, Match } from 'meteor/check';
import { Users } from "./collections/users";
import { fcmService } from "./services/fcm";
import { facebookService, FbProfile } from "./services/facebook";

const nonEmptyString = Match.Where((str) => {
check(str, String);
Expand Down Expand Up @@ -118,5 +119,17 @@ Meteor.methods({
check(token, nonEmptyString);

Users.collection.update({_id: this.userId}, {$set: {"fcmToken": token}});
},
async getFbProfile(): Promise<FbProfile> {
if (!this.userId) throw new Meteor.Error('unauthorized', 'User must be logged-in to call this method');

if (!Users.collection.findOne({'_id': this.userId}).services.facebook) {
throw new Meteor.Error('unauthorized', 'User must be logged-in with Facebook to call this method');
}

//TODO: handle error: token may be expired
const accessToken = await facebookService.getAccessToken(this.userId);
//TODO: handle error: user may have denied permissions
return await facebookService.getProfile(accessToken);
}
});

0 comments on commit e90aae0

Please sign in to comment.