Created
March 28, 2022 08:39
-
-
Save wttj-tech/84c77e4c0bb40e8f1b40732ac0c451ca to your computer and use it in GitHub Desktop.
Password rotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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