How to set conn.secret_key_base in a Phoenix app

The following error appears in my Phoenix application:

cookie store expects conn.secret_key_base to be set

It looks like this comes from a line of |> redirect(to: session_path(conn, :new))this authentication function in my PostController module:

  defp authenticate(conn, _opts) do
    if conn.assigns.current_user do
      conn
    else
      conn
      |> put_flash(:error, "You must be signed in to post a project.")
      |> redirect(to: session_path(conn, :new))
      |> halt()
    end
  end  

Obviously, this error means that you need to set conn.secret_key_base.

Where and how to set this value?

+4
source share
2 answers

This must be specified by default when creating the phoenix application:

https://github.com/phoenixframework/phoenix/blob/2861f0db3df3d81ee6ce79f928ef4e0b439c4dcd/installer/templates/new/config/config.exs#L16

If this configuration is not available for you, put the following in config/config.exs:

config :my_app, MyApp.Endpoint,
  secret_key_base: "some_secret",

mix phoenix.gen.secret , "some_secret".

+11

config/prod.secret.exs. , , .

# config/prod.secret.exs

use Mix.Config

config :trope_api, MyApp.Endpoint,
  secret_key_base: "SOMEVERYLONGSTRING"

config/prod.exs

# config/prod.exs

# Finally import the config/prod.secret.exs
# which should be versioned separately.
import_config "prod.secret.exs"

, , - , .

:

# config/prod.exs

# Just a test vaule for env variables
config :my_app, MyApp,
  test_value: System.get_env("TESTCONFIG")

$ PORT=4001 MIX_ENV=prod TESTCONFIG=testvalue mix phoenix.server

, . .env ( ) .gitignore. , :

export TESTCONFIG="Test Config Value"
export OTHERTESTCONFIG="Other Test Config Value"

source .env . . , , , . , , .env (db ..).

.env , .

+3

All Articles