Menu

1 / 3

Building a Secret Santa draw in Next.js: derangements, HMAC links, and not leaking emails

DEV Community: nextjs·kaka·about 10 hours ago
#zcyr7EjA
#devtonextjs#devto#dev#assignment#draw
Reading 0:00
15s threshold

Every December someone ships a Secret Santa app, and every December the same three things go wrong: the shuffle loops forever on a tricky exclusion list, the "private" assignment link contains the recipient's email in plain text, and the organizer dashboard quietly shows the whole pairing in a mode that was supposed to be blind.

I built one this year. These are the parts that were not obvious, with the code that ended up working.

1. A shuffle is not a draw

The obvious implementation is: shuffle the participant list, pair everyone with the next person, check the rules, retry if it fails.

// don't do this do { names = shuffle ( names ) } while ( ! isValid ( names ) )

Enter fullscreen mode

Exit fullscreen mode

This has two problems. With a tight exclusion list (couples, families) the valid-permutation rate collapses and the loop runs for a long time.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.