Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Phoenix Part II

Intro to Phoenix Part II

Hand rolling Authorization with Phoenix

Jessica Goulding

December 05, 2016
Tweet

More Decks by Jessica Goulding

Other Decks in Technology

Transcript

  1. WHAT WE COVERED LAST TIME ▸ Created new project ▸

    Reviewed Generators and what they gave us. ▸ Generated post model, controller and view ▸ Added Ex-Machina for test data
  2. AUTHORIZATION Basics of User ▸ Add a User model to

    our application Make use of our generators, User users email: string, password: string
  3. AUTHORIZATION Basics of User ▸ Add Controller and New Registration

    View ▸ Signup form view and controller action We can use pry here to look at what we are doing through each step
  4. AUTHORIZATION Validating and persisting User ▸ Add Validations to User

    model We are now at a point where we can submit a form, but still have errors. Now we are dealing with passwords, we need to do some work. We need a password hashing library, let’s use: https://github.com/riverrun/comeonin Add comeonin to mix.exs file, then run mix.deps.get. We can then implement our password function like we have below
  5. AUTHORIZATION Validating and persisting User ▸ Create Registrations Model ▸

    Add create to take in changeset and a repo ▸ Hash dat password We are now at a point where we can submit a form, but still have errors. Now we are dealing with passwords, we need to do some work. We need a password hashing library, let’s use: https://github.com/riverrun/comeonin Add comeonin to mix.exs file, then run mix.deps.get. We can then implement our password function like we have below
  6. AUTHORIZATION User Login with Sessions ▸ With users, come great

    responsibility. Let’s add a login/ logout feature ▸ Add login/logout to your routes
  7. AUTHORIZATION User Login with Sessions ▸ Create Sessions View ▸

    Edit Sessions Controller to handle create action
  8. AUTHORIZATION Clean Up Views ▸ Add current_user method to our

    session model ▸ Replace registration link in app with a check using the above method for if user is logged in, or if they need to Login/Register ▸ Who wants to sign in right after you create an account? Let’s clean up that last point to automatically login our users in after creation.
  9. TEXT HELPFUL MIX COMMANDS ▸ Running your server ▸ mix

    phoenix.server ▸ Looking at your routes ▸ mix phoenix.routes