4

I am still new to swift and coding with Xcode and using Firebase. I have been able to create a sign up and login page with Firebase authentication. I have been wondering how would I allow people to create custom unique usernames. I have been searching for a while now on the web and I can't find much.

I want to learn how to implement usernames with in my app so users can create unique usernames and have their own profiles.

Aloush
  • 45
  • 5
  • You can combine the authentication system (`Firebase/Auth`) with the `Firebase Database` and generate an unique key for each user, to be associated with their email. Each time you create a new user, loop through those values and check if the chosen username already exists. But make sure that your security rules are very well structured... – Mr. Xcoder Mar 19 '17 at 11:56
  • Thanks! is there any website or link with a tutorial i can check out that shows me how to those things, i am still pretty new to xcode. – Aloush Mar 19 '17 at 12:45
  • Firebase uses emails as usernames, and that's what users use to log into Firebase. Those usernames (emails) are stored internally on the Firebase authentication server. Do you want to create another unique username separate from the email? – Jay Mar 19 '17 at 13:33
  • Try this link :- http://stackoverflow.com/a/39612171/6297658 – Dravidian Mar 19 '17 at 14:26
  • @Jay Yes, how would i do that? – Aloush Mar 20 '17 at 06:03
  • @Aloush Was the answer you accepted an answer to the question? I am asking as it doesn't actually answer the question about how to create usernames with Firebase. – Jay Mar 20 '17 at 14:35
  • @Jay Nope, i thought it worked and began to try it out, it allowed me to store usernames into the database and such, i still do not know how to allow users to login with a username and how do show a user there username. – Aloush Mar 21 '17 at 06:54
  • Firebase works through an authentication system where users have a login (the email) and password. Those are used to authenticate against the Firebase server and are stored securely within the Firebase server. While you can craft your own authentication system which would enable the use of 'usernames' that are not emails, it's probably not the right way to go. If you can explain your use case and why you want usernames and bypass the built-in authentication system we may be able to provide a solution. Can you *update your question with more info* please? – Jay Mar 21 '17 at 12:10
  • I updated why i wanted usernames. Also thanks for your help, i appreciate it! – Aloush Mar 22 '17 at 11:05
  • It's still unclear why you need a separate username. Firebase authentication uses the email address as the username and assigns the user a uid. With that uid, you can store all kinds of information, a profile, pictures, or really whatever you want about the user in Firebase. Each email username is unique and Firebase has built in functionality to handle rules, security etc already built in. Adding a totally separate username really overcomplicates things and is most likely not needed. – Jay Mar 22 '17 at 14:17
  • Also, do you want to be able to log in with the separate username or is it for some other purpose? – Jay Mar 22 '17 at 14:21

2 Answers2

2

After creating user in Firebase Auth do something like this:

let newUser = UserItem(uid: < Value >, email: < Value >, login: < Value >, createdDate: < Value >)

// Create a child path with a key set to the uid underneath the "users" node
let refToMainDB = FIRDatabase.database().reference(withPath: "< PathToMainDB >")
refToMainDB.child("users").child(< uid >).setValue(newUser.toAnyObject()

For example my UserItem struct:

import Foundation
import FirebaseAuth
import FirebaseDatabase

struct UserItem {
let uid: String

let email: String
let login: String
let bioDescription: String
let nameAndSename: String
let createdDate: String

let ref: FIRDatabaseReference?

init(uid: String, email: String, login: String, bioDescription: String = "", nameAndSename: String = "", createdDate: String) {
    self.uid = uid

    self.email = email
    self.login = login
    self.bioDescription = bioDescription
    self.nameAndSename = nameAndSename
    self.createdDate = createdDate

    self.ref = nil
}

init(snapshot: FIRDataSnapshot) {
    uid = snapshot.key

    let snapshotValue = snapshot.value as! [String: AnyObject]
    email = snapshotValue["email"] as! String
    login = snapshotValue["login"] as! String
    bioDescription = snapshotValue["bioDescription"] as! String
    nameAndSename = snapshotValue["nameAndSename"] as! String
    createdDate = snapshotValue["createdDate"] as! String

    ref = snapshot.ref
}

func toAnyObject() -> Any {
    return [
        "uid": uid,
        "email": email,
        "login": login,
        "bioDescription": bioDescription,
        "nameAndSename": nameAndSename,
        "createdDate": createdDate
    ]
}
}

You can check my example of code for this here Hope it helps

Vlad Pulichev
  • 3,162
  • 2
  • 20
  • 34
  • Thanks so much! just a question, how would i show the username on a label, just like how you can show emails by doing self.label.text = "Welcome " + (FIRAuth.auth()?.currentUser?.email)! – Aloush Mar 20 '17 at 11:20
  • @Aloush You will do it another way. You need to fetch UserItem from database. I added another answer. Check it – Vlad Pulichev Mar 20 '17 at 11:24
0

In addition to my comment.

let currentUserId = FIRAuth.auth()?.currentUser?.uid
let currentUserRef = FIRDatabase.database().reference(withPath: "MainDataBase/users").child(currentUserId!) // MainDataBase/users - is part from you database

currentUserRef.observeSingleEvent(of: .value, with: { snapshot in
        self.userInfo = UserItem(snapshot: snapshot)
        self.label.text = "Welcome " + (self.userInfo.login)!  // for example it will show login
    })

Good luck

EDIT: Its better to have something like this:

var userInfo: UserItem! {
    didSet {
        self.label.text = "Welcome " + (self.userInfo.login)!  // for example it will show login
        // other actions
    }
Vlad Pulichev
  • 3,162
  • 2
  • 20
  • 34
  • Thank you once again, i am just having a slight error which comes which says 'value of type 'loggedInViewController' has no member 'userInfo'. Sorry for all the questions, i am still learning swift. – Aloush Mar 20 '17 at 11:56
  • @Aloush add var to your controller like me in edit. If it will help, mark as answer. Thx and good luck – Vlad Pulichev Mar 20 '17 at 12:09