Michael Hartl (http://www.railstutorial.org/book/modeling_users)의 레일 튜토리얼 서적을 기반으로 매우 간단한 레일 앱이 있습니다. 이 앱은 똑같이 간단한 iOS 앱을위한 콘텐츠 관리 시스템입니다. 나는 devise가 매우 유명하다는 것을 알고 있지만, 나는 이것이이 프로젝트에 필요하다고 생각하지 않는다. 내 iOS 앱을 내 레일 앱에 연결할 수 있기를 원하지만 내가 알 수있는 유일한 조언을 찾는 곳은 어디서나 궁리하다. 내가 원하는 것은 로그인 화면을 사용자에게 제공하여 세션을 설정할 수있게 한 다음 레일스 끝의 모든 권한 논리를 처리 할 수있게하려는 것뿐입니다. 여기에 내 현재 인증 방식의 아이디어를 제공 할 몇 가지 사항rails + iOS authentication with devise
내 세션 컨트롤러 :
class SessionsController < ApplicationController
def new
end
##
#Use the email in the nested hash to find the right user
#Check to make sure that the user authenticates with the given password
##
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or root_url
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
current_user.update_attribute(:remember_token,User.digest(User.new_remember_token))
cookies.delete(:remember_token)
self.current_user = nil
session.delete(:return_to) #not sure if this should really be here or if better way to fix bug
redirect_and_alert(root_url, "User Successfully Logged Out!",:success)
end
end
세션 도우미 :
module SessionsHelper
##
#set the remember token for the user
#make the cookie reflect that token
#update the users remember token column
#set the user being passed in as the current user
##
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.digest(remember_token))
self.current_user = user
end
#set the current user
def current_user=(user)
@current_user = user
end
#Helper current user method
def current_user
remember_token = User.digest(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
#Is the requesting user the current user
def current_user?(user)
user == current_user
end
#Is the user signed in?
def signed_in?
!current_user.nil?
end
#Store user request info for friendly forwarding
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
session.delete(:return_to)
end
#Store user request info for friendly forwarding
def store_location
session[:return_to] = request.url if request.get?
end
#Authorization
def signed_in_user
store_location
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
def super_user
redirect_and_alert(root_url,
"You are not allowed to do that. Contact the admin for this account.",
:error) unless (current_user.role.id == 1)
end
def super_user_or_admin
redirect_and_alert(root_url,
"You are not allowed to do that. Contact the admin for this account.",
:error) unless (current_user.role.id == 1 || current_user.role.id == 2)
end
def is_super_user
current_user.role.id == 1
end
def is_admin
current_user.role.id == 2
end
def is_regular_user
current_user.role.id == 3
end
end