Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log in automatically using Device after registration?

In my rails application I am using devise gem for to manage users. I am using mobile_fu gem for to separate users coming from different users.

What I want to achieve here is:

  1. If a user opens my site from a mobile device I will extract its MSISDN number reading header it is already done
  2. If that MSISDN number falls in a particular series then I want to automatic login that user to my website so that he will not have to fill the sign_in form.

How I can achieve this?

like image 547
vidur punj Avatar asked Dec 08 '12 05:12

vidur punj


1 Answers

You have to:

1) Register the user into the website for devise. 2) Login the user.

For option 1, you can do something like:

user = User.find_by_msisdn(params[:msisdn])
if user.nil?
  user = User.create(field_1: value1, field_2: value2)    
end

sign_in(user)

redirect_to after_sign_in_path(user)

The things to have in mind, the first line tries to find for the user, on the second line, if it cannot find the user, it creates the user right away, then it sign_in the user, and finally redirect the user to where he should go after the login.

like image 55
rorra Avatar answered Sep 19 '22 13:09

rorra