AngularFire2 - 如何获取Auth用户电子邮件?

时间:2016-12-14 14:38:10

标签: angular typescript angularfire2

我如何获得当前的Auth用户电子邮件?通过

来获取他的uID非常容易
constructor(public af: AngularFire){
           this.user = this.af.auth.getAuth().uid;
}

但我无法收到他的电子邮件。我怎么能得到这个?在getAuth()方法中,我可以获得uId和提供者编号。

1 个答案:

答案 0 :(得分:0)

要访问email属性,首先需要访问auth属性。 (如果你json它,getAuth()将返回{auth { .... }}

转换auth.displayName我自己使用以下方法。需要string一个或多个单词。如果只有一个字,则会将其设置为lastname,并显示空firstname。在2个或更多单词上,第一个单词将是firstname以及lastname之后的所有内容(由于string之后的一些逻辑被分割在空格上,并且尾随空格看起来更好长lastname

在方法本身中,我解释了' Edwin van der Sar'将被处理。

// Participant = {id: number, firstname: string, lastname: string}
    private splitName(part: Participant, fullname: String): Participant {
            // split the name by space (first last) (['Edwin','van','der','Sar']);
            let splittedName:Array<string> = fullname.split(' ');

            // if only one word is displayed (f.e. ivaro18) set it as lastname 
            // (improves search results later on)
            if(splittedName.length < 2) {
                part.lastname = splittedName[0];
            }
            // if first and last name are displayed
            else if (splittedName.length < 3) {
                part.firstname = splittedName[0];
                part.lastname = splittedName[1];
            }
            // if the user has a hard name
            else if (splittedName.length >= 3) {
                // first part will be the firstname (f.e. 'Edwin')
                part.firstname = splittedName[0];
                // loop through all other and concat them to be the lastname
                for(var i = 1; i < (splittedName.length); i++){
                    if(part.lastname != undefined){
                        // first part is already there, if it isnt the last part add a space ('der ')
                        if(!(i == (splittedName.length -1))) {
                            part.lastname += splittedName[i] +" ";
                        } else {
                            // it's the last part, don't add a space ('Sar')
                            part.lastname += splittedName[i];
                        }
                    }
                    // first part of the lastname ('van ')
                    else {
                        part.lastname = splittedName[i] +" ";
                    }

                }
            }
            return part;
    }

(很有可能可以改进,但它根本不慢,所以它不是我的先例之一)