2016-12-30 3 views
0

Meine Anwendung ist mit React gebaut, die vollständig von Meteor getrennt ist. Ich benutze Asteroid als Schnittstelle zu Meteor, das nur als Backend dient. Ich habe den Facebook-Login-Button am Frontend manuell angelegt und möchte die von Facebook geholten Daten an Accounts.createUser übergeben. Diese Methode verlangt zwei Parameter, die nicht verfügbar ist, weil ich es wie so formatiert haben:Accounts.createUser ohne Benutzername, Passwort und E-Mail

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

I wie unten ein Verfahren geschaffen haben, aber ich konnte den Benutzer mit entsprechenden Token anmelden oder was auch immer Indikator, der Meteor benötigt :

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()} 
    } 
} 

Wie man den Benutzer richtig einträgt?

Antwort

1

Okay, ich habe schon die Antwort bekommen. Ich muss die Datenrückgabe nicht von der Facebook-Antwort formatieren. Also hier die Umsetzung im Backend

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} 
} 

so irgendwo am vorderen Ende

asteroid.call("getLoginByExternalService", data).then(response => response) 
Verwandte Themen