Slide 1

Slide 1 text

AUTHORIZATION ELIXIR/ERLANG MEETUP: DECEMBER 5TH @JESSICAEWEST

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

AUTHORIZATION Basics of User ▸ Add a User model to our application Make use of our generators, User users email: string, password: string

Slide 4

Slide 4 text

AUTHORIZATION Basics of User ▸ Add Registration to our routes and link in views

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

AUTHORIZATION User Login with Sessions ▸ With users, come great responsibility. Let’s add a login/ logout feature ▸ Add login/logout to your routes

Slide 9

Slide 9 text

AUTHORIZATION User Login with Sessions ▸ Create Sessions controller

Slide 10

Slide 10 text

AUTHORIZATION User Login with Sessions ▸ Create Sessions View ▸ Edit Sessions Controller to handle create action

Slide 11

Slide 11 text

AUTHORIZATION User Loginwith Sessions ▸ Create Sessions model ▸ Add login

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

TEXT HELPFUL MIX COMMANDS ▸ Running your server ▸ mix phoenix.server ▸ Looking at your routes ▸ mix phoenix.routes

Slide 14

Slide 14 text

TEXT ALTERNATIVELY… SOMEONE ELSE HAS BUILT THIS ▸ Addict: https://github.com/trenpixster/addict ▸ Guardian: https://github.com/ueberauth/guardian