Jenifer K
Jenifer K

Reputation: 79

how to get unique id of every device in angular

Hi i am new to angular ... now i am trying to insert cart data in database table ... in my cart i want to store itemid,qty,deviceuniqueid ... why this deviceuniqueid means , in my webapp there is no login for user ...i want to show cart details in cart page .. thats only i need deviceuniqueid.... so how to get deviceuniqueid in angular... i search more but i can't find solution ...so please support me to develop webapp .. if any other new way to storedata in localstorage means help me to do this...thank you

Upvotes: 0

Views: 5357

Answers (1)

Some random IT boy
Some random IT boy

Reputation: 8467

There's no way you can get a device unique id from the browser; but you can generate one yourself upon startup and save it into localStorage for later re-use!

function generateUUIDV4(): string {
    // http://www.ietf.org/rfc/rfc4122.txt
    const s = []
    const hexDigits = '0123456789abcdef'
    for (let i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
    }
    s[14] = '4'  // bits 12-15 of the time_hi_and_version field to 0010
    // eslint-disable-next-line no-bitwise
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = '-'

    const uuid = s.join('')
    return uuid
  }

function getDeviceId() {
  let deviceId = localStorage.getItem('deviceId')
  if(!deviceId) {
     deviceId = generateUUIDV4()
     localStorage.setItem('deviceId', deviceId)
  }
  return deviceId
}

Upvotes: 2

Related Questions