January 4, 2016

Random Password Generator in cljs

I described how I generated "fake" data with clojure in a previous post.

That code has come in quite handy, I use it all the time to generate random passwords from the command line. But having to fire up a jvm every time that I need to generate a new password is a pain. So, let's use some clojurescript!

In the previous post, I mentioned that the test.check library is available in both clojure and clojurescript. So, in this post, I'd like to walk thru how I used clojurescript and devcards to create a convenient, web version of a random password generator.

Creating the cljs Password Generator

First, I created a new devcards project. I named my project "upgradingdave" like so:

lein new devcards upgradingdave

Note: Be careful to use a unique name for your project (specifically, try to avoid naming your project cljs or devcards). If you get a blank screen when you browse to cards.html, then you might need to try a different name. (see here for more info)

Next I added [org.clojure/test.check "0.9.0"] to the list of dependencies inside project.clj

Then, the next step is to start figwheel:

cd upgradingdave
lein figwheel

At this point, I could browse to http://localhost:3449/cards.html and see some devcards. Not only that, but figwheel is humming away so that any changes made to cljs source will automatically show up in the browser. How cool is that? A shiny new cljs project in 30 seconds!

Next, I opened and edited src/upgradingdave/core.cljs.

I added the code from my previous post, so that I could call gen-pwd in order to generate a password in clojurescript.

And then I added a little bit of code needed to display a form with an input field for "Password Length" and a button labeled "Generate Password". And that's all there was to it!

For more details and to see some code, check it out here.

Tags: clojure software clojurescript