Skip to content

Instantly share code, notes, and snippets.

@wttj-tech
Created March 28, 2022 08:39
Show Gist options
  • Save wttj-tech/84c77e4c0bb40e8f1b40732ac0c451ca to your computer and use it in GitHub Desktop.
Save wttj-tech/84c77e4c0bb40e8f1b40732ac0c451ca to your computer and use it in GitHub Desktop.
Password rotation
defmodule MyAppWeb.Router do
use Phoenix.Router
....
pipeline "auth" do
# Where we set :current_user
plug :require_authenticated_user
# Check password validity
plug MyApp.PasswordRotation
end
end
defmodule MyApp.PasswordRotation do
@expires_in 60
def init(opts), do: opts
def call(conn, _) do
current_user = conn.assigns[:current_user]
if Date.add(current_user.password_updated_at, @expires_in) < Date.utc_today() do
conn
|> Phoenix.Controller.put_flash("info", "Please update your password!")
|> Phoenix.Controller.redirect("/update-password")
|> Plug.Conn.halt()
else
conn
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment