Sinatra Bootstrap Starter / routes / user.rb

#routes

### Sign up ###
get '/signup' do
  haml :signup
end

post '/signup' do
  user = User.create(params[:user])
  user.password_salt = BCrypt::Engine.generate_salt
  user.password_hash = BCrypt::Engine.hash_secret(params[:user][:password], user.password_salt)
  if user.save
  	flash[:info] = "Thank you for registering #{user.email}"
  	session[:user] = user.token
  	redirect "/"
  else
    flash[:error] = "Doh! Something went wrong!"
  	redirect "/signup"
  end
end

### Log in ###
get '/login' do
  if current_user
    redirect_last
    
  else
    haml :login
  end
end

post '/login' do
  user = User.first(:email => params[:email])
  if user
  	if user.password_hash == BCrypt::Engine.hash_secret(params[:password], user.password_salt)
  	  session[:user] = user.token
      if params[:remember_me]
  	    response.set_cookie "user", {:value => user.token, :expires => (Time.now + 52*7*24*60*60)}
  	  end
      flash[:info] = "Successfully logged in"
  	  redirect_last
      
    else
      flash[:error] = "Email/Password combination does not match"
      redirect "/login"
    end
  else
  	flash[:error] = "That email address is not recognised"
  	redirect "/login"
  end
end

### Log out ###

get '/logout' do
  current_user.generate_token
  response.delete_cookie "user"
  session[:user] = nil
  flash[:info] = "Successfully logged out"
  redirect "/login"
end

### User Profile Page ###
get '/:username' do
  haml :profile
end

### Edit user account ###
get '/:username/edit' do
  @user = User.first()
  haml :edit
end

put '/:username/edit' do
  user = User.first
  success = user.update!(params[:user])
  
  if success
    flash[:info] = "Successfully updated!"
    redirect_last
  else
    flash[:error] = "Doh! Something went wrong!"
    redirect "/:username/edit"
  end
end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.