Skip to content

Commit

Permalink
Corrigido login por oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardocserra committed Sep 27, 2018
1 parent 8fa3dc9 commit d0039b9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
5 changes: 1 addition & 4 deletions app/controllers/oauths_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ def callback
if !existent_user.present?
@user = create_from(provider)
else
print(existent_user.id)
print(@user.uid)
print('User:')
print(@user.attributes)
Authentication.create(provider: provider, user_id: existent_user.id, uid: @user.uid)
@user = existent_user
end
auto_login(@user)
redirect_to adicionar_dados_path, :notice => "Logged in from #{provider.titleize}!"
Expand Down
29 changes: 25 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,33 @@ def new
def create
@user = User.new(user_params)
@user.role_id = 2

if @user.save
flash[:success] = 'Bem vindo!'
existent_user = User.find_by('email = ? or cpf = ?', user_params[:email], user_params[:cpf])
conseguiu=false
email_cpf_repetido = false
if existent_user.present?
if existent_user.crypted_password.present?
email_cpf_repetido = true
else
@user = existent_user
conseguiu=existent_user.update(password: user_params[:password], password_confirmation: user_params[:password_confirmation])
end
else
conseguiu = @user.save
end
if conseguiu
login(params[:user][:email], params[:user][:password])
redirect_to root_path
redirect_to user_path(@user)
else
@user = User.new(user_params)
if @user.password.length < 8
flash.now[:senha] = 'A senha deve conter no mínimo 8 caracteres.'
end
if @user.password != @user.password_confirmation
flash.now[:senha] = 'As senhas digitadas não coincidem.'
end
if email_cpf_repetido
flash.now[:email] = 'Já existe usuário com esse e-mail ou CPF.'
end
render 'new'
end
end
Expand Down
6 changes: 6 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="row"></div>
</div>
<div class="col s12 l12 z-depth-1">

<%= form_for @user do |f| %>
<div class="col s12 l10 offset-l1 input-field">
<%= f.label :first_name, 'Nome' %>
Expand Down Expand Up @@ -43,6 +44,11 @@
<%= link_to 'Voltar', :back, class: "btn waves-effect waves-light button_bold" %>
</div>
<% end %>
<div>
<% flash.each do |key, value| %>
<p style="color: red; text-align: right"><%= value %></p>
<% end %>
</div>
<div class="row hide-on-med-and-down"></div>
</div>
</div>
2 changes: 1 addition & 1 deletion config/initializers/sorcery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
#
config.google.key = ENV['ESCOLA_MODELO_WS_Google_key']
config.google.secret = ENV['ESCOLA_MODELO_WS_Google_secret']
config.google.callback_url = "http://localhost:3000/oauth/callback?provider=google"
config.google.callback_url = "https://escolamodelows.interlegis.leg.br/oauth/callback?provider=google"
config.google.user_info_mapping = {:uid => "id", :email => "email", :first_name => "name"}
config.google.scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
#
Expand Down

0 comments on commit d0039b9

Please sign in to comment.