(oldEmail: string, newEmail: string)
| 192 | * Renames all keys ending with `.{oldEmail}` to end with `.{newEmail}`. |
| 193 | */ |
| 194 | export function migrateUserStorage(oldEmail: string, newEmail: string) { |
| 195 | if (!oldEmail || !newEmail || oldEmail === newEmail) return; |
| 196 | |
| 197 | const suffix = `.${oldEmail}`; |
| 198 | const keysToMigrate: string[] = []; |
| 199 | |
| 200 | for (let i = 0; i < localStorage.length; i++) { |
| 201 | const key = localStorage.key(i); |
| 202 | if (key?.endsWith(suffix)) { |
| 203 | keysToMigrate.push(key); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | for (const key of keysToMigrate) { |
| 208 | const newKey = key.slice(0, -suffix.length) + `.${newEmail}`; |
| 209 | const value = localStorage.getItem(key); |
| 210 | if (value !== null) { |
| 211 | localStorage.setItem(newKey, value); |
| 212 | localStorage.removeItem(key); |
| 213 | } |
| 214 | } |
| 215 | } |
no test coverage detected