Sinatra Authentication


(Surya Poojary) #1

Hello rubyists!

I'm trying to learn Sinatra and want to implement an authentication system
like

If username == "me" and password == "she" // go to the next page

Else

// Take to some page that says "you are not authorised"

How do I do this ?

Gracias,

Rubyists.


(Raj Sahae) #2

- Raj

···

On Sat, Oct 20, 2018, 2:14 AM Surya Poojary <suryapjr@gmail.com> wrote:

Hello rubyists!

I'm trying to learn Sinatra and want to implement an authentication system
like

If username == "me" and password == "she" // go to the next page

Else

// Take to some page that says "you are not authorised"

How do I do this ?

Gracias,

Rubyists.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


(Dmitriy Non) #3

As Raj suggested, you should've just googled it:)

Though I would recommend to decompose your problem.
Obviously, you need to learn how to recieve parameters in your sinatra app.
Then you need to learn how to keep user data. Session will do, right?
And last, you need to learn how to redirect user.

So, google this topics:

1. sinatra parameters
2. sinatra session
3. sinatra redirect

and go from there

···

On 20 Oct 2018, at 12:13, Surya Poojary <suryapjr@gmail.com> wrote:

Hello rubyists!

I'm trying to learn Sinatra and want to implement an authentication system like

If username == "me" and password == "she" // go to the next page

Else

// Take to some page that says "you are not authorised"

How do I do this ?

Gracias,

Rubyists.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


(Ale Miralles) #4

I recommend you to check the Sinatra's home page. They put in there almost
all you need to get a Sinatra app off the ground.
Anyway, here is a minimum example you can use to fiddle around a little bit.

This is the contents of the main file. (i.e., app.rb):

require 'sinatra'
enable :sessions

get "/" do
if session[:usr]
erb :index
else
erb :login
end
end

post "/create_session" do
if params[:usr] == "amiralles" && params[:pwd] == "123"
session[:usr] = params[:usr]
redirect "/"
else
erb :login
end
end

Now, create a directory called "views" and place this files on it:

Homepage: index.erb
<h1>
Wellcome <%= session[:usr] %>!
</h1>

Login page: login.erb
<form action="/create_session" method="POST">
<input name="usr" type="text"></input> <br/>
<input name="pwd" type="password"></input> <br/>
<input type="submit" value="Login"></input>
</form>

Finally, run `ruby app.rb` and you will be ready to go.

Hope this helps!

~ Ale Miralles.


http://amiralles.com.ar

···

On Sat, 20 Oct 2018 at 06:14, Surya Poojary <suryapjr@gmail.com> wrote:

Hello rubyists!

I'm trying to learn Sinatra and want to implement an authentication system
like

If username == "me" and password == "she" // go to the next page

Else

// Take to some page that says "you are not authorised"

How do I do this ?

Gracias,

Rubyists.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>