Guardian - How do you set up a secret key?

Please forgive the noob question, but I’m following tutorials and finding I’m getting an error in my config/config.exs related to the serializer line - as an undefined function.

Can someone please take me through the steps you use?
And can you show me how you do it for dev and production envs, which I’m still learning.


Here’s my config/config.exs

config :guardian, Guardian,
  allowed_algos: ["HS512"], # optional
  verify_module: Guardian.JWT,  # optional
  issuer: "myapp",
  ttl: { 30, :days },
  verify_issuer: true, # optional
  serializer: myapp.GuardianSerializer,
  secret_key: to_string(Mix.env),
  hooks: GuardianDb,
  permissions: %{
    default: [
1 Like

Remember what I said about adding as much info as possible @RisingFromAshes :wink:

  • Which tutorials are you following? (Include links)
  • Paste the exact errors you are getting
  • Include as much info as possible for others to reproduce the problem

Also, when posting code use Markdown ticks to format it properly in your post (I’ve edited your post above - click the pencil icon to see the changes I made). As I mentioned in your other thread, you are more likely to get help if you take the time into writing out your post with thought (and consideration) :slight_smile:

1 Like

Sorry, I was in a rush (and I’m having a bad day), normally I try to give as much info as poss but I’ve been reading multiple tutorials.

Here’s 3 that I’ve looked at and tried to follow in part to try to set up Guardian:

Phoenix: simple authentication & authorization in step-by-step tutorial form

Elixir /Phoenix — Lets code authentication. Todo application part 3.

Authentication in Phoenix/Elixir app with Ueberauth and Guardian

Where I think I’m confused is - I set up config/config.exs as per my first post and get the following error after running:

mix phoenix.gen.secret

** (Mix.Config.LoadError) could not load config config/config.exs
    ** (CompileError) config/config.exs:33: undefined function myapp/0
    (mix) expanding macro: Mix.Config.config/3
    config/config.exs:26: (file)

Line 33 is:

serializer: myapp.GuardianSerializer,

I hope that helps thanks.

1 Like

Should myapp be Myapp or MyApp?


All fine. :slight_smile:

This should be correct. myapp is trying to access a variable/binding named myapp, Where MyApp or Myapp (upper-case initial character) is an atom, I.E. a name in this context. :slight_smile:


Any thoughts on what I am doing wrong?

myapp is not a valid module name. A module name starts with an upper case letter as @dsissitka and @OvermindDL1 have already mentioned. Basically you are telling guardian what the name of the module is that will be used as a serialiser. You have provided an invalid module name.

If you did a mix new myapp then probably your application module is probably Myapp or maybe MyApp as @OvermindDL1 said.


Thank you!

That made a lot more sense to me and has fixed the problem - I can now generate a secret key.

This community is awesome!!


Thanks, now I understand and really appreciate your explanation!

1 Like