簡體   English   中英

Ruby Sinatra-根據本地用戶數據庫對用戶進行身份驗證(Linux / Mac)

[英]Ruby Sinatra - Authenticate user against local user database (linux / mac)

是否可以針對linux / mac上的本地用戶數據庫對用戶進行身份驗證? 我想在linux上本地創建用戶,然后使用sinatra或任何其他建議的ruby gem強制進行身份驗證(沒有rails知識:()

我沒有任何數據庫,我的應用程序是如此簡單,因此應如下所示:

require 'sinatra'

use Rack::Auth::Basic, "Restricted Area" do |username, password|
  [username, password] == ['admin', 'admin']
end

get '/' do
  "You're welcome"
end

我的建議是使用數據庫。 如果您最終選擇了這條路線,請按照以下步驟操作:

添加到您的gemfile gem'sqlite gem 'sqlite'和gem'sinatra gem 'sinatra-activerecord'運行命令bundle exec rake db:create_migration NAME=setup_users_table 這將創建一個包含migrations/<random numbers>_setup_users_table.rbdb目錄。 在該文件中,在change函數內添加代碼。 要創建帶有用戶名和密碼字段的Users表,請添加以下代碼:

create_table :users do |i|
    i.string :username
    i.string :password
end

現在運行bundle exec rake db:migrate 如果成功,則您有一個正常工作的數據庫。 要訪問它,您需要將以下代碼添加到您的應用文件中:

class User < ActiveRecord::Base
end

現在您可以出發了!

創建用戶:

User.create(username:<whatever>,password:<whatever>)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM