Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:57:59+00:00 2026-06-13T23:57:59+00:00

I have the following action: users.rb: def omniauth_create auth = request.env[omniauth.auth] user = User.from_omniauth(env[omniauth.auth])

  • 0

I have the following action:

users.rb:

  def omniauth_create
    auth = request.env["omniauth.auth"]
    user = User.from_omniauth(env["omniauth.auth"])
    unless user.email.blank?
      if user.id.nil?
        # Save the user since he hasn't been created yet
        user.save!
      end
      sign_in user
      redirect_back_or user
    else
      # Send user to a form to fill his email
      #session[:omniauth] = request.env['omniauth.auth'].except('extra')
      redirect_to(enter_email_path(oprovider: user.provider,
                                   ouid: user.uid,
                                   oname: user.name,
                                   opassword: user.password,
                                   opassword_confirmation: user.password))
    end
  end

It does the following:

  • If the user’s email is not blank, sign him in, and redirect him to his profile (and save him if his id is nil. In other words, if he hasn’t been created yet).
  • If the user’s email is blank, send him to enter_email_path (where the user can enter his email).

Now I want to add another if statement that flashes an error if the email had been already taken, and redirects the user to the root_path

I’m not very sure how to do this, Any suggestions? (and where to put that if statement?)

EDIT:

Strange, got this instead of the redirect to the root path:

Validation failed: Email has already been taken

I don’t know if this helps but here is the origin of from_omniauth:

  def self.from_omniauth(auth)
    find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
  end

  def self.create_with_omniauth(auth)
    new do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.email = auth["info"]["email"]
      user.password = user.password_confirmation = SecureRandom.urlsafe_base64(n=6) 
    end
  end

The code as it is right now:

user.rb:

# if user.email.present?
  if user.id.nil?
    # User.find_by_email(user.email).present?
    if User.exists?(:email => user.email)
      redirect_to root_path
    end
    user.save!
  end
  sign_in user
  redirect_back_or user
else

(the rest didn’t change).

It seems like the code is ignoring the if User.exists?(:email => user.email) part?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:58:00+00:00Added an answer on June 13, 2026 at 11:58 pm

    Rails has a method to check if an object exists based on parameters. You could do this:

    if user.email.present?
      if user.id.nil?
        if User.exists?(:email => user.email)
          # return redirect email is already token
        end
    
        # save user
      end
      # sign_in user
    else
      # redirect to get email
    end
    

    By the way, I am not familiar with Omniauth so I am not sure what is right but new_record? is usually used when checking if object is already saved or not. If you have an id, it usually is.
    If you are confused you could create functions in your User model for better reading like

    class User
      def new?
        id.nil?
      end
    
      def email_taken?
        self.class.exists?(:email => email)
      end
    end
    
    # back to controller
    if user.email.present?
      if user.new?
        if user.email_taken?
          # return redirect email is already token
        end
    
        # save user
      end
      # sign_in user
    else
      # redirect to get email
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in my controller action method: if (User.Identity.IsAuthenticated) { //
I have the following route: {language}/{controller}.mvc/{action}/{id} Once a user has choosen the language it
I have the following action, appointments_for_week : class StylistsController < ApplicationController def appointments_for_week stylist
I have the following index action: class ExpensesController < ApplicationController def index() @expenses =
Okay, I have the following create action #posts_controller, nested resource under discussions def create
I have written a function having the following signature: def action_handler(request, model): This action_handler
I have the following action methods: public ActionResult ProfileSettings() { Context con = new
I have the following action method: [HttpGet, Authorize, OutputCache(Duration = 60, VaryByHeader = Cookie,
I have the following action in my ASP.NET Web API: public IEnumerable<Car> carssOfUser(int id,
I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.