Aloush
Aloush

Reputation: 45

How would I create Usernames with Firebase?

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.

Upvotes: 4

Views: 1535

Answers (2)

Vlad Pulichev
Vlad Pulichev

Reputation: 3272

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
    }

Upvotes: 0

Vlad Pulichev
Vlad Pulichev

Reputation: 3272

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

Upvotes: 2

Related Questions