> For the complete documentation index, see [llms.txt](https://awcrotwell.gitbook.io/mochi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://awcrotwell.gitbook.io/mochi/guides/authenticable/pipes/authenticate.cr.md).

# authenticate.cr

Copy & Paste the code block below into: `pipes/authenticate`

```
class HTTP::Server::Context
  property current_user : User?
end

class CurrentUser < Amber::Pipe::Base
  def call(context)
    user_id = context.session["user_id"]?
    call_next(context) if user_id.nil?

    user = User.find user_id

    # Uncomment for omniauthable
    # user ||= User.where { _uid == user_id }.first

    if user
      context.current_user = user
    end
    call_next(context)
  end
end

class Authenticate < Amber::Pipe::Base
  def call(context)
    if context.current_user
      call_next(context)
    else
      context.flash[:warning] = "Please Sign In"
      context.response.headers.add "Location", "/signin"
      context.response.status_code = 302
    end
  end
end
```
