Search code examples
authenticationsinatrawarden

Creating and authenticating user on post ("sign up"). Warden + Sinatra


TL;DR: How can I create a sign up feature with sinatra and warden?

I am trying to make a simple authentication system for a Sinatra app, and I found that warden is probably the best bet. I have found numerous examples on how to use it. I started working from the examples by sklise.

I quickly run into the problem of signing up. See that it is possible to create a new user with something like

post '/auth/signup' do
  u = User.new(:username => params[:username], :password => params[:password])
  u.save

But then what? How can I authenticate / sign this brand new user in? I cannot seem to find any single reference to how a sign up feature should be built with sinatra + warden. As a matter of fact, I can't seem to find anything for warden at all. None of the examples on the Warden wiki has a sign up feature. Do anyone have a solution for this?

Thanks


Solution

  • Not exactly a warden aficionado, but this looks like where you want to look:

    https://github.com/hassox/warden/blob/906edf86c6c31be917a921097031b89361d022e8/lib/warden/strategies/base.rb#L116

    From your example I think you'd want to do something like this:

    post '/auth/signup' do
      u = User.new(:username => params[:username], :password => params[:password])
      u.save
      env['warden'].success!(u)
    ...