Tried to put this in to the update handler of the devise sessions controller, but that proved to be a nightmare... Stashed it here. Let me know if you'd prefer it elsewhere.