Save my secrets!

JavaScript (ES6),  60  50 bytes

Saved 10 bytes thanks to @JonasWilms!

Takes input as either (alias,password,secret) or (alias,password). Returns undefined the first time a secret is stored, or false if the password is incorrect.


Try a 1st test case online!

Try a 2nd test case online!


We define a named function \$f\$ whose underlying object is also used to store the passwords and the secrets.


f = (             // f = named function whose underlying object is used for storage
  a,              // a = alias
  p,              // p = password
  s,              // s = optional secret
  [P, v] = f[a]   // if f[a] is defined, retrieve the password P and the secret v
           || [p] // otherwise, copy p in P
) =>              //
  p == P && (     // if p is not equal to P, yield false; else:
    f[a] = [      //   update f[a]:
      P,          //     save the new password (or leave it unchanged)
      s || v      //     save the new secret if it's defined, or keep the previous one
    ],            //
    v             //   return the previous secret
  )               //

Python 2, 94 93 bytes

def f(a,p,s=0,d={}):
 if q==p:d[a]=p,s or t;return t
 elif q<1<s:d[a]=p,s

Try it online!

For once, Python's weird default dict parameter works in my favor...

Ruby, 64 bytes

Builds a hash for aliases to a single key-pair of password => secret. Probably could be more elegant.


Try it online!