4

I have created a simple REST api for my application using node/ express. I am using AngularJS on the front end to serve pages to the user.

I would like to add functionality such that the API can only be accessed via my front-end and anyone should not be able to do a GET/POST request to my site and get the data?

What strategies can I use to achieve this?

asked Apr 30, 2014 at 16:27
6
  • Using authentication middleware? Passport, for example. Commented Apr 30, 2014 at 16:29
  • I couldn't quite understand how to integrate that into my solution as I am don't want to use password etc.. Commented Apr 30, 2014 at 16:49
  • 1
    You don't have to use password etc.; there are different auth strategies. Commented Apr 30, 2014 at 16:52
  • Ok did some reading, github.com/jaredhanson/passport-anonymous looks like this could help me. Am i correct? Commented Apr 30, 2014 at 16:58
  • This thread may help you: stackoverflow.com/questions/15496915/… Commented Apr 30, 2014 at 17:53

1 Answer 1

1

HTTP request can be formatted and sent to sever by many other means beside a browser (curl for example), so any server always detecting correct source of a request is not guaranteed.

The basic method to protect an endpoint would be to use some kind of authentication. The requesting client must present something uniquely identifying it. API should provide clients a token after it proves itself authentic (via login etc), and all subsequent requests would be checked for this token.

answered Apr 30, 2014 at 17:19
Sign up to request clarification or add additional context in comments.

2 Comments

If my client is a web application written in AngularJS, anyone can view the source and the token, how can I go around that? Sorry if my questions are amateurish.
@dopplesoldner That's why you encrypt it and store it in session storage of one user's browser.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.