0
Omniauth-identity gem을 사용하여 사용자를 생성하려고하는데 어떤 이유로 세션 오류가 발생합니다. 왜 신원이 사용자 생성 대신 세션 생성을 가리키는가? Identity를 생성하여 사용자를 만들고이를 올바른 경로로 리디렉션하려면 어떻게합니까? Omniauth- 사용자가 사용자를 생성하지 않음.
오류
새 사용자를 등록하는 동안No route matches {:action=>"feed", :controller=>"users", :id=>nil} missing required keys: [:id]
20 if user
21 session[:user_id] = user.id
22 redirect_to feed_user_path(@user), notice: "Signed in!"
23 else
24 session[:user_id] = nil
25 flash.now[:error] = 'Invalid email/password combination'
app/controllers/sessions_controller.rb:22:in `create'
routes.rb
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match "/auth/identity/callback", to: "sessions#create", via: 'post'
sessions_controller
def create
if omniauth_env = request.env.fetch("omniauth.auth", nil)
email = request.env['omniauth.auth']['info']['email']
else
email = params['session']['email']
end
user = Identity.find_or_create_by(email: email).user
Token.update_or_create_with_omniauth(user, omniauth_env) if omniauth_env
if user
session[:user_id] = user.id
redirect_to feed_user_path(@user), notice: "Signed in!"
else
session[:user_id] = nil
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
users_controller
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to callback_links_path
else
render 'new'
end
end
new.html.erb (사용자)
<%= form_tag("/auth/identity/register", method: 'post') do %>
<% if @identity && @identity.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(@identity.errors.count, "error") %> prohibited this account from being saved:</h2>
<ul>
<% @identity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row" id="content-box-signup">
<div class="span4">
<div class="field">
<%= label_tag :first_name %>
<%= text_field_tag :first_name, @identity.try(:first_name) %>
</div>
<div class="field">
<%= label_tag :last_name %>
<%= text_field_tag :last_name, @identity.try(:last_name) %>
</div>
<div class="field">
<%= label_tag :user_name %>
<%= text_field_tag :user_name, @identity.try(:user_name) %>
</div>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, @identity.try(:email) %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="field">
<%= label_tag :password_confirmation %>
<%= password_field_tag :password_confirmation %>
</div>
<br>
<div class="actions"><%= submit_tag "Create my account", class: "btn btn-large btn-info" %></div>
</div>
</div>
<% end %>
new.html.erb (세션)을 sessions_controller에서
<div class="row" id="content-box-signup">
<div class="span4">
<%= form_tag "/auth/identity/callback" do %>
<div class="field">
<%= label_tag :auth_key, "Email" %>
<%= text_field_tag :auth_key %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="actions"><%= submit_tag "Login" %></div>
<% end %>
<p><%= link_to "Forgot password?", new_password_reset_path %></p>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>