2016-12-30 15 views
0

私のアプリケーションはMeteorから完全に分離されたReactで構築されています。バックエンドとしてのみ機能するMeteorとのインターフェースには小惑星を使用します。フロントエンドでFacebookのログインボタンを手動で作成し、FacebookからフェッチしたデータをAccounts.createUserに渡したいと思います。私は以下のようにメソッドを作成しているAccounts.createUserユーザー名、パスワード、電子メールなし

const data = { 
    services: { 
     facebook: fb 
    }, 
    profile: { 
     first_name: fb.first_name, 
     last_name: fb.last_name, 
    } 
} 

が、私は、適切なトークンまたは何流星が必要で、これまでの指標としてユーザーをログインに失敗しました:私はそうのようにそれをフォーマットしているので、この方法は利用できない二つのパラメータを要求します:

getLoginByExternalService(options) { 
    if (Meteor.userId()) throw new Meteor.Error('400',`Please logout ${Meteor.userId()}`); 
    const email = options.services.facebook.email 
    const facebookId = options.services.facebook.id 
    const user = {services: {}} 
    user.services = options.services 
    const users = Meteor.users.find({"services.facebook.id": facebookId}).fetch(); 

    if (!users.length) { 
     const userId = Accounts.insertUserDoc(options, user) 
     if (Meteor.isServer) 
      this.setUserId(userId) 
     else 
      Meteor.setUserId(userId) 
     return userId 
    } else { 

     if (Meteor.isServer) 
      this.setUserId(users[0]._id) 

     if (Meteor.isClient) 
      Meteor.setUserId(userId) 

     return {users, userId: Meteor.userId()} 
    } 
} 

正しくログインするには?

答えて

1

私はすでに答えがあります。私はFacebookの応答からデータの返信をフォーマットする必要はありません。だからここ

getLoginByExternalService(resp) { 
    if (Meteor.userId()) Meteor.logout(Meteor.userId()) //who knows? 
    const accessToken = resp.accessToken 
    const identity = getIdentity(accessToken) 
    const profilePicture = getProfilePicture(accessToken) 
    const serviceData = { 
     accessToken: accessToken, 
     expiresAt: (+new Date) + (1000 * resp.expiresIn) 
    } 
    const whitelisted = ['id', 'email', 'name', 'first_name', 'last_name', 'link', 'username', 'gender', 'locale', 'age_range'] 
    const fields = _.pick(identity, whitelisted) 
    const options = {profile: {}} 
    const profileFields = _.pick(identity, getProfileFields()) 
    //creating the token and adding to the user 
    const stampedToken = Accounts._generateStampedLoginToken() 
    //hashing is something added with Meteor 0.7.x, 
    //you don't need to do hashing in previous versions 
    const hashStampedToken = Accounts._hashStampedToken(stampedToken) 
    let ref = null 
    _.extend(serviceData, fields) 
    _.extend(options.profile, profileFields) 
    options.profile.avatar = profilePicture 

    try { 
     ref = Accounts.updateOrCreateUserFromExternalService("facebook", serviceData, options); 
    } catch (e) { 
     if (e.reason === "Email already exists.") { 
     const existingUser = Meteor.users.findOne({ 'emails.address': identity.email }) 
     if (existingUser) { 
      if (identity.verified) { 
       Meteor.users.update({ _id: existingUser._id }, { $set: { 'services.facebook': serviceData }}) 
       ref = { userId: existingUser._id } 
       console.log(`Merged facebook identity with existing local user ${existingUser._id}`); 
      } else { 
       throw Meteor.Error(403, "Refusing to merge unverified facebook identity with existing user") 
      } 
     } 
    } else { 
     throw Meteor.Error(e.error, e.reason) 
    } 
    } 

    Meteor.users.update(ref.userId, {$push: {'services.resume.loginTokens': hashStampedToken}}) 

    return {id: ref.userId, token: stampedToken.token} 
} 

ので、どこかでフロントエンド

asteroid.call("getLoginByExternalService", data).then(response => response) 
でバックエンドの実装
関連する問題