# Authentication for both web & api

**URL:** <https://www.ruby-forum.com/t/authentication-for-both-web-api/237633>\
**Category:** Rails\
**Created:** [November 22, 2013, 1:12am UTC](https://www.ruby-forum.com/t/authentication-for-both-web-api/237633 "2013-11-22T01:12:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [November 22, 2013, 1:12am UTC](https://www.ruby-forum.com/t/authentication-for-both-web-api/237633/1 "2013-11-22T01:12:57Z")

</div>

Hello,

I need to develop an application which needs to authenticate user by  
verifying login name/password for both web access and api. I need the  
same controller to cater to both web and api. I can put the  
before\_filter for the controller to make sure that user is logged-in and  
user\_id is in session. But how do I design the controller that if the  
request comes as a rest web service request then before filter should  
not check session for logged user but should authenticate with  
login/password passed as parameters with request and then should go  
ahead whichever way the request came to controller.

I would really appreciate any code samples/links to sites which explain  
how to do that.

Thanks.

---

<div class="post-metadata">

**Author:** ![sm94582](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@sm94582](https://www.ruby-forum.com/u/sm94582)\
**Post date:** [November 22, 2013, 10:00pm UTC](https://www.ruby-forum.com/t/authentication-for-both-web-api/237633/2 "2013-11-22T22:00:27Z")

</div>

On Nov 21, 2013, at 4:12 PM, renu mehta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ahead whichever way the request came to controller.
> 
> I would really appreciate any code samples/links to sites which explain  
> how to do that.
> 
> Thanks.

So I havent personally implemented such a thing, but Ive used such  
things. AWS API requests do this thing where you establish a connection  
using the two keys (access and secret) and get a token to use for the  
rest of your API accesses (Im pretty sure this times out eventually). Im  
sure you could work something similar to that using the user/pw?

---

<div class="post-metadata">

**Author:** ![sm94582](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e99b99/32.png) [@sm94582](https://www.ruby-forum.com/u/sm94582)\
**Post date:** [November 22, 2013, 10:28pm UTC](https://www.ruby-forum.com/t/authentication-for-both-web-api/237633/3 "2013-11-22T22:28:26Z")

</div>

Basically, I want to know how to implement different authentication  
checks for different request paths - from browser or from api.
