Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
Notify User of New Signin with Firebase - Swift/Xcode

I have looked all over and can't figure out how to notify a user when a new device signs into their account. I'm using Firebase Authentication. I'd like to setup some kind of notification system or email that tells the user someone has signed into their account from a different device so they can know for security reasons.

Ideas?

Also, how could you monitor information about what device (s) are signed into a specific account? For example, maybe the user is curious how many devices he has signed into his account, what type of device they are, what the name of those devices are, and where they are located (example: San Antonio, Texas).

Ideas?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

yes, you can use keychain for this situation. First you should create an uniqueId. After you can save to the db uniqueId. This value will change if the user uses another device. I'm using third party framework for Keychain services. You can use framework. It's perfect.

https://github.com/kishikawakatsumi/KeychainAccess

final class DeviceManager {
    
    static let shared = DeviceManager()
    
    private init() { }
    
    var uniqueDeviceId: String {
        get {
            let keychain = Keychain(service: KeyManager.keychainServiceName).accessibility(.always)
            if let deviceId = keychain[KeyManager.keychainDeviceIdKey] {
                return deviceId
            }
            
            let vendorId = UIDevice.current.identifierForVendor!.uuidString
            keychain[KeyManager.keychainDeviceIdKey] = vendorId
            
            return vendorId
        }
    }
}
over 4 years ago · Santiago Trujillo Report

0

Super simple; have a field in your user document that stores a device name along with its status.

You app will be observing this users document and when something changes, all of the users devices will be notified of that change.

Let me set this up; here's a basic Firestore structure

users
   uid_0
      userName: "Jay"
      devices:
         device_0: offline
         device_1: offline

When the app starts, it will add an observer to this users document (using the uid as the documentId)

func observeUser() {
    let usersCollection = self.db.collection("users")
    usersCollection.document("uid_0").addSnapshotListener { (documentSnapshot, err) in

        guard let document = documentSnapshot else {
            print("Error fetching document: \(err!)")
            return
        }

        let device = document.get("devices") as! [String: String]
        print(device)
    }
}

Now in the Firestore closure shown above, if a users device changes status, offline to online for example, it outputs all of the devices to console. You would take whatever action is needed when the device changes status.

Keep in mind that if a NEW device is added, that event will also fire so you could present a message in the UI "A new device was added!"

So then some testing code that toggles the device_0 status from offline to online. I have a button click that does self.status = !self.status and then calls the toggleStatus function

var status = false
func toggleStatus() {
    var isOnline = ""
    if self.status == false {
        isOnline = "online"
    } else {
        isOnline = "offline"

    }
    let userCollection = self.db.collection("users")
    let thisDevice = "device_0"

    let devicesDict = [
        "devices":
            [thisDevice: isOnline] //sets device_0 to offline or online
    ]

    let document = usersCollection.document("uid_0").setData(devicesDict, merge: true)
}

In a nutshell, when a user authenticates with a device for the first time, it would perhaps ask for a device name, or craft one from the devices mac address or something under the hood. That device name is stored in the users document/devices with it's online status.

The device name would be stored locally as well, in user defaults for example so it's automatically sent up to Firestore upon login.

The end result here is that if any users devices change status; offline to online or vice versa, or any device is added or removed all of the devices are notified of that event.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!