Mochi
  • Introduction
  • Installation
  • Usage
  • Guides
    • Configuration
    • Authenticable
      • Introduction
      • CLI Installation
      • Manual Installation
      • Views
        • Layouts
          • _session.ecr
        • Session
          • new.ecr
        • User
          • edit.ecr
          • show.ecr
          • new.ecr
      • Models
        • granite_user.cr
        • jennifer_ser.cr
      • Controllers
        • user_controller.cr
        • session_controller.cr
      • Pipes
        • authenticate.cr
    • Confirmable
      • Introduction
      • CLI Installation
      • Manual Installation
      • Controllers
        • registration_controller.cr
      • Mailers
        • confirmation_mailer.ecr
        • confirmation_mailer.text.ecr
    • Trackable
      • Introduction
      • CLI Installation
      • Manual Installation
    • Omniauthable
      • Introduction
      • CLI Installation
      • Manual Installation
    • Recoverable
      • Introduction
      • CLI Installation
      • Manual Installation
      • Controllers
        • registration_controller.cr
      • Mailers
        • confirmation_mailer.ecr
        • confirmation_mailer.text.ecr
    • Mochi Mailer
  • Roadmap
  • Contributing
  • Contributors
Powered by GitBook
On this page

Was this helpful?

  1. Guides
  2. Authenticable

Manual Installation

  1. Create mochi.cr in initalizers and paste this in:

     require "mochi"
  2. Create a migration and paste in:

    Granite:

    ```sql -- +micrate Up CREATE TABLE users ( id INTEGER NOT NULL PRIMARY KEY, email VARCHAR, password_digest VARCHAR, created_at TIMESTAMP, updated_at TIMESTAMP );

  -- +micrate Down
  DROP TABLE IF EXISTS users;
```

or

**Jennifer:**

```crystal
class CreateUser < Jennifer::Migration::Base
  def up
    create_table(:users) do |t|
      t.string :email
      t.string :password_digest
      t.timestamp :created_at
      t.timestamp :updated_at
    end
  end

  def down
    drop_table(:users)
  end
end
```
  1. Migrate:

    Granite:

     amber db migrate

    or

    Jennifer:

     crystal sam.cr -- db:migrate
  2. Create a controller titled user_controller.cr and paste in this file:

  3. Create a controller titled session_controller.cr and paste in this file:

  4. Add these to your routes:

    Change pipeline :web to pipeline :web, :auth and add:

     plug CurrentUser.new

    Create an :auth pipeline with:

     pipeline :auth do
       plug Authenticate.new
     end

    Create a new route section just for :auth:

     routes :auth do
       get "/profile", UserController, :show
       get "/profile/edit", UserController, :edit
       patch "/profile", UserController, :update
       get "/signout", SessionController, :delete
     end

    Add this to your :web routes:

     get "/signin", SessionController, :new
     post "/session", SessionController, :create
     get "/signup", UserController, :new
     post "/registration", UserController, :create
  5. Create a piple titled authenticate.cr and paste in this file:

  6. Copy & Paste all the views found here:

  7. Add a model:

    Granite:

    or

    Jennifer:

  8. Open config/application.cr and between the # Start Generator & # End Generator add:

    require "../src/models/**"
    require "../src/pipes/**"
  9. Open application_controller.cr and add:

    def current_user
      context.current_user
    end
  10. Done! And that's why we have a CLI.

PreviousCLI InstallationNextViews

Last updated 5 years ago

Was this helpful?

{:target="_blank"}

user_controller
session_controller
authenticate
Views
user
user