Ruby on Rails Allow Tracking / Indexing of protected pages - ruby-on-rails

I need to allow indexing of user protected pages for Facebook sharing. Basically, I have a RoR app that offers coupons and I need for users to be able to share the coupon on facebook. Only problem is that a user must have an account to view all the coupon details on each coupon's page. I want them to be able to share the coupon (title, price only) with a link to that coupon, but the link to the coupon will essentially redirect to the sign up page if the user does not have an account. Therefore, when Facebook (and other indexing robots) go to pull info from the coupon URL, they also get redirected to the sign up page and index the sign up page info instead of the coupon page info.
Any way to allow indexing of each coupon page for crawlers, but redirect actual users to the sign up page?

Found this post... Ended up using the second answer given. In the coupons controller's show method, I just updated my before_filter to something like:
if ( !request.env["HTTP_USER_AGENT"].match(/\(.*https?:\/\/.*\)/) && !user_signed_in? )
redirect_to new_user_registration_path
end
Therefore when a request is made to view a coupon, it will allow a robot through to index the page, but will redirect non-signed-in users to either register or login. Really only useful in my case for Facebook sharing, but I'm sure will help SEO and other indexing. I used Facebook developer tools to verify it was indexing properly.
Hope this is helpful to someone else out there.

Related

Rails redirect_to Unless In the Process of Purchasing

So I have a rails app that uses Koudoku to handle subscription purchases and devise for users. I'd like my users to be redirected to a dashboard page when they login, unless they're in the process of purchasing a subscription.
Here's the problem...
When a visitor goes to buy a subscription, they select a plan and Koudoku checks to see if they are signed in or are a registered user and it prompts them to log-in or sign-up. Sort of a standard 2-step purchase process. Once they've done that, it redirects them to the checkout page where they can enter their credit card information to purchase the plan they clicked on originally.
If I add in a devise standard after_sign_in_path_for(resource) method, to redirect them to the dashboard after they log-in, it breaks the purchase process if they're not already logged-in. They get redirected to the dashboard after they log-in even if they've just picked a plan and were prompted to log-in. They then have to go back to the plans page and pick a plan again.
How do I redirect existing users and subscription owners to the dashboard after log-in, unless they are in the process of purchasing a plan??
I solved this issue by requiring my visitors to become users first before they can view the pricing page and select a plan.
You can use Rails session variable to solve this. Whenever a user selects a plan, you should save the user purchase status(a boolean value) in a session variable. So after login you can check the session variable to conditionally redirect the user to target page.
Don't worry, you can override Devise 'after_sign_in_path_for(resource)' method for conditional redirect. Refer here

progressive engagement with devise in rails 4

I'm trying to implement a functionality that would allow guests to submit booking forms and then be redirected to sign-in or subscribe in order to save it. Superprof.co.uk has the same functionality so here's what I mean:
As a guest (a user who isn't signed in) can click on "book a course" in what would be the "course#show" page in rails
He is then redirected to fill the booking form (even if he isn't signed in)
When he submits the form he is asked to subscribe or sign-in.
Is there a way to do something similar with devise? How can I keep the booking information and save it only after the user signs in ? Thanks in advance for your answers
A simple approach would be to save booking information to session[:booking]. Then overwrite devise controller and after sign up or sign in check if session[:booking] is set and if so create booking records in DB.
A more correct but also a more complicated approach would be to use guest user. You can check guest user record railscast for more information about that.

How to restrict user action from email link without login?

As part of my app, users get to approve certain "requests" via email. Requests have their own model and therefore each has a unique id.
Users get an email with a named route in a link: 'approve/:id' where :id is the id of the request. The approve method then handles the approval logic, etc.
How can I prevent a user from approving requests made to other users without having the user login beforehand? Since the ids are freely displayed in the URL, I guess a GUID would be needed or?
If you really want to do that, then yes, you'd need a GUID of some sort. Perhaps a cryptographic hash of the user_id or email address(?). so you end up using /approve/:id/:GUID.
I'm surprised you don't want the user to login though, remember that if they login you can redirect them on to the approval automatically. Also if the cookie is still valid a user may already be logged in.

Automatically login to Twitter

I'm doing a little experimentation. The end goal is to open Twitter in a new tab for a user that is already logged in.
I have gone down the oAuth route and I can post to twitter, retrieve tweets etc. but when I visit Twitter.com I still get the login page. I have a couple of questions
A) Is it possible to automaticaslly log a user in like this (I can see why it can't be but need to make sure)
B) Does anyone know of another way to acheive it?
When you redirect the user to twitter to authorize, send them to oauth/authenticate, instead.
This flow is called Sign in with Twitter.

Devise sign in and registration forms from somewhere else in my app

I am building an e-commerce style site which lists products that you can buy. When you click on buy it takes you through to a URL like: http://shopfront.com/deal/123/buy.
I would like to provide Devise sign up and sign in on that buy page. I can currently get users to sign up and upon a successful sign up they will be redirected back to my buy page for the item they are interested in but if they provide insufficient details, they get redirected to devise's default sign up form which displays the error they encountered. Afterwards they are no longer redirected to my buy page and instead end up on the home page.
I have my own registrations controller which is where I am doing the redirect back to the relevant buy page on successful sign ups but I cannot figure out how to redirect unsuccessful sign ups back to the buy page.
I need to implement more or less the same functionality for a sign in form which will be displayed next to the sign up form on that same buy page. Any assistance would be appreciated.
Nice way is to store client state in session
put something like,
session[:return_to] = request.fullpath
in your controller, you may like to put this in private method
In general "in session", means to store data in cookies,
as session store points by default to
Rails::Application.config.session_store
=>
ActionDispatch::Session::CookieStore

Resources